-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy patheuler-0077.cpp
98 lines (88 loc) · 2.32 KB
/
euler-0077.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
// ////////////////////////////////////////////////////////
// # Title
// Prime summations
//
// # URL
// https://projecteuler.net/problem=77
// http://euler.stephan-brumme.com/77/
//
// # Problem
// It is possible to write ten as the sum of primes in exactly five different ways:
//
// 7 + 3
// 5 + 5
// 5 + 3 + 2
// 3 + 3 + 2 + 2
// 2 + 2 + 2 + 2 + 2
//
// What is the first value which can be written as the sum of primes in over five thousand different ways?
//
// # Solved by
// Stephan Brumme
// March 2017
//
// # Algorithm
// Instead of re-using the code from problem 76 (which was based on probem 31) I wrote this code from scratch
// because the solution is actually much simpler than the previous challenges.
//
// Main idea:
// ''combinations(x) = combinations(x - prime1) + combinations(x - prime2) + ...''
// I subtract each prime and look up ''combinations(x - currentPrime)'' and sum all those numbers
// ==> nice Dynamic Programming solution !
#include <iostream>
#include <vector>
int main()
{
const unsigned int MaxNumber = 1000;
// store number of ways to represent a number as a sum of primes
std::vector<unsigned long long> combinations(MaxNumber + 1, 0);
// degenerated case
combinations[0] = 1;
// store all primes
std::vector<unsigned int> primes;
for (unsigned int i = 2; i <= MaxNumber; i++)
{
bool isPrime = true;
// test against all prime numbers we have so far (in ascending order)
for (auto p : primes)
{
// next prime is too large to be a divisor ?
if (p*p > i)
break;
// divisible ? => not prime
if (i % p == 0)
{
isPrime = false;
break;
}
}
// only primes after this point ...
if (!isPrime)
continue;
primes.push_back(i);
// now add all solutions
for (unsigned int pos = 0; pos <= MaxNumber - i; pos++)
combinations[pos + i] += combinations[pos];
}
//#define ORIGINAL
#ifdef ORIGINAL
// find first number with more than 5000 combinations
for (size_t i = 0; i < combinations.size(); i++)
if (combinations[i] > 5000)
{
std::cout << i << std::endl;
break;
}
#else
unsigned int tests = 1;
std::cin >> tests;
while (tests--)
{
// look up combinations
unsigned int n;
std::cin >> n;
std::cout << combinations[n] << std::endl;
}
#endif
return 0;
}