Skip to content

Commit 299b023

Browse files
committed
problem 49
1 parent 09cc4f7 commit 299b023

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

49.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require 'set'
2+
3+
def sieve(below)
4+
total = below - 2#00000
5+
array = [].fill(0 .. total){false}
6+
p = 2
7+
loop do
8+
unless array[p - 2]
9+
2.upto(below / p) do |x|
10+
array[x * p - 2] = true
11+
end
12+
end
13+
p += 1
14+
break if p > total
15+
end
16+
result = []
17+
array.each_index{|index|result << index + 2 unless array[index]}
18+
Set.new(result)
19+
end
20+
21+
primes = sieve(9999)
22+
result = Set.new
23+
primes.select{|x| x >= 1000}.each do |x|
24+
mutations = x.to_s.each_char.to_a.permutation.to_a.map(&:join).to_set.to_a.map(&:to_i).select{|x|x>1000}.select{|x|primes.include?(x)}.sort
25+
if mutations.size > 2
26+
mutations.each_index do |y|
27+
if y > 1
28+
0.upto(y - 1) do |z|
29+
if mutations.include?(mutations[y] * 2 - mutations[z])
30+
result.add("#{mutations[z]}#{mutations[y]}#{mutations[y] * 2 - mutations[z]}")
31+
end
32+
end
33+
end
34+
35+
end
36+
end
37+
end
38+
result.each{|x|puts x}
39+

0 commit comments

Comments
 (0)