-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem41.rb
More file actions
46 lines (40 loc) · 924 Bytes
/
problem41.rb
File metadata and controls
46 lines (40 loc) · 924 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
#!/usr/bin/ruby
require 'euler_helper.rb'
def digits n
q = []
each_digit(n) {|d| q.unshift(d) }
q
end
def primes_under x
primes = [2]
i = 3
while i < x
sq = Math.sqrt(i).round
primes.push(i) unless primes.detect {|divisor| (divisor <= sq) ? (i % divisor == 0) : break}
i += 2
end
primes
end
def largest_pd_prime under
primes = nil
benchmark do
primes = primes_under(under)#.inject({}) {|acc, i| acc[i] = 1; acc}
end
benchmark do
primes = primes[primes.index(primes.detect{|p| p > (under / 10) })..(primes.size - 1)]
end
benchmark do
primes = primes.select {|p| (d = digits(p)) && d.uniq.size == d.size }
end
puts primes.last
end
#benchmark do
# largest_pd_prime 1000000
#largest_pd_prime 1000000000
#end
pr = nil
benchmark do
candidates = (1..7).to_a.permutation(7).to_a.map{|p| p.join.to_i}
pr = candidates.select {|n| prime?(n) }
end
puts pr.sort.last