-
Notifications
You must be signed in to change notification settings - Fork 1
/
problem130.rb
executable file
·36 lines (33 loc) · 1.2 KB
/
problem130.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
# A number consisting entirely of ones is called a repunit. We shall define R(k) to be a repunit of length k; for example, R(6) = 111111.
#
# Given that n is a positive integer and GCD(n, 10) = 1, it can be shown that there always exists a value, k, for which R(k) is divisible by n, and let A(n) be the least such value of k; for example, A(7) = 6 and A(41) = 5.
#
# You are given that for all primes, p > 5, that p − 1 is divisible by A(p). For example, when p = 41, A(41) = 5, and 40 is divisible by 5.
#
# However, there are rare composite values for which this is also true; the first five examples being 91, 259, 451, 481, and 703.
#
# Find the sum of the first twenty-five composite values of n for which
# GCD(n, 10) = 1 and n − 1 is divisible by A(n).
require 'miller-rabin'
require 'common'
def a(n)
return nil if n % 2 == 0 or n % 5 == 0
count = 1
current = 1
while current != 0
count += 1
current = (current * 10 + 1) % n
end
return count
end
max = 25
found = []
current = 2
while found.length < max
puts "#{current}, #{found.length}"
temp = a(current) unless current.prime?
found << current if temp != nil and (current - 1) % temp == 0
current += 1
end
puts found.inspect
puts found.sum