Time for an 2014 update…

This commit is contained in:
Ingy döt Net 2014-01-17 05:32:22 +00:00
parent 372c577f83
commit 09687c4926
2520 changed files with 34227 additions and 7318 deletions

View file

@ -2,3 +2,5 @@ Create a program to continually calculate and output the next digit of <math>\pi
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]]

12
Task/Pi/Julia/pi.julia Normal file
View file

@ -0,0 +1,12 @@
prec = get_bigfloat_precision()
spi = ""
digit = 1
while true
if digit > length(spi) - 6
prec *= 2
set_bigfloat_precision(prec)
spi = string(big(π))
end
print(spi[digit])
digit += 1
end

41
Task/Pi/Lua/pi.lua Normal file
View file

@ -0,0 +1,41 @@
a = {}
n = 1000
len = math.modf( 10 * n / 3 )
for j = 1, len do
a[j] = 2
end
nines = 0
predigit = 0
for j = 1, n do
q = 0
for i = len, 1, -1 do
x = 10 * a[i] + q * i
a[i] = math.fmod( x, 2 * i - 1 )
q = math.modf( x / ( 2 * i - 1 ) )
end
a[1] = math.fmod( q, 10 )
q = math.modf( q / 10 )
if q == 9 then
nines = nines + 1
else
if q == 10 then
io.write( predigit + 1 )
for k = 1, nines do
io.write(0)
end
predigit = 0
nines = 0
else
io.write( predigit )
predigit = q
if nines ~= 0 then
for k = 1, nines do
io.write( 9 )
end
nines = 0
end
end
end
end
print( predigit )

View file

@ -1,24 +1,22 @@
/*REXX program to spit out pi digits (few at a time) until Ctrl-Break. */
arg digs .; if digs=='' then digs=1e6 /*allow the specification of digs*/
fn='PI_DIGITS.OUT' /*file that can be written to. */
/*REXX program spits out digits of pi (one at a time) until Ctrl-Break.*/
arg digs .; if digs=='' then digs=1e6 /*allow the specification of digs*/
fn = 'PI_DIGITS.OUT' /*file used for output: PI digits*/
numeric digits digs /*big digs, the slower the spits.*/
pi=0; s=16; r=4; v=5; vs=v*v; g=239; gs=g*g; old=; spewed=0; j=1
call time 'E'
/*─────────────────────────────────────John Machin's formula for pi. */
do n=1 by 2
pi=pi + s/(n*v) - r/(n*g)
if pi==old then leave /*no further with current DIGITS.*/
s=-s; r=-r; v=v*vs; g=g*gs
if n\==1 then do j=spewed+1 to compare(pi,old)
spit=substr(pi,j,1)
call charout ,spit /*spit out 1 digit of pi.*/
call charout fn,spit /* ...and also to a file.*/
end
spewed=j-1
old=pi
pi=0; s=16; r=4; v=5; vs=v*v; g=239; gg=g*g; j=1; spit=0; old=
call time 'Reset' /*reset the REXX wall-clock timer*/
/*───calculate PI with increasing*/
do n=1 by 2 /*───accuracy (up to DIGS digits)*/
pi=pi + s/(n*v) - r/(n*g) /*───using John Machin's formula.*/
if pi==old then leave /*have exceeded DIGITS accuracy. */
s=-s; r=-r; v=v*vs; g=g*gg /*set some variable for shortcuts*/
if n\==1 then do j=spit+1 to compare(pi,old) /*spit out some π digs.*/
spit=substr(pi,j,1) /*obtain a digit of π to spit out*/
call charout ,spit /*spit out one (new) digit of pi.*/
call charout fn,spit /* ···and also echo it to a file.*/
end /*j*/
spit=j-1 /*adjust for DO index increment.*/
old=pi /*use the "OLD" value next time. */
end /*n*/
say; say n%2+1 'iterations took' format(time("E"),,2) 'seconds.'
say; say n%2+1 'iterations took' format(time("Elapsed"),,2) 'seconds.'
/*stick a fork in it, we're done.*/

28
Task/Pi/Ruby/pi.rb Normal file
View file

@ -0,0 +1,28 @@
def pi
q, r, t, k, n, l = 1, 0, 1, 1, 3, 3
dot = nil
loop do
if 4*q+r-t < n*t
yield n
if dot.nil?
yield '.'
dot = '.'
end
nr = 10*(r-n*t)
n = ((10*(3*q+r)) / t) - 10*n
q *= 10
r = nr
else
nr = (2*q+r) * l
nn = (q*(7*k+2)+r*l) / (t*l)
q *= k
t *= l
l += 2
k += 1
n = nn
r = nr
end
end
end
pi {|digit| print digit; $stdout.flush}