Skip to content
This repository has been archived by the owner on Jun 24, 2019. It is now read-only.

Commit

Permalink
Took into account Pao's comments JuliaLang/julia#693 and cleaned the …
Browse files Browse the repository at this point in the history
…code. Also: added some documentation for pidigits ; added an option to print the intermediate digits as requested by the shootout, leaving it as a non-default option because printing is so slow in Julia compared to Python...
  • Loading branch information
yledu committed Apr 11, 2012
1 parent 4d117ca commit 6e36542
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
46 changes: 33 additions & 13 deletions shootout/pidigits.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
#Assume running from julia base dir
load("timing.jl")
load("../../extras/bigint.jl")
load("bigint.jl")

function pidigits(N)
function pidigits(N::Int, printOut::Bool)
"""
See http://shootout.alioth.debian.org
Transliterated from Mario Perucci Python's program
See http://shootout.alioth.debian.org/u64q/performance.php?test=pidigits#about
Transliterated from Mario Pernici Python's program
INPUT:
- N -- a positive integer giving the number of digits of pi to be computed
- printOut -- a boolean specifying if we want intermediate printouts of digits in packets of 10
OUTPUT:
- returns the last ten digits anyway
- prints all the digits in packets of 10 iff printOut == true
"""

i = k = ns = 0
k1 = 1
n,a,d,t,u = map(BigInt,(1,0,1,0,0))

while i<N
while true
k += 1
t = lshift(n,uint(1))
n *= k
Expand All @@ -27,12 +42,14 @@ function pidigits(N)
if d > u
ns = ns*10 + t
i += 1
if mod(i,N-10) == 0
#show(ns)
#printf("\t:%d\n", i)
# if i >= N
# return ns
# end
if mod(i,10) == 0
if printOut
show(ns)
printf("\t:%d\n", i)
end
if i >= N
return ns
end
ns = 0
end
a -= d*t
Expand All @@ -42,17 +59,20 @@ function pidigits(N)
end
end
end
return ns
end

function pidigits(N::Int)
pidigits(N,false)
end


@assert pidigits(1000) == 9216420198

if length(ARGS)==1
N = int(ARGS[1])
else
N = 1000
end
#pidigits(N)
@timeit pidigits(N) "pidigits"


9 changes: 5 additions & 4 deletions shootout/timing.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
macro timeit(ex,name)
t, i = gensym(2)
quote
t = Inf
for i=1:5
t = min(t, @elapsed $ex)
$t = Inf
for $i=1:5
$t = min($t, @elapsed $ex)
end
printf("julia, %s: best of 5 took %.1f ms\n", $name, t*1000)
printf("julia, %s: best of 5 took %.1f ms\n", $name, $t*1000)
end
end

Expand Down

0 comments on commit 6e36542

Please sign in to comment.