I'm no programmer, yet, but I did manage to find some code to solve #60. It didn't take long at all to find and I'm sure you could easily do the same if you were really stuck, but I just wanted to let you know that it just got the answer in just under 7.5 seconds so if yours is taking a while to iterate then it might be worth thinking about a different approach :)
I'll post, but hide, the code so it doesn't ruin it for you.
Source: Free Lancers Unite » Project Euler Problem #60!
I'll post, but hide, the code so it doesn't ruin it for you.
Read More:
Code:
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;
}
}
}
Source: Free Lancers Unite » Project Euler Problem #60!