Project Euler

Project Euler Problem 51

steloflute 2012. 6. 9. 00:33

Problem 51

29 August 2003

By replacing the 1st digit of *3, it turns out that six of the nine possible values: 13, 23, 43, 53, 73, and 83, 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 among the ten generated numbers, 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.


Answer:
121313

 

C#

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;

namespace Euler {
    static class Program {
        /// <summary>
        /// Returns primes under limit using sieve.
        /// </summary>
        /// <param name="limit">An integer to limit.</param>
        /// <returns>Primes under limit.</returns>
        static HashSet<int> primesUnder(int limit) {
            var p = new bool[limit];
            if (limit >= 3) p[2] = true;
            for (int i = 3; i < limit; i += 2) p[i] = true;
            for (int i = 3; i * i < limit; i += 2) {
                if (!p[i]) continue;
                for (int j = i + i; j < limit; j += i)
                    p[j] = false;
            }
            var r = new HashSet<int>();
            for (int i = 2; i < limit; i++)
                if (p[i]) r.Add(i);
            return r;
        }

        static void Main(string[] args) {
            var primes = primesUnder(10000000);
            foreach (var p in primes) {                
                var lMax = p.ToString().Length - 2;
                for (var i = 0; i <= lMax; i++) {
                    for (var i2 = i + 1; i2 <= lMax; i2++) {
                        for (var i3 = i2 + 1; i3 <= lMax; i3++) {
                            var numPrimes = 0;
                            var strTemp = new StringBuilder(p.ToString()); ;
                            var firstPrime = "";
                            for (var d = '0'; d <= '9'; d++) {
                                if (i == 0 && d == '0') continue;
                                strTemp[i] = d;
                                strTemp[i2] = d;
                                strTemp[i3] = d;                                
                                if (primes.Contains(Convert.ToInt32(strTemp.ToString()))) {
                                    if (firstPrime.ToString() == "") firstPrime = strTemp.ToString();
                                    numPrimes++;
                                    //Console.Write(strTemp + " ");
                                }
                            }
                            //Console.WriteLine();
                            if (numPrimes == 8) {
                                Console.WriteLine("{0} {1} {2} {3}", i, i2, i3, firstPrime);
                                goto End;
                            }
                        }
                    }
                }
            }
        End:
            Console.WriteLine("Press");
            Console.ReadKey();
        }
    }
}



'Project Euler' 카테고리의 다른 글

Project Euler Problem 54  (0) 2012.06.09
Project Euler Problem 52  (0) 2012.06.09
Project Euler Problem 50  (0) 2012.06.09
Project Euler Problem 49  (0) 2012.06.09
Project Euler Problem 48  (0) 2012.06.09