-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp133.cpp
More file actions
69 lines (61 loc) · 1.37 KB
/
Copy pathp133.cpp
File metadata and controls
69 lines (61 loc) · 1.37 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
// See p133.tex for solution
#include <iostream>
#include "euler/prime_test.hpp"
#include "euler/modular.hpp"
#include "euler.h"
BEGIN_PROBLEM(133, solve_problem_133)
PROBLEM_TITLE("Repunit nonfactors")
PROBLEM_ANSWER("453647705")
PROBLEM_DIFFICULTY(1)
PROBLEM_FUN_LEVEL(3)
PROBLEM_TIME_COMPLEXITY("M*ln(M)")
PROBLEM_SPACE_COMPLEXITY("1")
PROBLEM_KEYWORDS("repunit,prime factor")
END_PROBLEM()
// Returns the sum of prime factors p <= M such that p does not divide any
// repunit of 10^k digits.
static unsigned int sum_repunit_prime_nonfactors(unsigned int M)
{
if (verbose())
{
std::cout << "Prime divisors of R(10^k):";
}
unsigned int sum = 2+3+5;
for (unsigned int p = 7; p <= M; p = euler::next_prime(p))
{
// Factorize p-1 = (2^r * 5^s) * d = x * d
unsigned int n = p - 1, x = 1;
while (n % 2 == 0)
{
n /= 2;
x *= 2;
}
while (n % 5 == 0)
{
n /= 5;
x *= 5;
}
// Check whether 10^x == 1 (mod p).
if (!(euler::modpow(euler::mod(10, p), x, p) == 1))
{
sum += p;
}
else if (verbose())
{
std::cout << " " << p;
}
}
if (verbose())
{
std::cout << std::endl;
}
return sum;
}
static void solve_problem_133()
{
#if 0
std::cout << sum_repunit_prime_nonfactors(100) << std::endl;
#else
std::cout << sum_repunit_prime_nonfactors(100000) << std::endl;
#endif
}