Problem 51

Problem 51

By replacing the 1st digit of *57, it turns out that six of the possible values: 157, 257, 457, 557, 757, and 857, are all prime.

By replacing the 3rd and 4th digits of 56**3 with the same digit, this 5-digit number is the first example having seven primes, yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. Consequently 56003, being the first member of this family, is the smallest prime with this property.

Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family.

Rozwiazanie:

#!/usr/bin/env ruby N = 1000000 t = Array.new(N+1, true) 2.upto(N) do |i| (2*i).step(N, i) { |j| t[j] = false } end t[0..1] = [false, false] x = 0 2.upto(N) do |i| if t[i] d = ("0".."9").select do |j| len = i.to_s.split("").select { |k| k == j }.length len > 1 && len < i.to_s.length end b = d.any? do |j| ("0".."9").select do |k| l = i.to_s.gsub(j, k).to_i l.to_s.length == i.to_s.length && t[l] end.length == 8 end if b x = i break end end end p x