Continued fraction: Difference between revisions

m
 
(21 intermediate revisions by 10 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,739 ⟶ 1,938:
main :: IO ()
main = mapM_ putStrLn [cf2dec 200 sqrt2, cf2dec 200 napier, cf2dec 200 pie]</syntaxhighlight>
 
=={{header|J}}==
<syntaxhighlight lang="j"> cfrac=: +`% / NB. Evaluate a list as a continued fraction
 
sqrt2=: cfrac 1 1,200$2 1x
pi=:cfrac 3, , ,&6"0 *:<:+:>:i.100x
e=: cfrac 2 1, , ,~"0 >:i.100x
 
NB. translate from fraction to decimal string
NB. translated from factor
dec =: (-@:[ (}.,'.',{.) ":@:<.@:(* 10x&^)~)"0
 
100 10 100 dec sqrt2, pi, e
1.4142135623730950488016887242096980785696718753769480731766797379907324784621205551109457595775322165
3.1415924109
2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274</syntaxhighlight>
 
Note that there are two kinds of continued fractions. The kind here where we alternate between '''a''' and '''b''' values, but in some other tasks '''b''' is always 1 (and not included in the list we use to represent the continued fraction). The other kind is evaluated in J using <code>(+%)/</code> instead of <code>+`%/</code>.
 
=={{header|Icon}}==
Line 1,767 ⟶ 1,948:
 
procedure main ()
local sqrt2_frac, e_frac, pi_frac
writes (" sqrt 2.0 = ")
write (evaluate_continued_fraction (continued_fraction (sqrt2_a, sqrt2_b),
Line 1,819 ⟶ 1,999:
pi = 3.141592411
</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j"> cfrac=: +`% / NB. Evaluate a list as a continued fraction
 
sqrt2=: cfrac 1 1,200$2 1x
pi=:cfrac 3, , ,&6"0 *:<:+:>:i.100x
e=: cfrac 2 1, , ,~"0 >:i.100x
 
NB. translate from fraction to decimal string
NB. translated from factor
dec =: (-@:[ (}.,'.',{.) ":@:<.@:(* 10x&^)~)"0
 
100 10 100 dec sqrt2, pi, e
1.4142135623730950488016887242096980785696718753769480731766797379907324784621205551109457595775322165
3.1415924109
2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274</syntaxhighlight>
 
Note that there are two kinds of continued fractions. The kind here where we alternate between '''a''' and '''b''' values, but in some other tasks '''b''' is always 1 (and not included in the list we use to represent the continued fraction). The other kind is evaluated in J using <code>(+%)/</code> instead of <code>+`%/</code>.
 
=={{header|Java}}==
Line 1,913 ⟶ 2,111:
 
=={{header|Julia}}==
{{works with|Julia|01.68.5}}
High performant lazy evaluation on demand with Julias iterators.
<syntaxhighlight lang="julia">function _sqrt(a::Bool, n)
<syntaxhighlight lang="julia">
if a
using .Iterators: countfrom, flatten, repeated, zip
return n > 0 ? 2.0 : 1.0
using .MathConstants: ℯ
else
using Printf
return 1.0
end
end
 
function _napiercf(a₀, a::Bool, nb = repeated(1))
m = BigInt[a₀ 1; 1 0]
if a
for (aᵢ, bᵢ) ∈ zip(a, b)
return n > 0 ? Float64(n) : 2.0
m *= [aᵢ 1; bᵢ 0]
else
returnisapprox(m[1]/m[2], nm[3]/m[4]; >atol 1 ? n= 1e-12) 1.0&& : 1.0break
end
m[1]/m[2]
end
 
out((k, v)) = @printf "%2s: %.12f ≈ %.12f\n" k v eval(k)
function _pi(a::Bool, n)
if a
return n > 0 ? 6.0 : 3.0
else
return (2.0 * n - 1.0) ^ 2.0 # exponentiation operator
end
end
 
function calc(f::Function, expansions::Integer)
a, b = true, false
r = 0.0
for i in expansions:-1:1
r = f(b, i) / (f(a, i) + r)
end
return f(a, 0) + r
end
 
for (v, f) in (("√2", _sqrt), ("e", _napier), ("π", _pi))
@printf("%3s = %f\n", v, calc(f, 1000))
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.414213562373 = 1.414214414213562373
ℯ: 2.718281828459 ≈ 2.718281828459
e = 2.718282
π: 3.141592653590 = 3.141593141592653590</pre>
 
=={{header|Klong}}==
Line 3,229 ⟶ 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,648 ⟶ 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,777 ⟶ 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