Project Euler Problem #60!

February 22nd, 2010
by Serinox

Problem #60 says:

The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property.

Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.

At first I thought of writing a class that would generate all permutations for the possible answers. It was simpler to write a simple program that generates a list of primes and loops through it. Solution below is c#/mono and runs in 20 or so seconds.

using System;
using System.Collections;
using System.Diagnostics;

namespace ProjectEuler
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			Stopwatch sw = new Stopwatch();
			sw.Start();
			ArrayList p = GeneratePrimes(10000);

			foreach (long i1 in p)
			{
				foreach (long i2 in p)
				{
					if (i2 <= i1) continue;
					if (!IsPrime(ref p, long.Parse(i1.ToString() + i2.ToString())) ||
                                            !IsPrime(ref p, long.Parse(i2.ToString() + i1.ToString())))
					{
						continue;
					}
					foreach (long i3 in p)
					{
						if (i3 <= i2) continue;
						if (!IsPrime(ref p, long.Parse(i1.ToString() + i3.ToString())) ||
                                                    !IsPrime(ref p, long.Parse(i3.ToString() + i1.ToString())) ||
						    !IsPrime(ref p, long.Parse(i2.ToString() + i3.ToString())) ||
                                                    !IsPrime(ref p, long.Parse(i3.ToString() + i2.ToString())))
						{
							continue;
						}
						foreach(long i4 in p)
						{
							if (i4 <= i3) continue;
							if (!IsPrime(ref p, long.Parse(i4.ToString() + i3.ToString())) ||
                                                            !IsPrime(ref p, long.Parse(i3.ToString() + i4.ToString())) ||
						       	    !IsPrime(ref p, long.Parse(i2.ToString() + i4.ToString())) ||
                                                            !IsPrime(ref p, long.Parse(i4.ToString() + i2.ToString())) ||
							    !IsPrime(ref p, long.Parse(i1.ToString() + i4.ToString())) ||
                                                            !IsPrime(ref p, long.Parse(i4.ToString() + i1.ToString())))
							{
								continue;
							}
							foreach(long i5 in p)
							{
								if (i5 <= i4) continue;
								if (!IsPrime(ref p, long.Parse(i5.ToString() + i3.ToString())) ||
                                                                    !IsPrime(ref p, long.Parse(i3.ToString() + i5.ToString())) ||
							    	    !IsPrime(ref p, long.Parse(i2.ToString() + i5.ToString())) ||
                                                                    !IsPrime(ref p, long.Parse(i5.ToString() + i2.ToString())) ||
								    !IsPrime(ref p, long.Parse(i1.ToString() + i5.ToString())) ||
                                                                    !IsPrime(ref p, long.Parse(i5.ToString() + i1.ToString())) ||
								    !IsPrime(ref p, long.Parse(i4.ToString() + i5.ToString())) ||
                                                                    !IsPrime(ref p, long.Parse(i5.ToString() + i4.ToString())))
								{
									continue;
								}
								else
								{
									Console.WriteLine(i1+i2+i3+i4+i5);
									sw.Stop();
									Console.WriteLine(sw.Elapsed);
									Console.ReadLine();
								}
							}
						}
					}
				}
			}

			sw.Stop();
			Console.WriteLine(sw.Elapsed);
			Console.ReadLine();
		}
		static bool IsPrime(ref ArrayList primes, long num)
		{
			bool RetVal = true;
			long check = Convert.ToInt64(Math.Ceiling(Math.Sqrt(Convert.ToDouble(num))));
			foreach (long l in primes)
			{
				if (l > check)
					break;
				if (num % l == 0)
					RetVal = false;
			}
			return RetVal;
		}
        static ArrayList GeneratePrimes(long num)
        {
            ArrayList RetValue = new ArrayList();
            RetValue.Add((long)2);
            RetValue.Add((long)3);
            long Stepper = 5;
            long Check = 1;
            while (Stepper <= num)
            {
                foreach (long i in RetValue)
                {
                    if (Stepper % i == 0)
                    {
                        Check = 0;
                        break;
                    }
                    if (Math.Sqrt(Stepper) < i)
                    {
                        break;
                    }
                }
                if (Check == 1)
                {
                    //Console.WriteLine(((float)Stepper / (float)num) * 100);
                    RetValue.Add(Stepper);
                }
                Check = 1;
                Stepper++;
                Stepper++;
            }
            return RetValue;
        }

	}
}

Tags: , ,
Posted in Project Euler | Comments (0)

No comments yet

Leave a Reply

You must be logged in to post a comment.
This blog collects statistical data with Statpress SEOlution in the reVierphone Edition! Give it a try.