Created
November 17, 2010 07:36
-
-
Save rbranson/703107 to your computer and use it in GitHub Desktop.
Implementation of pidigits using GMP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The Computer Language Benchmarks Game | |
# http://shootout.alioth.debian.org/ | |
# transliterated from Mario Pernici's Python program | |
# contributed by Rick Branson | |
require "rubygems" | |
require "gmp" | |
def gmpz(n) | |
GMP::Z.new(n) | |
end | |
N = gmpz((ARGV[0] || 100).to_i) | |
i = k = ns = gmpz(0) | |
k1 = gmpz(1) | |
n,a,d,t,u = [1,0,1,0,0].map { |n| gmpz(n) } | |
g_zero = gmpz(0) | |
g_one = gmpz(1) | |
g_two = gmpz(2) | |
g_three = gmpz(3) | |
g_ten = gmpz(10) | |
loop do | |
k += g_one | |
t = n << g_one | |
n *= k | |
a += t | |
k1 += g_two | |
a *= k1 | |
d *= k1 | |
if a >= n | |
q = (n*g_three +a) | |
t = q.fdiv(d) | |
u = q.fmod(d) | |
u += n | |
if d > u | |
ns = ns * g_ten + t | |
i += g_one | |
if i % g_ten == g_zero | |
puts "#{ns.to_s.rjust(10, '0')}\t:#{i.to_s}" | |
ns = g_zero | |
end | |
break if i >= N | |
a -= d * t | |
a *= g_ten | |
n *= g_ten | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment