-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem95.h
More file actions
108 lines (100 loc) · 3.32 KB
/
problem95.h
File metadata and controls
108 lines (100 loc) · 3.32 KB
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
//
// Created by Pratim, Sonia, Karun on 5/12/22.
//
#ifndef PROJECT_EULER_PROBLEM95_H
#define PROJECT_EULER_PROBLEM95_H
#include "unordered_set"
#include "unordered_map"
#include "vector"
#include "../include/prime_lib.h"
#include "../include/print_lib.h"
using namespace std;
void generate_factor_map(unordered_map<int, unordered_set<int>>& factor_map,
unordered_map<int, int>& factor_sum_map,
unordered_set<int>& prime_set,
vector<int>& primes,
int N) {
for (int n = 2; n < N; n++) {
if (prime_set.find(n) != prime_set.end()) {
factor_map[n] = unordered_set<int> {1, n};
factor_sum_map[n] = 1;
} else {
int index = 0;
while (n % primes[index] != 0) {
index++;
}
int prime_factor = primes[index];
int last = n / prime_factor;
if (factor_sum_map[last] == -1) {
factor_sum_map[n] = -1;
continue;
}
int factor_sum = factor_sum_map[last] + last;
unordered_set<int> factor_set = factor_map[last];
for (int factor: factor_map[n / prime_factor]) {
int new_factor = factor * prime_factor;
if (factor_set.find(new_factor) == factor_set.end()) {
factor_set.insert(new_factor);
if (new_factor != n)
factor_sum += new_factor;
if (factor_sum > N) {
factor_sum = -1;
break;
}
}
}
if (factor_sum > 0) {
factor_map[n] = factor_set;
}
factor_sum_map[n] = factor_sum;
}
}
}
int find_chain(int N, unordered_map<int, int>& factor_sum_map, unordered_map<int, int>& chain_map) {
if (chain_map.find(N) != chain_map.end())
return chain_map[N];
unordered_map<int, int> index_map;
int index = 0;
int n = N;
index_map[n] = 0;
n = factor_sum_map[n];
while (index_map.find(n) == index_map.end()) {
if (n == 1 or n == -1) {
for (auto itr: index_map) {
chain_map[itr.second] = -1;
return -1;
}
}
index_map[n] = index;
n = factor_sum_map[n];
index ++;
}
int chain_length = index - index_map[n];
for (auto itr: index_map) {
if (itr.second >= index_map[n])
chain_map[n] = chain_length;
else
chain_map[n] = -1;
}
return chain_map[N];
}
void problem_95_solution(bool log) {
int N = 1000000;
vector<int> primes = sieve(N);
unordered_set<int> prime_set (primes.begin(), primes.end());
unordered_map<int, unordered_set<int>> factor_map;
unordered_map<int, int> factor_sum_map;
unordered_map<int, int> chain_map;
generate_factor_map(factor_map, factor_sum_map, prime_set, primes, N);
int longest = 0;
int smallest = 0;
for (int n = 2; n < N; n++) {
int chain_length = find_chain(n, factor_sum_map, chain_map);
if (chain_length > longest) {
longest = chain_length;
smallest = n;
}
}
if (log) cout << smallest << endl;
}
#endif //PROJECT_EULER_PROBLEM95_H