-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem026.cpp
46 lines (42 loc) · 933 Bytes
/
problem026.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
#include "lib/mathutils.h"
#include <iostream>
int multiplicativeOrder(int base, int n)
{
if (n == 1 || gcd(base, n) != 1) {
return 0;
}
int order = 1;
int powMod = base;
while (powMod != 1) {
powMod = (powMod * base) % n;
++order;
}
return order;
}
int longestReciprocalCycle(int upper)
{
// See http://mathforum.org/library/drmath/view/67018.html.
int longestPeriod = 0;
int result = 1;
int i = upper;
while (i > longestPeriod) {
int x = i;
int y = gcd(x, 10);
while (y != 1) {
x /= y;
y = gcd(x, 10);
}
int period = multiplicativeOrder(10, x);
if (period > longestPeriod) {
longestPeriod = period;
result = i;
}
--i;
}
return result;
}
int main()
{
std::cout << "Answer: " << longestReciprocalCycle(1000) << '\n';
return 0;
}