-
Notifications
You must be signed in to change notification settings - Fork 2
/
extMath.rb
56 lines (49 loc) · 905 Bytes
/
extMath.rb
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
51
52
53
54
55
56
class << Math
#log base 2 definition
def log2(n); log(n) / log(2); end
#log2 star definition
def log2star(n)
if n < 1
return 0
else
return 1+log2star(log2(n))
end
end
#factorial definition
def factorial(n)
sum = 1
1.upto(n){|i|
sum *= i
}
return sum
end
#calculates factorial in log space
def log2factorial(n)
sum = 0
1.upto(n){|i|
sum += Math.log2(i)
}
return sum
end
# uses cancelling to do huge factorials
# the efficient way
# (a choose b)
def binomCoefficient(a,b)
top = 1
0.upto(b-1){|i|
top *= (a-i)
}
bottom = Math.factorial(b)
return top/bottom
end
# same, but in log2 space
# (a choose b)
def binomCoefficientLog2(a,b)
top = 0
0.upto(b-1){|i|
top += Math.log2(a-i)
}
bottom = Math.log2factorial(b)
return top - bottom
end
end