Continued fraction: Difference between revisions

m
 
(17 intermediate revisions by 9 users not shown)
Line 1,119:
2.7182818284590452354
3.1415926228048469486</pre>
 
=={{header|dc}}==
 
<syntaxhighlight lang="dc">[20k 0 200 si [li lbx r li lax + / li 1 - dsi 0<:]ds:x 0 lax +]sf
 
[[2q]s2[0<2 1]sa[R1]sb]sr # sqrt(2)
[[R2q]s2[d 0=2]sa[R1q]s1[d 1=1 1-]sb]se # e
[[3q]s3[0=3 6]sa[2*1-d*]sb]sp # pi
 
c lex lfx p
lrx lfx p
lpx lfx p</syntaxhighlight>
{{out}}
<pre>3.14159262280484694855
1.41421356237309504880
2.71828182845904523536</pre>
 
20 decimal places and 200 iterations.
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
numfmt 8 0
func calc_sqrt .
n = 100
sum = n
while n >= 1
a = 1
if n > 1
a = 2
.
b = 1
sum = a + b / sum
n -= 1
.
return sum
.
func calc_e .
n = 100
sum = n
while n >= 1
a = 2
if n > 1
a = n - 1
.
b = 1
if n > 1
b = n - 1
.
sum = a + b / sum
n -= 1
.
return sum
.
func calc_pi .
n = 100
sum = n
while n >= 1
a = 3
if n > 1
a = 6
.
b = 2 * n - 1
b *= b
sum = a + b / sum
n -= 1
.
return sum
.
print calc_sqrt
print calc_e
print calc_pi
</syntaxhighlight>
 
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">
defmodule CFrac do
def compute([a | _], []), do: a
def compute([a | as], [b | bs]), do: a + b/compute(as, bs)
 
def sqrt2 do
a = [1 | Stream.cycle([2]) |> Enum.take(1000)]
b = Stream.cycle([1]) |> Enum.take(1000)
IO.puts compute(a, b)
end
 
def exp1 do
a = [2 | Stream.iterate(1, &(&1 + 1)) |> Enum.take(1000)]
b = [1 | Stream.iterate(1, &(&1 + 1)) |> Enum.take(999)]
IO.puts compute(a, b)
end
 
def pi do
a = [3 | Stream.cycle([6]) |> Enum.take(1000)]
b = 1..1000 |> Enum.map(fn k -> (2*k - 1)**2 end)
IO.puts compute(a, b)
end
end
</syntaxhighlight>
 
{{out}}
<pre>
>elixir -e CFrac.sqrt2()
1.4142135623730951
 
>elixir -e CFrac.exp1()
2.7182818284590455
 
>elixir -e CFrac.pi()
3.141592653340542
</pre>
 
=={{header|Erlang}}==
Line 1,293 ⟶ 1,403:
2.71828165766640 < e < 2.71828184277783
2.71828182735187 < e < 2.71828184277783
</pre>
===Apéry's constant===
See [https://tpiezas.wordpress.com/2012/05/04/continued-fractions-for-zeta2-and-zeta3/ Continued fractions for Zeta(2) and Zeta(3)] section II. Zeta(3)
<syntaxhighlight lang="fsharp">
let aπ()=let mutable n=0 in (fun ()->n<-n+1;-decimal(pown n 6))
let bπ()=let mutable n=0M in (fun ()->n<-n+1M; (2M*n-1M)*(17M*n*n-17M*n+5M))
cf2S (aπ()) (bπ()) |>Seq.map(fun n->6M/n) |> Seq.take 10 |> Seq.pairwise |> Seq.iter(fun(n,g)->printfn "%1.20f < p < %- 1.20f" (min n g) (max n g));;
</syntaxhighlight>
{{out}}
<pre>
1.20205479452054794521 < p < 1.20205690119184928874
1.20205690119184928874 < p < 1.20205690315781676650
1.20205690315781676650 < p < 1.20205690315959270361
1.20205690315959270361 < p < 1.20205690315959428400
1.20205690315959428400 < p < 1.20205690315959428540
1.20205690315959428540 < p < 1.20205690315959428540
1.20205690315959428540 < p < 1.20205690315959428540
1.20205690315959428540 < p < 1.20205690315959428540
1.20205690315959428540 < p < 1.20205690315959428540
</pre>
 
Line 1,564 ⟶ 1,693:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Continued_fraction}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
The following function definition creates a continued fraction:
In '''[https://formulae.org/?example=Continued_fraction this]''' page you can see the program(s) related to this task and their results.
 
[[File:Fōrmulæ - Continued fraction 01.png]]
 
The function accepts the following parameters:
 
{| class="wikitable" style="margin:auto"
|-
! Parameter !! Description
|-
| a₀ || Value for a₀
|-
| λa || Lambda expression to define aᵢ
|-
| λb || Lambda expression to define bᵢ
|-
| depth || Depth to calculate the continued fraction
|}
 
Since Fōrmulæ arithmetic simplifies numeric results as they are generated, the result is not a continued fraction by default.
 
If we want to create the structure, we can introduce the parameters as string or text expressions (or lambda expressions that produce them). Because string or text expressions are not reduced when they are operands of additions and divisions, the structure is preserved, such as follows:
 
[[File:Fōrmulæ - Continued fraction 02.png]]
 
[[File:Fōrmulæ - Continued fraction 03.png]]
 
'''Case 1.''' <math>\sqrt 2</math>
 
In this case
 
* a₀ is 1
* λa is n ↦ 2
* λb is n ↦ 1
 
Let us show the results as a table, for several levels of depth (1 to 10).
 
The columns are:
 
* The depth
* The "textual" call, in order to generate the structure
* The normal (numeric) call. Since arithmetic operations are exact by default, it is usually a rational number.
* The value of the normal (numeric) call, forced to be shown as a decimal number, by using the Math.Numeric expression (the N(x) expression)
 
[[File:Fōrmulæ - Continued fraction 04.png]]
 
[[File:Fōrmulæ - Continued fraction 05.png]]
 
'''Case 2.''' <math>e</math>
 
In this case
 
* a₀ is 2
* λa is n ↦ n
* λb is n ↦ 1 if n = 1, n - 1 elsewhere
 
[[File:Fōrmulæ - Continued fraction 06.png]]
 
[[File:Fōrmulæ - Continued fraction 07.png]]
 
'''Case 3.''' <math>\pi</math>
 
In this case
 
* a₀ is 3
* λa is n ↦ 6
* λb is n ↦ 2(n - 1)²
 
[[File:Fōrmulæ - Continued fraction 08.png]]
 
[[File:Fōrmulæ - Continued fraction 09.png]]
 
=={{header|Go}}==
Line 1,912 ⟶ 2,111:
 
=={{header|Julia}}==
{{works with|Julia|1.08.5}}
<syntaxhighlightHigh lang="julia"># juliasperformant lazy iterators allow evaluation on demand with Julias iterators.
<syntaxhighlight lang="julia">
using .Iterators: countfrom, flatten, repeated, zip
using .MathConstants: ℯ
using Printf
 
function cf(a₀, a, b = repeated(1))
using .Iterators, .MathConstants, Printf
 
function cf(a, b = repeated(1))
a₀, a = peel(a)
m = BigInt[a₀ 1; 1 0]
coeffs = for (aᵢ, bᵢ) ∈ zip(a, b)
m *= [aᵢ 1; bᵢ 0]
while !isapprox(m[1]/m[2], m[3]/m[4]; atol = 1e-9)
isapprox(m[1]/m[2], m[3]/m[4]; atol = 1e-12) && break
(aᵢ, bᵢ), coeffs = peel(coeffs)
end
m *= [aᵢ 1; bᵢ 0]
end
m[1]/m[2]
end
 
out((k, v)) = @printf "%2s: %.12f ≈ %.12f\n" k v eval(k)
for (k, v) in [
:(√2) => cf(flatten((1, repeated(2)))),
:(ℯ) => cf(flatten((2, countfrom())),
flatten((1, countfrom()))),
:(π) => cf(flatten((3, repeated(6))),
(k^2 for k ∈ countfrom(1, 2)))]
@printf "%2s: %.9f ≈ %.9f\n" k v eval(k)
end</syntaxhighlight>
 
foreach(out, (
:(√2) => cf(1, repeated(2)),
:ℯ => cf(2, countfrom(), flatten((1, countfrom()))),
:π => cf(3, repeated(6), (k^2 for k ∈ countfrom(1, 2)))))
</syntaxhighlight>
{{out}}
<pre>√2: 1.414213562414213562373 ≈ 1.414213562414213562373
ℯ: 2.718281828718281828459 ≈ 2.718281828718281828459
π: 3.141592653141592653590 ≈ 3.141592654141592653590</pre>
 
=={{header|Klong}}==
Line 3,215 ⟶ 3,412:
e = 2.718281828459046
PI = 3.141592653588017
</pre>
 
=={{header|RPL}}==
This task demonstrates how both global and local variables, arithmetic expressions and stack can be used together to build a compact and versatile piece of code.
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
4 ROLL ROT → an bn
≪ 'N' STO 0 '''WHILE''' N 2 ≥ '''REPEAT'''
an + INV bn * EVAL
'N' 1 STO- '''END'''
an + / + EVAL
'N' PURGE
≫ ≫ ‘'''→CFRAC'''’ STO
|
'''→CFRAC''' ''( a0 an b1 bn N -- x ) ''
Unstack an and bn
Loop from N to 2
Calculate Nth fraction
Decrement counter
Calculate last fraction with b1 in stack, then add a0
Discard N variable - not mandatory but hygienic
|}
{{in}}
<pre>
1 2 1 1 100 →CFRAC
2 'N' 1 'N-1' 100 →CFRAC
3 6 1 '(2*N-1)^2' 1000 →CFRAC
1 1 1 1 100 →CFRAC
1 '2-MOD(N,2)' 1 1 100 →CFRAC
</pre>
{{out}}
<pre>
5: 1.41421356237
4: 2.71828182846
3: 3.14159265334
2: 1.61803398875
1: 1.73205080757
</pre>
 
Line 3,634 ⟶ 3,874:
e ≈ 2.7182818284590455
π ≈ 3.141592653339042</pre>
 
{{trans|Java}}
 
<syntaxhighlight lang="swift">
import Foundation
 
func calculate(n: Int, operation: (Int) -> [Int])-> Double {
var tmp: Double = 0
for ni in stride(from: n, to:0, by: -1) {
var p = operation(ni)
tmp = Double(p[1])/(Double(p[0]) + tmp);
}
return Double(operation(0)[0]) + tmp;
}
 
func sqrt (n: Int) -> [Int] {
return [n > 0 ? 2 : 1, 1]
}
 
func napier (n: Int) -> [Int] {
var res = [n > 0 ? n : 2, n > 1 ? (n - 1) : 1]
return res
}
 
func pi(n: Int) -> [Int] {
var res = [n > 0 ? 6 : 3, Int(pow(Double(2 * n - 1), 2))]
return res
}
print (calculate(n: 200, operation: sqrt));
print (calculate(n: 200, operation: napier));
print (calculate(n: 200, operation: pi));
 
</syntaxhighlight>
 
=={{header|Tcl}}==
Line 3,763 ⟶ 4,036:
=={{header|Wren}}==
{{trans|D}}
<syntaxhighlight lang="ecmascriptwren">var calc = Fn.new { |f, n|
var t = 0
for (i in n..1) {
2,120

edits