-
Notifications
You must be signed in to change notification settings - Fork 0
/
eulerproblems.cpp
162 lines (151 loc) · 4.01 KB
/
eulerproblems.cpp
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
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
void projecteuler1();
void projecteuler2();
void projecteuler3();
void projecteuler4();
void projecteuler5();
void projecteuler6();
int main() {
projecteuler6();
return 0;
}
/*
projecteuler.net 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.
*/
void projecteuler1(){
int sum = 0;
int num = 0;
for (int i = 0; i < 1000; i++) {
if (i % 3 == 0 || i% 5 == 0) {
num = i;
sum += num;
}
}
cout << "The sum of all the multiples of 3 or 5 below 1000 is " << sum << endl;
}
/*
projecteuler.net 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.
*/
void projecteuler2() {
//generate the fibonnacci sequence
unsigned int sum = 0;
int storage; //to store the previous number temporarily
int prevnum = 0;
int fibonacci = 1;
bool fourmil = false;
while (fourmil == false) {
if (fibonacci < 4000000) {
storage = fibonacci;
fibonacci += prevnum;
prevnum = storage;
if (fibonacci % 2 == 0) {
sum += fibonacci;
}
}
else {
fourmil = true;
}
}
cout << "The sum of all even terms in the Fibonacci sequence whose values do not exceed four million are: " << sum << endl;
}
/*
projecteuler.net Problem 3
What is the largest prime factor of the number 600,851,475,143 ?
*/
void projecteuler3() {
const long long number = 600851475143;
long long tempnum = number;
long long largestprime = 0;
int counter = 2;
while (counter * counter <= tempnum) {
if (tempnum % counter == 0) {
tempnum = tempnum / counter;
largestprime = counter;
}
else {
counter = (counter == 2) ? 3 : counter + 2;
}
}
if (tempnum > largestprime) { // the remainder is a prime number
largestprime = tempnum;
}
cout << "The largest prime factor of 600,851,475,143 is " << largestprime << endl;
}
/*
projecteuler.net 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.
*/
void projecteuler4() {
int num, first, second, backup;
int inv = 0;
int palindromei = 0;
for (first = 999; first > 100; first--) {
for (second = 999; second > 100; second--) {
num = first * second;
backup = num;
do {
int digit;
digit = num % 10;
inv = (inv * 10) + digit;
num = num / 10;
} while (num != 0);
if (backup == inv) {
if (backup > palindromei) {
palindromei = backup;
}
}
inv = 0;
}
}
cout << "The largest palindrome from two 3 digit integers is: " << palindromei << endl;
}
/*
projecteuler.net 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?
*/
void projecteuler5() {
bool small = true;
int number = 0;
while (small==true)
{
number++;
for (int i = 1; i <= 20; i++) {
if (number%i == 0) {
if (i == 20) {
small = false;
}
}
else {
i = 21;
}
}
}
cout << "The smallest number that can be divided by the numbers 1-20 is: " << number << endl;
}
/*
projecteuler.net Problem 6
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
*/
void projecteuler6() {
int sumofsquare = 0;
int square;
int squareofsum = 0;
for (int i = 1; i <= 100; i++) {
square = i*i;
sumofsquare += square;
squareofsum += i;
}
squareofsum = squareofsum*squareofsum;
square = squareofsum - sumofsquare;
cout << "The difference between the sum of the squares and the square of the sum is: " << square << endl;
}