-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler0037_truncatable_primes_redux.rb
62 lines (56 loc) · 1.39 KB
/
euler0037_truncatable_primes_redux.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
require 'pp'
def sieve_of_eratosthenes limit
set_of_numbers = Hash.new
for i in 2..limit
set_of_numbers[i] = true
end
for i in 2..Math.sqrt(limit).floor
if set_of_numbers[i] == true
j = i*i
while (j<=limit) do
set_of_numbers[j] = false
j += i
end
end
end
set_of_numbers
end
def test_trunc_left num
divisor = 10**(Math.log10(num).floor)
num_trunc = num % divisor
return true if num_trunc == 0
return false unless $primes.include? num_trunc
if $memo_nums_left[num_trunc] == nil
$memo_nums_left[num_trunc] = test_trunc_left num_trunc
end
$memo_nums_left[num_trunc]
end
def test_trunc_right num
num_trunc = (num/10).floor
return true if num_trunc == 0
return false unless $primes.include? num_trunc
if $memo_nums_right[num_trunc] == nil
$memo_nums_right[num_trunc] = test_trunc_right num_trunc
end
$memo_nums_right[num_trunc]
end
$memo_nums_left = []
$memo_nums_right = []
count = sum = 0
limit = 1000000
$primes_hash = sieve_of_eratosthenes limit
$primes = $primes_hash.select {|k,v| v}.keys
nums = []
$primes.each do |num|
next if ["0","1","4","6","8","9"].include? num.to_s.split("").first
next if ["0","1","4","6","8","9"].include? num.to_s.split("").last
if test_trunc_left(num) && test_trunc_right(num)
count += 1
sum += num
nums << num
pp num
end
end
pp count - 4
pp sum -2-3-5-7
pp nums[4..-1]