#/*********************************************************** # bernoull.rb -- Bernoulli (ベルヌーイ) 数 #***********************************************************/ def gcd( x, y ) # 最大公約数 while (y != 0); t = x % y; x = y; y = t; end return x end N = 40 DBL_EPSILON = 2.2204460492503131e-16 t = [] (N+1).times { t.push(0) } q = 1 t[1] = 1 for n in 2..N for i in 1...n; t[i - 1] = i * t[i]; end t[n - 1] = 0 n.downto(2) {|i| t[i] += t[i - 2] } if (n % 2 == 0) q *= 4 b1 = n * t[0]; b2 = q * (q - 1) if (b1 < 1 / DBL_EPSILON && b2 < 1 / DBL_EPSILON) d = gcd(b1, b2); b1 /= d; b2 /= d printf("|B(%2d)| = %.0f/%.0f\n", n, b1, b2) else printf("|B(%2d)| = %g\n", n, b1 / b2) end end end exit 0