-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp132.cpp
More file actions
66 lines (58 loc) · 1.25 KB
/
Copy pathp132.cpp
File metadata and controls
66 lines (58 loc) · 1.25 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
// See p132.tex for solution
#include <iostream>
#include "euler/gcd.hpp"
#include "euler/prime_test.hpp"
#include "euler/modular.hpp"
#include "euler.h"
BEGIN_PROBLEM(132, solve_problem_132)
PROBLEM_TITLE("Prime factors of a large repunit")
PROBLEM_ANSWER("843296")
PROBLEM_DIFFICULTY(1)
PROBLEM_FUN_LEVEL(3)
PROBLEM_TIME_COMPLEXITY("P*(log(P))^2")
PROBLEM_SPACE_COMPLEXITY("1")
PROBLEM_KEYWORDS("repunit,factorization")
END_PROBLEM()
// Returns the sum of the first m prime factors of a repunit of n digits.
static unsigned int sum_repunit_prime_factors(unsigned int n, unsigned int m)
{
if (m == 0)
{
return 0;
}
unsigned int sum = 0;
if (n % 3 == 0)
{
--m;
sum += 3;
if (verbose())
{
std::cout << 3 << " ";
}
}
for (unsigned int p = 7; m > 0; p = euler::next_prime(p))
{
if (euler::modpow(euler::mod(10, p), euler::gcd(n,p-1), p) == 1)
{
sum += p;
--m;
if (verbose())
{
std::cout << p << " ";
}
}
}
if (verbose())
{
std::cout << std::endl;
}
return sum;
}
static void solve_problem_132()
{
#if 0
std::cout << sum_repunit_prime_factors(10, 4) << std::endl;
#else
std::cout << sum_repunit_prime_factors(1000000000, 40) << std::endl;
#endif
}