2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
185
Task/Continued-fraction/COBOL/continued-fraction.cobol
Normal file
185
Task/Continued-fraction/COBOL/continued-fraction.cobol
Normal file
|
|
@ -0,0 +1,185 @@
|
|||
identification division.
|
||||
program-id. show-continued-fractions.
|
||||
|
||||
environment division.
|
||||
configuration section.
|
||||
repository.
|
||||
function continued-fractions
|
||||
function all intrinsic.
|
||||
|
||||
procedure division.
|
||||
fractions-main.
|
||||
|
||||
display "Square root 2 approximately : "
|
||||
continued-fractions("sqrt-2-alpha", "sqrt-2-beta", 100)
|
||||
display "Napier constant approximately : "
|
||||
continued-fractions("napier-alpha", "napier-beta", 40)
|
||||
display "Pi approximately : "
|
||||
continued-fractions("pi-alpha", "pi-beta", 10000)
|
||||
|
||||
goback.
|
||||
end program show-continued-fractions.
|
||||
|
||||
*> **************************************************************
|
||||
identification division.
|
||||
function-id. continued-fractions.
|
||||
|
||||
data division.
|
||||
working-storage section.
|
||||
01 alpha-function usage program-pointer.
|
||||
01 beta-function usage program-pointer.
|
||||
01 alpha usage float-long.
|
||||
01 beta usage float-long.
|
||||
01 running usage float-long.
|
||||
01 i usage binary-long.
|
||||
|
||||
linkage section.
|
||||
01 alpha-name pic x any length.
|
||||
01 beta-name pic x any length.
|
||||
01 iterations pic 9 any length.
|
||||
01 approximation usage float-long.
|
||||
|
||||
procedure division using
|
||||
alpha-name beta-name iterations
|
||||
returning approximation.
|
||||
|
||||
set alpha-function to entry alpha-name
|
||||
if alpha-function = null then
|
||||
display "error: no " alpha-name " function" upon syserr
|
||||
goback
|
||||
end-if
|
||||
set beta-function to entry beta-name
|
||||
if beta-function = null then
|
||||
display "error: no " beta-name " function" upon syserr
|
||||
goback
|
||||
end-if
|
||||
|
||||
move 0 to alpha beta running
|
||||
perform varying i from iterations by -1 until i = 0
|
||||
call alpha-function using i returning alpha
|
||||
call beta-function using i returning beta
|
||||
compute running = beta / (alpha + running)
|
||||
end-perform
|
||||
call alpha-function using 0 returning alpha
|
||||
compute approximation = alpha + running
|
||||
|
||||
goback.
|
||||
end function continued-fractions.
|
||||
|
||||
*> ******************************
|
||||
identification division.
|
||||
program-id. sqrt-2-alpha.
|
||||
|
||||
data division.
|
||||
working-storage section.
|
||||
01 result usage float-long.
|
||||
|
||||
linkage section.
|
||||
01 iteration usage binary-long unsigned.
|
||||
|
||||
procedure division using iteration returning result.
|
||||
if iteration equal 0 then
|
||||
move 1.0 to result
|
||||
else
|
||||
move 2.0 to result
|
||||
end-if
|
||||
|
||||
goback.
|
||||
end program sqrt-2-alpha.
|
||||
|
||||
*> ******************************
|
||||
identification division.
|
||||
program-id. sqrt-2-beta.
|
||||
|
||||
data division.
|
||||
working-storage section.
|
||||
01 result usage float-long.
|
||||
|
||||
linkage section.
|
||||
01 iteration usage binary-long unsigned.
|
||||
|
||||
procedure division using iteration returning result.
|
||||
move 1.0 to result
|
||||
|
||||
goback.
|
||||
end program sqrt-2-beta.
|
||||
|
||||
*> ******************************
|
||||
identification division.
|
||||
program-id. napier-alpha.
|
||||
|
||||
data division.
|
||||
working-storage section.
|
||||
01 result usage float-long.
|
||||
|
||||
linkage section.
|
||||
01 iteration usage binary-long unsigned.
|
||||
|
||||
procedure division using iteration returning result.
|
||||
if iteration equal 0 then
|
||||
move 2.0 to result
|
||||
else
|
||||
move iteration to result
|
||||
end-if
|
||||
|
||||
goback.
|
||||
end program napier-alpha.
|
||||
|
||||
*> ******************************
|
||||
identification division.
|
||||
program-id. napier-beta.
|
||||
|
||||
data division.
|
||||
working-storage section.
|
||||
01 result usage float-long.
|
||||
|
||||
linkage section.
|
||||
01 iteration usage binary-long unsigned.
|
||||
|
||||
procedure division using iteration returning result.
|
||||
if iteration = 1 then
|
||||
move 1.0 to result
|
||||
else
|
||||
compute result = iteration - 1.0
|
||||
end-if
|
||||
|
||||
goback.
|
||||
end program napier-beta.
|
||||
|
||||
*> ******************************
|
||||
identification division.
|
||||
program-id. pi-alpha.
|
||||
|
||||
data division.
|
||||
working-storage section.
|
||||
01 result usage float-long.
|
||||
|
||||
linkage section.
|
||||
01 iteration usage binary-long unsigned.
|
||||
|
||||
procedure division using iteration returning result.
|
||||
if iteration equal 0 then
|
||||
move 3.0 to result
|
||||
else
|
||||
move 6.0 to result
|
||||
end-if
|
||||
|
||||
goback.
|
||||
end program pi-alpha.
|
||||
|
||||
*> ******************************
|
||||
identification division.
|
||||
program-id. pi-beta.
|
||||
|
||||
data division.
|
||||
working-storage section.
|
||||
01 result usage float-long.
|
||||
|
||||
linkage section.
|
||||
01 iteration usage binary-long unsigned.
|
||||
|
||||
procedure division using iteration returning result.
|
||||
compute result = (2 * iteration - 1) ** 2
|
||||
|
||||
goback.
|
||||
end program pi-beta.
|
||||
8
Task/Continued-fraction/Clojure/continued-fraction.clj
Normal file
8
Task/Continued-fraction/Clojure/continued-fraction.clj
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
(defn cfrac
|
||||
[a b n]
|
||||
(letfn [(cfrac-iter [[x k]] [(+ (a k) (/ (b (inc k)) x)) (dec k)])]
|
||||
(ffirst (take 1 (drop (inc n) (iterate cfrac-iter [1 n]))))))
|
||||
|
||||
(def sq2 (cfrac #(if (zero? %) 1.0 2.0) (constantly 1.0) 100))
|
||||
(def e (cfrac #(if (zero? %) 2.0 %) #(if (= 1 %) 1.0 (double (dec %))) 100))
|
||||
(def pi (cfrac #(if (zero? %) 3.0 6.0) #(let [x (- (* 2.0 %) 1.0)] (* x x)) 900000))
|
||||
25
Task/Continued-fraction/Java/continued-fraction.java
Normal file
25
Task/Continued-fraction/Java/continued-fraction.java
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import static java.lang.Math.pow;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class Test {
|
||||
static double calc(Function<Integer, Integer[]> f, int n) {
|
||||
double temp = 0;
|
||||
|
||||
for (int ni = n; ni >= 1; ni--) {
|
||||
Integer[] p = f.apply(ni);
|
||||
temp = p[1] / (double) (p[0] + temp);
|
||||
}
|
||||
return f.apply(0)[0] + temp;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<Function<Integer, Integer[]>> fList = new ArrayList<>();
|
||||
fList.add(n -> new Integer[]{n > 0 ? 2 : 1, 1});
|
||||
fList.add(n -> new Integer[]{n > 0 ? n : 2, n > 1 ? (n - 1) : 1});
|
||||
fList.add(n -> new Integer[]{n > 0 ? 6 : 3, (int) pow(2 * n - 1, 2)});
|
||||
|
||||
for (Function<Integer, Integer[]> f : fList)
|
||||
System.out.println(calc(f, 200));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,80 +1,43 @@
|
|||
/*REXX program calculates and displays values of some specific continued*/
|
||||
/*───────────── fractions (along with their α and ß terms). */
|
||||
/*───────────── Continued fractions: also known as anthyphairetic ratio.*/
|
||||
T=500 /*use 500 terms for calculations.*/
|
||||
showDig=100; numeric digits 2*showDig /*use 100 digits for the display.*/
|
||||
a=; @=; b= /*omitted ß terms are assumed = 1*/
|
||||
/*══════════════════════════════════════════════════════════════════════*/
|
||||
a=1 rep(2); call tell '√2'
|
||||
/*══════════════════════════════════════════════════════════════════════*/
|
||||
a=1 rep(1 2); call tell '√3' /*also: 2∙sin(π/3) */
|
||||
/*══════════════════════════════════════ ___ ════════════════════════*/
|
||||
/*generalized √ N */
|
||||
do N=2 to 11; a=1 rep(2); b=rep(N-1); call tell 'gen √'N; end
|
||||
N=1/2; a=1 rep(2); b=rep(N-1); call tell 'gen √½'
|
||||
/*══════════════════════════════════════════════════════════════════════*/
|
||||
do j=1 for T; a=a j; end; b=1 a; a=2 a; call tell 'e'
|
||||
/*══════════════════════════════════════════════════════════════════════*/
|
||||
do j=1 for T by 2; a=a j; b=b j+1; end; call tell '1÷[√e-1]'
|
||||
/*══════════════════════════════════════════════════════════════════════*/
|
||||
do j=1 for T; a=a j; end; b=a; a=0 a; call tell '1÷[e-1]'
|
||||
/*══════════════════════════════════════════════════════════════════════*/
|
||||
a=1 rep(1); call tell 'φ, phi'
|
||||
/*══════════════════════════════════════════════════════════════════════*/
|
||||
a=1; do j=1 for T by 2; a=a j 1; end; call tell 'tan(1)'
|
||||
/*══════════════════════════════════════════════════════════════════════*/
|
||||
a=1; do j=1 for T; a=a 2*j+1; end; call tell 'coth(1)'
|
||||
/*══════════════════════════════════════════════════════════════════════*/
|
||||
a=2; do j=1 for T; a=a 4*j+2; end; call tell 'coth(½)' /*also: [e+1] ÷ [e-1] */
|
||||
/*══════════════════════════════════════════════════════════════════════*/
|
||||
T=10000
|
||||
a=1 rep(2)
|
||||
do j=1 for T by 2; b=b j**2; end; call tell '4÷π'
|
||||
/*══════════════════════════════════════════════════════════════════════*/
|
||||
T=10000
|
||||
a=1; do j=1 for T; a=a 1/j; @=@ '1/'j; end; call tell '½π, ½pi'
|
||||
/*══════════════════════════════════════════════════════════════════════*/
|
||||
T=10000
|
||||
a=0 1 rep(2)
|
||||
do j=1 for T by 2; b=b j**2; end; b=4 b; call tell 'π, pi'
|
||||
/*══════════════════════════════════════════════════════════════════════*/
|
||||
T=10000
|
||||
a=0; do j=1 for T; a=a j*2-1; b=b j**2; end; b=4 b; call tell 'π, pi'
|
||||
/*══════════════════════════════════════════════════════════════════════*/
|
||||
T=100000
|
||||
a=3 rep(6)
|
||||
do j=1 for T by 2; b=b j**2; end; call tell 'π, pi'
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
|
||||
/*────────────────────────────────CF subroutine─────────────────────────*/
|
||||
cf: procedure; parse arg C x,y; !=0; numeric digits digits()+5
|
||||
do k=words(x) to 1 by -1; a=word(x,k); b=word(word(y,k) 1,1)
|
||||
d=a+!; if d=0 then call divZero /*in case divisor is bogus.*/
|
||||
!=b/d /*here's a binary mosh pit.*/
|
||||
end /*k*/
|
||||
return !+C
|
||||
/*────────────────────────────────DIVZERO subroutine────────────────────*/
|
||||
divZero: say; say '***error!***'; say 'division by zero.'; say; exit 13
|
||||
/*────────────────────────────────GETT subroutine───────────────────────*/
|
||||
getT: parse arg stuff,width,ma,mb,_
|
||||
do m=1; mm=m+ma; mn=max(1,m-mb); w=word(stuff,m)
|
||||
w=right(w,max(length(word(As,mm)),length(word(Bs,mn)),length(w)))
|
||||
if length(_ w)>width then leave /*stop getting terms?*/
|
||||
_=_ w /*whole, don't chop. */
|
||||
end /*m*/ /*done building terms*/
|
||||
return strip(_) /*strip leading blank*/
|
||||
/*────────────────────────────────REP subroutine────────────────────────*/
|
||||
rep: parse arg rep; return space(copies(' 'rep, T%words(rep)))
|
||||
/*────────────────────────────────RF subroutine─────────────────────────*/
|
||||
rf: parse arg xxx,z
|
||||
do m=1 for T; w=word(xxx,m) ; if w=='1/1' | w=1 then w=1
|
||||
if w=='1/2' | w=1/2 then w='½'; if w=-.5 then w='-½'
|
||||
if w=='1/4' | w=1/4 then w='¼'; if w=-.25 then w='-¼'
|
||||
z=z w
|
||||
end
|
||||
return z /*done re-formatting.*/
|
||||
/*────────────────────────────────TELL subroutine───────────────────────*/
|
||||
tell: parse arg ?; v=cf(a,b); numeric digits showdig; As=rf(@ a); Bs=rf(b)
|
||||
say right(?,8) '=' left(v/1,showdig) ' α terms= ' getT(As,72 ,0,1)
|
||||
if b\=='' then say right('',8+2+showdig+1) ' ß terms= ' getT(Bs,72-2,1,0)
|
||||
a=; @=; b=; return
|
||||
/*REXX program calculates and displays values of various continued fractions. */
|
||||
parse arg terms digs .
|
||||
if terms=='' | terms=="," then terms=500
|
||||
if digs=='' | digs=="," then digs=100
|
||||
numeric digits digs /*use 100 decimal digits for display.*/
|
||||
b.=1 /*omitted ß terms are assumed to be 1.*/
|
||||
/*══════════════════════════════════════════════════════════════════════════════════════*/
|
||||
a.=2; call tell '√2', cf(1)
|
||||
/*══════════════════════════════════════════════════════════════════════════════════════*/
|
||||
a.=1; do N=2 by 2 to terms; a.N=2; end; call tell '√3', cf(1) /*also: 2∙sin(π/3) */
|
||||
/*══════════════════════════════════════════════════════════════════════════════════════*/
|
||||
a.=2 /* ___ */
|
||||
do N=2 to 17 /*generalized √ N */
|
||||
b.=N-1; NN=right(N, 2); call tell 'gen √'NN, cf(1)
|
||||
end /*N*/
|
||||
/*══════════════════════════════════════════════════════════════════════════════════════*/
|
||||
a.=2; b.=-1/2; call tell 'gen √ ½', cf(1)
|
||||
/*══════════════════════════════════════════════════════════════════════════════════════*/
|
||||
do j=1 for terms; a.j=j; if j>1 then b.j=a.p; p=j; end; call tell 'e', cf(2)
|
||||
/*══════════════════════════════════════════════════════════════════════════════════════*/
|
||||
a.=1; call tell 'φ, phi', cf(1)
|
||||
/*══════════════════════════════════════════════════════════════════════════════════════*/
|
||||
a.=1; do j=1 for terms; if j//2 then a.j=j; end; call tell 'tan(1)', cf(1)
|
||||
/*══════════════════════════════════════════════════════════════════════════════════════*/
|
||||
do j=1 for terms; a.j=2*j+1; end; call tell 'coth(1)', cf(1)
|
||||
/*══════════════════════════════════════════════════════════════════════════════════════*/
|
||||
do j=1 for terms; a.j=4*j+2; end; call tell 'coth(½)', cf(2) /*also: [e+1]÷[e-1] */
|
||||
/*══════════════════════════════════════════════════════════════════════════════════════*/
|
||||
terms=100000
|
||||
a.=6; do j=1 for terms; b.j=(2*j-1)**2; end; call tell 'π, pi', cf(3)
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
cf: procedure expose a. b. terms; parse arg C; !=0; numeric digits 9+digits()
|
||||
do k=terms by -1 for terms; d=a.k+!; !=b.k/d
|
||||
end /*k*/
|
||||
return !+C
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
tell: parse arg ?,v; $=left(format(v)/1,1+digits()); w=50 /*50 bytes of terms*/
|
||||
aT=; do k=1; _=space(aT a.k); if length(_)>w then leave; aT=_; end /*k*/
|
||||
bT=; do k=1; _=space(bT b.k); if length(_)>w then leave; bT=_; end /*k*/
|
||||
say right(?,8) "=" $ ' α terms='aT ...
|
||||
if b.1\==1 then say right("",12+digits()) ' ß terms='bT ...
|
||||
a=; b.=1; return /*only 50 bytes of α & ß terms ↑ are displayed. */
|
||||
|
|
|
|||
45
Task/Continued-fraction/Rust/continued-fraction.rust
Normal file
45
Task/Continued-fraction/Rust/continued-fraction.rust
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
use std::iter;
|
||||
|
||||
// Calculating a continued fraction is quite easy with iterators, however
|
||||
// writing a proper iterator adapter is less so. We settle for a macro which
|
||||
// for most purposes works well enough.
|
||||
//
|
||||
// One limitation with this iterator based approach is that we cannot reverse
|
||||
// input iterators since they are not usually DoubleEnded. To circumvent this
|
||||
// we can collect the elements and then reverse them, however this isn't ideal
|
||||
// as we now have to store elements equal to the number of iterations.
|
||||
//
|
||||
// Another is that iterators cannot be resused once consumed, so it is often
|
||||
// required to make many clones of iterators.
|
||||
macro_rules! continued_fraction {
|
||||
($a:expr, $b:expr ; $iterations:expr) => (
|
||||
($a).zip($b)
|
||||
.take($iterations)
|
||||
.collect::<Vec<_>>().iter()
|
||||
.rev()
|
||||
.fold(0 as f64, |acc: f64, &(x, y)| {
|
||||
x as f64 + (y as f64 / acc)
|
||||
})
|
||||
);
|
||||
|
||||
($a:expr, $b:expr) => (continued_fraction!($a, $b ; 1000));
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Sqrt(2)
|
||||
let sqrt2a = (1..2).chain(iter::repeat(2));
|
||||
let sqrt2b = iter::repeat(1);
|
||||
println!("{}", continued_fraction!(sqrt2a, sqrt2b));
|
||||
|
||||
|
||||
// Napier's Constant
|
||||
let napiera = (2..3).chain(1..);
|
||||
let napierb = (1..2).chain(1..);
|
||||
println!("{}", continued_fraction!(napiera, napierb));
|
||||
|
||||
|
||||
// Pi
|
||||
let pia = (3..4).chain(iter::repeat(6));
|
||||
let pib = (1i64..).map(|x| (2 * x - 1).pow(2));
|
||||
println!("{}", continued_fraction!(pia, pib));
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
10 LET a0=1: LET b1=1: LET a$="2": LET b$="1": PRINT "SQR(2) = ";: GO SUB 1000
|
||||
20 LET a0=2: LET b1=1: LET a$="N": LET b$="N": PRINT "e = ";: GO SUB 1000
|
||||
30 LET a0=3: LET b1=1: LET a$="6": LET b$="(2*N+1)^2": PRINT "PI = ";: GO SUB 1000
|
||||
100 STOP
|
||||
1000 LET n=0: LET e$="": LET p$=""
|
||||
1010 LET n=n+1
|
||||
1020 LET e$=e$+STR$ VAL a$+"+"+STR$ VAL b$+"/("
|
||||
1030 IF LEN e$<(4000-n) THEN GO TO 1010
|
||||
1035 FOR i=1 TO n: LET p$=p$+")": NEXT i
|
||||
1040 PRINT a0+b1/VAL (e$+"1"+p$)
|
||||
1050 RETURN
|
||||
Loading…
Add table
Add a link
Reference in a new issue