-
Notifications
You must be signed in to change notification settings - Fork 0
/
148.rb
99 lines (68 loc) · 1.57 KB
/
148.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
=begin
We can easily verify that none of the entries in the first seven rows of Pascal's triangle are divisible by 7:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
However, if we check the first one hundred rows, we will find that only 2361 of the 5050 entries are not divisible by 7.
Find the number of entries which are not divisible by 7 in the first one billion (109) rows of Pascal's triangle.
=end
def fact(n)
return n == 0 ? 1 : n * fact(n-1)
end
def nCk(n,k)
fact(n) / (fact(k) * fact(n-k))
end
=begin
0.upto(100) do |n|
print "#{n}:\t "
count = 0
0.upto(n) do |k|
if nCk(n,k) % 7 == 0
#print " X "
count += 1
else
#print " - "
end
#print " #{nCk(n,k)} "
end
print "c = #{count}"
puts
end
exit
=end
=begin
From Lucas' Theorem
http://www.math.hmc.edu/funfacts/ffiles/30002.4-5.shtml
Fun Corollary: If p is prime and N has base p representation (aj,...,a1,a0),
then there are exactly (1+aj)...(1+a1)(1+a0) values of k for which (N CHOOSE k) is NOT a multiple of p. This is the number of non-multiples of p in the N-th row of
=end
def alt(n)
return @vp_cache[n] if @vp_cache[n]
tmp = n
result = 1
while (tmp != 0)
result *= ((tmp % 7) + 1)
tmp = tmp / 7
end
@vp_cache[n] = result
end
count = 0
0.upto(10**9) do |n|
count += alt(n)
end
puts count
exit
1.upto(100) do |n|
0.upto(n) do |k|
end
end
=begin
Primes.setup(100)
Primes.primes.each do |p|
puts "#{p}: #{vp(5, p)}"
end
=end