-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp141.cpp
More file actions
82 lines (76 loc) · 1.67 KB
/
Copy pathp141.cpp
File metadata and controls
82 lines (76 loc) · 1.67 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
// See p141.tex for solution
#include <cstdint>
#include <iostream>
#include "euler/imath.hpp"
#include "euler/prime_factor.hpp"
#include "euler/sequence.hpp"
#include "euler.h"
BEGIN_PROBLEM(141, solve_problem_141)
PROBLEM_TITLE("Investigating progressive numbers which are also square")
PROBLEM_ANSWER("878454337159")
PROBLEM_DIFFICULTY(2)
PROBLEM_FUN_LEVEL(2)
PROBLEM_TIME_COMPLEXITY("N^0.58")
PROBLEM_SPACE_COMPLEXITY("1")
END_PROBLEM()
// Find all progressive numbers.
template <class Func>
static void find_progressive_numbers(int64_t N, Func f)
{
// Special case: r = 1
for (int64_t q = 2; ; q++)
{
int64_t n = q*q*q+1;
if (n > N)
{
break;
}
f(n);
}
// For each r >= 2, r must divide q^2, and q must be greater than r.
for (int r = 2; static_cast<int64_t>(r) * r < N; r++)
{
int base = 1;
for (const auto &pk: euler::distinct(euler::factorize(r)))
{
const int p = pk.first;
const int k = static_cast<int>(pk.second);
for (int i = (k-1)/2; i >= 0; --i)
{
base *= p;
}
}
for (int q = (r / base + 1) * base; ; q += base)
{
int64_t p = static_cast<int64_t>(q) * q / r;
int64_t n = p * q + r;
if (n > N)
{
break;
}
f(n);
}
}
}
static int64_t sum_progressive_squares(int64_t N)
{
int64_t sum = 0;
find_progressive_numbers(N, [&sum](int64_t n)
{
int64_t root = euler::isqrt(n);
if (root * root == n)
{
sum += n;
}
});
return sum;
}
static void solve_problem_141()
{
#if 0
const int64_t N = 99999;
#else
const int64_t N = 999999999999LL;
#endif
std::cout << sum_progressive_squares(N) << std::endl;
}