-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpi.rb
More file actions
50 lines (39 loc) · 893 Bytes
/
Copy pathpi.rb
File metadata and controls
50 lines (39 loc) · 893 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require "bigdecimal"
#only accura up to a few digits.
def pi
three = BigDecimal.new("3")
big_num = BigDecimal.new("640320")
n = (0..200).reduce(0) do |num, k|
num += numerator(k) / denominator(k)
end
n *= 12
n /= (big_num ** (three / 2))
n = n / n / n
end
def numerator(num)
new = BigDecimal.new("1")
new *= (1..(6 * num)).reduce(:*) if num != 0
new *= 13591409 + (545140134 * num)
end
def denominator(num)
div = BigDecimal.new("1")
if num != 0
div *= (1..(3 * num)).reduce(:*)
div *= ((1..num).reduce(:*) ** 3)
end
div *= (-640320) ** (3 * num)
end
def other_pi
pi = 3
four = BigDecimal("4")
(10**9).times do |n|
n *= 4
pi += four / ((2 + n) * (3 + n) * (4 + n))
pi -= four / ((4 + n) * (5 + n) * (6 + n))
end
pi
end
time = Time.now
puts moo = other_pi
puts moo.to_s.length
puts "#{(Time.now - time).round(1)} seconds"