2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,6 +1,14 @@
|
|||
Create a program to continually calculate and output the next digit of <math>\pi</math> (pi). The program should continue forever (until it is aborted by the user) calculating and outputting each digit in succession. The output should be a decimal sequence beginning 3.14159265 ...
|
||||
[[File:pi_symbol.jpg|500px||right]]
|
||||
|
||||
Create a program to continually calculate and output the next decimal digit of <big><big><math>\pi</math></big></big> (pi).
|
||||
|
||||
The program should continue forever (until it is aborted by the user) calculating and outputting each decimal digit in succession.
|
||||
|
||||
The output should be a decimal sequence beginning 3.14159265 ...
|
||||
|
||||
|
||||
Note: this task is about calculating pi. For information on built-in pi constants see [[Real constants and functions]].
|
||||
Note: this task is about ''calculating'' pi. For information on built-in pi constants see [[Real constants and functions]].
|
||||
|
||||
|
||||
Related Task [[Arithmetic-geometric mean/Calculate Pi]]
|
||||
<br><br>
|
||||
|
|
|
|||
104
Task/Pi/360-Assembly/pi.360
Normal file
104
Task/Pi/360-Assembly/pi.360
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
* Spigot algorithm do the digits of PI 02/07/2016
|
||||
PISPIG CSECT
|
||||
USING PISPIG,R13 base register
|
||||
B 72(R15) skip savearea
|
||||
DC 17F'0' savearea
|
||||
STM R14,R12,12(R13) prolog
|
||||
ST R13,4(R15) "
|
||||
ST R15,8(R13) "
|
||||
LR R13,R15 "
|
||||
SR R0,R0 0
|
||||
ST R0,MORE more=0
|
||||
LA R6,1 i=1
|
||||
LOOPI1 C R6,=A(NBUF) do i=1 to hbound(buf)
|
||||
BH ELOOPI1 "
|
||||
SR R9,R9 karray=0
|
||||
L R7,=A(NVECT) j=hbound(vect)
|
||||
LR R1,R7 j
|
||||
SLA R1,2 .
|
||||
LA R10,VECT-4(R1) r10=@vect(j)
|
||||
LOOPJ EQU * do j=hbound(vect) to 1 by -1
|
||||
L R5,=F'100000' 100000
|
||||
M R4,0(R10) *vect(j)
|
||||
LR R2,R5 r2=100000*vect(j)
|
||||
LR R5,R9 karray
|
||||
MR R4,R7 karray*j
|
||||
AR R2,R5 r2+karray*j
|
||||
LR R11,R2 n=100000*vect(j)+karray*j
|
||||
LR R3,R7 j
|
||||
SLA R3,1 2*j
|
||||
BCTR R3,0 2*j-1)
|
||||
LR R4,R11 n
|
||||
SRDA R4,32 .
|
||||
DR R4,R3 n/(2*j-1)
|
||||
LR R9,R5 karray=n/(2*j-1)
|
||||
LR R5,R9 karray
|
||||
MR R4,R3 karray*(2*j-1)
|
||||
LR R1,R11 n
|
||||
SR R1,R5 n-karray*(2*j-1)
|
||||
ST R1,0(R10) vect(j)=n-karray*(2*j-1)
|
||||
SH R10,=H'4' r10=@vect(j)
|
||||
BCT R7,LOOPJ end do j
|
||||
LR R4,R9 karray
|
||||
SRDA R4,32 .
|
||||
D R4,=F'100000' karray/100000
|
||||
LR R11,R5 k=karray/100000
|
||||
L R2,MORE more
|
||||
AR R2,R11 +k
|
||||
LR R1,R6 i
|
||||
SLA R1,2 .
|
||||
ST R2,BUF-4(R1) buf(i)=more+k
|
||||
LR R5,R11 k
|
||||
M R4,=F'100000' *100000
|
||||
LR R1,R9 karray
|
||||
SR R1,R5 -k*100000
|
||||
ST R1,MORE more=karray-k*100000
|
||||
LA R6,1(R6) i=i+1
|
||||
B LOOPI1 end do i
|
||||
ELOOPI1 L R1,BUF buf(1)
|
||||
CVD R1,PACKED convert buf(1) to packed decimal
|
||||
OI PACKED+7,X'0F' prepare unpack
|
||||
UNPK PG(1),PACKED packed decimal to zoned printable
|
||||
MVI PG+1,C'.' output '.'
|
||||
XPRNT PG,80 print buffer
|
||||
MVC PG,=CL80' ' clear buffer
|
||||
LA R3,PG pgi=0
|
||||
LA R6,2 i=2
|
||||
LOOPI2 C R6,=A(NBUF) do i=2 to hbound(buf)
|
||||
BH ELOOPI2 "
|
||||
MVC 0(1,R3),=C' ' output ' '
|
||||
LA R3,1(R3) pgi=pgi+1
|
||||
LR R1,R6 i
|
||||
SLA R1,2 .
|
||||
L R2,BUF-4(R1) buf(i)
|
||||
CVD R2,PACKED convert v to packed decimal
|
||||
OI PACKED+7,X'0F' prepare unpack
|
||||
UNPK XDEC,PACKED packed decimal to zoned printable
|
||||
MVC 0(5,R3),XDEC+7 output buf(i) with 5 decimals
|
||||
LA R3,5(R3) pgi=pgi+5
|
||||
LR R4,R6 i
|
||||
BCTR R4,0 i-1
|
||||
SRDA R4,32 .
|
||||
D R4,=F'10' (i-1)/10
|
||||
LTR R4,R4 if (i-1)//10=0
|
||||
BNZ NOSKIP then
|
||||
XPRNT PG,80 print buffer
|
||||
LA R3,PG pgi=0
|
||||
MVC PG,=CL80' ' clear buffer
|
||||
NOSKIP LA R6,1(R6) i=i+1
|
||||
B LOOPI2 end do i
|
||||
ELOOPI2 L R13,4(0,R13) epilog
|
||||
LM R14,R12,12(R13) "
|
||||
XR R15,R15 "
|
||||
BR R14 exit
|
||||
LTORG
|
||||
MORE DS F more
|
||||
PACKED DS 0D,PL8 packed decimal
|
||||
PG DC CL80' ' buffer
|
||||
XDEC DS CL12 temp
|
||||
BUF DC (NBUF)F'0' buf(nbuf)
|
||||
VECT DC (NVECT)F'2' vect(nvect) init 2
|
||||
YREGS
|
||||
NBUF EQU 201 number of 5 decimals
|
||||
NVECT EQU 3350 nvect=ceil(nbuf*50/3)
|
||||
END PISPIG
|
||||
38
Task/Pi/Clojure/pi.clj
Normal file
38
Task/Pi/Clojure/pi.clj
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
(ns pidigits
|
||||
(:gen-class))
|
||||
|
||||
(def calc-pi
|
||||
; integer division rounding downwards to -infinity
|
||||
(let [div (fn [x y] (long (Math/floor (/ x y))))
|
||||
|
||||
; Computations performed after yield clause in Python code
|
||||
update-after-yield (fn [[q r t k n l]]
|
||||
(let [nr (* 10 (- r (* n t)))
|
||||
nn (- (div (* 10 (+ (* 3 q) r)) t) (* 10 n))
|
||||
nq (* 10 q)]
|
||||
[nq nr t k nn l]))
|
||||
|
||||
; Update of else clause in Python code: if (< (- (+ (* 4 q) r) t) (* n t))
|
||||
update-else (fn [[q r t k n l]]
|
||||
(let [nr (* (+ (* 2 q) r) l)
|
||||
nn (div (+ (* q 7 k) 2 (* r l)) (* t l))
|
||||
nq (* k q)
|
||||
nt (* l t)
|
||||
nl (+ 2 l)
|
||||
nk (+ 1 k)]
|
||||
[nq nr nt nk nn nl]))
|
||||
|
||||
; Compute the lazy sequence of pi digits translating the Python code
|
||||
pi-from (fn pi-from [[q r t k n l]]
|
||||
(if (< (- (+ (* 4 q) r) t) (* n t))
|
||||
(lazy-seq (cons n (pi-from (update-after-yield [q r t k n l]))))
|
||||
(recur (update-else [q r t k n l]))))]
|
||||
|
||||
; Use Clojure big numbers to perform the math (avoid integer overflow)
|
||||
(pi-from [1N 0N 1N 1N 3N 3N])))
|
||||
|
||||
;; Indefinitely Output digits of pi, with 40 characters per line
|
||||
(doseq [[i q] (map-indexed vector calc-pi)]
|
||||
(when (= (mod i 40) 0)
|
||||
(println))
|
||||
(print q))
|
||||
15
Task/Pi/Fortran/pi-1.f
Normal file
15
Task/Pi/Fortran/pi-1.f
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
Coded by Stanley Rabinowitz, 12 Vine Brook Road, Westford MA, 01886-4212.
|
||||
INTEGER VECT(3350),BUFFER(201)
|
||||
DATA VECT/3350*2/,MORE/0/
|
||||
DO 2 N = 1,201
|
||||
KARRAY = 0
|
||||
DO 3 L = 3350,1,-1
|
||||
NUM = 100000*VECT(L) + KARRAY*L
|
||||
KARRAY = NUM/(2*L - 1)
|
||||
3 VECT(L) = NUM - KARRAY*(2*L - 1)
|
||||
K = KARRAY/100000
|
||||
BUFFER(N) = MORE + K
|
||||
2 MORE = KARRAY - K*100000
|
||||
WRITE (*,100) BUFFER
|
||||
100 FORMAT (I2,"."/(1X,10I5.5))
|
||||
END
|
||||
48
Task/Pi/Fortran/pi-2.f
Normal file
48
Task/Pi/Fortran/pi-2.f
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
!================================================
|
||||
program pi_spigot_unbounded
|
||||
!================================================
|
||||
do
|
||||
call print_next_pi_digit()
|
||||
end do
|
||||
|
||||
contains
|
||||
|
||||
!------------------------------------------------
|
||||
subroutine print_next_pi_digit()
|
||||
!------------------------------------------------
|
||||
use fmzm
|
||||
type (im) :: q, r, t, k, n, l, nr
|
||||
logical :: dot=.false., init=.false.
|
||||
save :: q, r, t, k, n, l
|
||||
if (.not.init) then
|
||||
q=to_im(1)
|
||||
r=to_im(0)
|
||||
t=to_im(1)
|
||||
k=to_im(1)
|
||||
n=to_im(3)
|
||||
l=to_im(3)
|
||||
init=.true.
|
||||
end if
|
||||
if (4*q+r-t < n*t) then
|
||||
write(6,fmt='(i1)',advance='no') to_int(n)
|
||||
if (.not.dot) then
|
||||
write(6,fmt='(a1)',advance='no') '.'
|
||||
dot=.true.
|
||||
end if
|
||||
flush(6)
|
||||
nr = 10 * ( r - n*t )
|
||||
n = 10 * ( (3*q + r) / t - n )
|
||||
q = 10 * q
|
||||
r = nr
|
||||
else
|
||||
nr = (2*q + r) * l
|
||||
n = ( (q * (7*k + 2) + r*l) / (t*l) )
|
||||
q = q * k
|
||||
t = t * l
|
||||
l = l + 2
|
||||
k = k + 1
|
||||
r = nr
|
||||
end if
|
||||
end subroutine
|
||||
|
||||
end program
|
||||
14
Task/Pi/JavaScript/pi.js
Normal file
14
Task/Pi/JavaScript/pi.js
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
var calcPi = function() {
|
||||
var n = 20000;
|
||||
var pi = 0;
|
||||
for (var i = 0; i < n; i++) {
|
||||
var temp = 4 / (i*2+1);
|
||||
if (i % 2 == 0) {
|
||||
pi += temp;
|
||||
}
|
||||
else {
|
||||
pi -= temp;
|
||||
}
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
use bigint try=>"GMP"
|
||||
use bigint try=>"GMP";
|
||||
|
||||
# Pi/4 = 4 arctan 1/5 - arctan 1/239
|
||||
# expanding it with Taylor series with what's probably the dumbest method
|
||||
|
|
|
|||
|
|
@ -1,23 +1,23 @@
|
|||
/*REXX program spits out digits of π (pi) (one at a time) until Ctrl-Break.*/
|
||||
parse arg digs . /*obtain optional argument from the CL.*/
|
||||
if digs=='' | digs=="," then digs=1e6 /*Not specified? Then use one million.*/
|
||||
fn = 'PI_DIGITS.OUT' /*fileID used for output: the π digits.*/
|
||||
numeric digits digs /*with bigger digs, spitting is slower.*/
|
||||
call time 'Reset' /*reset the wall-clock (elapsed) timer.*/
|
||||
signal on halt /*───► HALT when Ctrl─Break is pressed.*/
|
||||
pi=0; s=16; r=4; v=5; vv=v*v; g=239; gg=g*g; spit=0; old=
|
||||
|
||||
do n=1 by 2 /*calculate π with increasing accuracy */
|
||||
pi=pi + s/(n*v) - r/(n*g) /* ··· using John Machin's formula.*/
|
||||
if pi==old then leave /*have we exceeded the DIGITS accuracy?*/
|
||||
s=-s; r=-r; v=v*vv; g=g*gg /*compute some variables for shortcuts.*/
|
||||
do j=spit+1 to compare(pi,old) /*spit out some (new) digits of π (pi)*/
|
||||
parse var pi =(j) spit +1 /*equivalent to: spit=substr(pi,j,1) */
|
||||
call charout ,spit /*display one (new) decimal digit of π.*/
|
||||
call charout fn,spit /*··· and also write π digit to a file.*/
|
||||
end /*j*/ /* [↑] 0, 1, or 2 decimal dig are spit*/
|
||||
spit=j-1 /*adjust for DO loop index increment.*/
|
||||
old=pi /*use "OLD" value for the next COMPARE.*/
|
||||
end /*n*/
|
||||
say /*stick a fork in it, we're all done. */
|
||||
halt: say n%2+1 'iterations took' format(time("Elapsed"),,2) 'seconds.'
|
||||
/*REXX program spits out decimal digits of pi (one digit at a time) until Ctrl-Break.*/
|
||||
parse arg digs oFID . /*obtain optional argument from the CL.*/
|
||||
if digs=='' | digs=="," then digs=1e6 /*Not specified? Then use the default.*/
|
||||
if oFID=='' | oFID=="," then oFID='PI_SPIT.OUT' /* " " " " " " */
|
||||
numeric digits digs /*with bigger digs, spitting is slower.*/
|
||||
call time 'Reset' /*reset the wall─clock (elapsed) timer.*/
|
||||
signal on halt /*───► HALT when Ctrl─Break is pressed.*/
|
||||
pi=0; v=5; vv=v*v; g=239; gg=g*g; spit=0 /*assign some values to some variables.*/
|
||||
s=16 /*calculate π with increasing accuracy */
|
||||
r=4; do n=1 by 2 until old=pi; old=pi /*just calculate pi with odd integers*/
|
||||
pi=pi + s/(n*v) - r/(n*g) /* ··· using John Machin's formula.*/
|
||||
if pi==old then leave /*have we exceeded the DIGITS accuracy?*/
|
||||
s=-s; r=-r; v=v*vv; g=g*gg /*compute some variables for shortcuts.*/
|
||||
do j=spit+1 to compare(pi,old) /*spit out some (new) digits of π (pi)*/
|
||||
parse var pi =(j) spit +1 /*equivalent to: spit=substr(pi,j,1) */
|
||||
call charout ,spit /*display one (new) decimal digit of π.*/
|
||||
call charout oFID,spit /*··· and also write π digit to a file.*/
|
||||
end /*j*/ /* [↑] 0, 1, or 2 decimal dig are spit*/
|
||||
spit=j-1 /*adjust for DO loop index increment.*/
|
||||
end /*n*/
|
||||
say /*stick a fork in it, we're all done. */
|
||||
exit: say; say n%2+1 'iterations took' format(time("Elapsed"),,2) 'seconds.'; exit
|
||||
halt: say; say 'PI_SPIT halted via use of Ctrl-Break.'; signal exit /*show iterations.*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue