-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEulerLib.cs
75 lines (61 loc) · 2.29 KB
/
EulerLib.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
using System;
using System.Collections.Generic;
using System.Linq;
namespace Euler
{
public static partial class EulerLib
{
public static void Problem1 ()
{
/*
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.
*/
Console.WriteLine("The sum of all multiples of 3 or 5 below 1000:");
var results = new List<int>();
for (int i = 0; i < 1000; i++)
{
if ((i % 3 == 0) && (i % 5 != 0))
results.Add(i);
if ((i % 5 == 0) && (i % 3 != 0))
results.Add(i);
if ((i % 3 == 0) && (i % 5 == 0))
results.Add(i);
}
Console.WriteLine(results.Sum());
}
public static void Problem2()
{
/*
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.
*/
Console.WriteLine("The sum of the even-valued terms in the Fibonacci sequence whose values do not exceed four million:");
int[] terms = new int[3];
terms[0] = 1;
terms[1] = 2;
int termsSum = 2;
while (terms[2] <= 4000000)
{
if (terms[2] % 2 == 0)
termsSum += terms[2];
terms[2] = terms[0] + terms[1];
terms[0] = terms[1];
terms[1] = terms[2];
}
Console.WriteLine(termsSum);
}
public static void Problem3()
{
/*
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
*/
Console.WriteLine("The largest prime factor of 600851475143");
}
}
}