-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProjectEuler_1to5.cs
233 lines (213 loc) · 5.78 KB
/
ProjectEuler_1to5.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// ProjectEuler_1to5.cs
// Joe Mortillaro
// 10/1/2014
using System.Collections.Generic; // This allows Lists to be used.
using System;
using System.Linq;
public class ProjectEuler_1to5
{
public static void Main()
{
var problem01 = new Problem01();
System.Console.WriteLine("Answer 01: " + problem01.Solve());
var problem02 = new Problem02();
System.Console.WriteLine("Answer 02: " + problem02.GetAnswer());
var problem03 = new Problem03();
System.Console.WriteLine("Answer 03: " + problem03.AnswerNumber);
var problem04 = new Problem04();
System.Console.WriteLine("Answer 04: " + problem04.getAnswer());
var problem05 = new Problem05();
System.Console.WriteLine("Answer 05: " + problem05.getAnswer());
}
}
public static class Tools
{
static public List<long> Factor(long number)
{
long x = 2;
long y = number;
var returnList = new List<long>();
do
{
if (y % x == 0)
{
returnList.Add(x);
y = y/x;
x = 2;
}
} while (++x <= y/2);
if (y != 1) returnList.Add(y);
return returnList;
}
static public Dictionary<long, long> GetCount(List<long> numberList)
{
var returnDict = new Dictionary<long, long>();
foreach (long x in numberList)
{
if (returnDict.ContainsKey(x) == true)
{
returnDict[x] += 1;
}
else if (returnDict.ContainsKey(x) == false) {
returnDict.Add(x, 1);
}
}
return returnDict;
}
}
// Problem 1:
// If we list all the natural numbers below 10 that are multiples of
// 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
// Find the sum of all the multiples of 3 or 5 below 1000.
// Answer: 233168
class Problem01
{
public int Solve()
{
var answerNumber = 0;
for (var x = 1; x < 1000; ++x)
{
if (x % 3 == 0 || x % 5 == 0)
{
answerNumber += x;
}
}
return answerNumber;
}
}
// Problem 2:
// Each new term in the Fibonacci sequence is generated by adding the
// previous two terms. By starting with 1 and 2, the first 10 terms
// will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
// By considering the terms in the Fibonacci sequence whose values do
// not exceed four million, find the sum of the even-valued terms.
// Answer: 4613732
class Problem02
{
int answerNumber = 0;
public int GetAnswer()
{
return answerNumber;
}
public Problem02()
{
answerNumber = 0;
var fibList = new List<int>();
var x = 0;
var y = 1;
var z = 1;
while (z < 4000000)
{
z = x + y;
x = y;
y = z;
if (z % 2 == 0) fibList.Add(z);
}
answerNumber = fibList.Sum();
}
}
// Problem 3:
// The prime factors of 13195 are 5, 7, 13 and 29.
// What is the largest prime factor of the number 600851475143 ?
// Answer: 6857
class Problem03
{
public long AnswerNumber { get; set;}
public Problem03()
{
AnswerNumber = 0;
var factorList = new List<long>();
factorList = Tools.Factor(600851475143);
long z = 1;
foreach (long x in factorList)
{
z = z * x;
if (x > AnswerNumber)
{
AnswerNumber = x;
}
}
}
}
// Problem 4:
// A palindromic number reads the same both ways. The largest
// palindrome made from the product of two 2-digit numbers is
// 9009 = 91(99).
// Find the largest palindrome made from the product of two 3-digit
// numbers.
// Answer: 906609
class Problem04
{
long answerNumber = 0;
bool test(long n)
{
var returnValue = true;
string s = n.ToString();
for (int x = 0; x <= s.Length/2; ++x)
{
if (s[x] != s[s.Length - x-1])
{
returnValue = false;
}
}
return returnValue;
}
public Problem04() {
for (int y = 999; y > 0; --y)
{
for (int x = y-1; x > 0; --x)
{
if (x*y <= this.answerNumber) break;
if (this.test(x*y) == true)
{
this.answerNumber = x*y;
}
}
if (y*(y-1) <= this.answerNumber) break;
}
}
public int getAnswer()
{
return (int)answerNumber;
}
}
// Problem 5:
// 2520 is the smallest number that can be divided by each of the
// numbers from 1 to 10 without any remainder.
// What is the smallest positive number that is evenly divisible
// by all of the numbers from 1 to 20?
// Answer: 232792560
class Problem05
{
long answerNumber = 1;
public Problem05()
{
var factorCount = new Dictionary<long, long>();
for (int x = 1; x <= 20; ++x)
{
var tempCount = Tools.GetCount(Tools.Factor(x));
foreach (long i in tempCount.Keys)
{
if (factorCount.ContainsKey(i))
{
if (factorCount[i] < tempCount[i])
{
factorCount[i] = tempCount[i];
}
}
else
{
factorCount.Add(i, tempCount[i]);
}
}
}
foreach (long i in factorCount.Keys)
{
answerNumber *= (long) Math.Pow(i, factorCount[i]);
}
}
public int getAnswer()
{
return (int) answerNumber;
}
}