-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem052.cs
80 lines (71 loc) · 1.68 KB
/
Problem052.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
namespace ProjectEuler.Solutions
{
using System;
using NUnit.Framework;
/// <summary>
/// Permuted multiples.
/// It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.
/// <para>Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.</para>
/// </summary>
public class Problem052 : Problem
{
public override long Solution()
{
long smallest;
for (int baseNumber = 1;; baseNumber++)
{
bool isPermutedMultiple = true;
string baseNumberString = baseNumber.ToString();
for (int factor = 6; factor > 1; factor--)
{
int numberToCheck = baseNumber * factor;
if (!IsPermutation(baseNumberString, numberToCheck.ToString()))
{
isPermutedMultiple = false;
break;
}
}
if (isPermutedMultiple)
{
smallest = baseNumber;
break;
}
}
return smallest;
}
private static bool IsPermutation(string baseNumber, string numberToCheck)
{
bool isPermutation = true;
if (baseNumber.Length != numberToCheck.Length)
{
isPermutation = false;
}
else
{
char[] baseArray = baseNumber.ToCharArray();
char[] checkArray = numberToCheck.ToCharArray();
Array.Sort(baseArray);
Array.Sort(checkArray);
for (int i = 0; i < baseArray.Length; i++)
{
if (baseArray[i] != checkArray[i])
{
isPermutation = false;
break;
}
}
}
return isPermutation;
}
[Test]
public void TestForIsPermutation()
{
Assert.IsTrue(IsPermutation("125874", "251748"));
}
[Test]
public void TestForProblem()
{
Assert.AreEqual(142857, this.Solution());
}
}
}