-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem030.cs
64 lines (54 loc) · 1.5 KB
/
Problem030.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
using System;
using System.Collections.Generic;
using System.Linq;
namespace Euler
{
class Problem030
{
public int Run()
{
var powersOf5 = new List<int>(10);
for (int i = 0; i < 10; i++)
{
if (i == 0) powersOf5.Add(0);
else powersOf5.Add((int) Math.Pow(i, 5));
}
// To find upper bound
for (int i = 0; i < 10; i++)
{
Console.WriteLine("{0} - {1}", "".PadLeft(i, '9'), powersOf5[9] * i);
}
int upperBound = 6 * powersOf5[9];
int result = 0;
int num = 2;
do
{
var digits = GetIntArray(num);
var sum = 0;
foreach (var digit in digits)
{
sum += powersOf5[digit];
if (sum > num) break;
}
if (sum == num)
{
result += sum;
Console.WriteLine("{0} - {1}", num, sum);
}
num++;
} while (num <= upperBound);
return result;
}
IEnumerable<int> GetIntArray(int num)
{
var listOfInts = new List<int>();
while (num > 0)
{
listOfInts.Add(num % 10);
num = num / 10;
}
//listOfInts.Reverse();
return listOfInts;
}
}
}