-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem041.cs
47 lines (41 loc) · 1.12 KB
/
Problem041.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
namespace ProjectEuler.Solutions
{
using NUnit.Framework;
using ProjectEuler.Helper;
/// <summary>
/// Pandigital prime.
/// We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.
/// <para>What is the largest n-digit pandigital prime that exists?</para>
/// </summary>
public class Problem041 : Problem
{
public override long Solution()
{
long largestPandigitalPrime = 0;
int[] primes = Primes.EratosthenesSieve(7654321).ToArray();
for (int i = primes.Length - 1; i >= 0; i--)
{
long prime = primes[i];
string primeString = prime.ToString();
if (Numbers.IsPandigital(primeString, primeString.Length))
{
largestPandigitalPrime = prime;
break;
}
}
return largestPandigitalPrime;
}
[Test]
public void TestForExample()
{
const int number = 2143;
Assert.IsTrue(Primes.IsPrimeNumber(number));
Assert.IsTrue(Numbers.IsPandigital(number.ToString(), 4));
}
[Test]
public void TestForProblem()
{
Assert.AreEqual(7652413, this.Solution());
}
}
}