-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp108.cpp
More file actions
68 lines (64 loc) · 1.47 KB
/
Copy pathp108.cpp
File metadata and controls
68 lines (64 loc) · 1.47 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
/**
* In the following equation x, y, and n are positive integers.
*
* 1/x + 1/y = 1/n
*
* For n = 4 there are exactly three distinct solutions:
*
* 1/5 + 1/20 = 1/4
* 1/6 + 1/12 = 1/4
* 1/8 + 1/8 = 1/4
*
* What is the least value of n for which the number of distinct solutions
* exceeds one-thousand?
*
* NOTE: This problem is an easier version of Problem 110.
*
* SOLUTION:
*
* We observe the following relation:
*
* (x-n)*(y-n) = n^2
*
* Hence the solutions (x,y) map one-to-one to the divisors of n^2.
*
* Let the prime factors of n be (p1,p2,...,pm), then (x,y) is a partition of
*
* (p1^2k1)*(p2^2k2)*...*(pm^2km) = n^2.
*
* The detailed solution is presented for problem 110.
*/
#include <iostream>
#include "euler/prime_factor.hpp"
#include "euler.h"
BEGIN_PROBLEM(108, solve_problem_108)
PROBLEM_TITLE("Diophantine reciprocals I")
PROBLEM_ANSWER("180180")
PROBLEM_DIFFICULTY(1)
PROBLEM_FUN_LEVEL(1)
PROBLEM_TIME_COMPLEXITY("")
PROBLEM_SPACE_COMPLEXITY("")
END_PROBLEM()
// Counts number of distinct solutions 1/x + 1/y = 1/n.
static size_t count_solutions(int n)
{
size_t count = 1;
for (const auto &pk: euler::distinct(euler::factorize(n)))
{
const size_t k = pk.second;
count *= (2 * k + 1);
}
return (count + 1) / 2;
}
static void solve_problem_108()
{
for (int n = 2; ; n++)
{
const size_t count = count_solutions(n);
if (count >= 1000)
{
std::cout << n << std::endl;
break;
}
}
}