- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem005.cpp
More file actions
Latest commit
31 lines (28 loc) · 666 Bytes
/
Copy pathproblem005.cpp
File metadata and controls
31 lines (28 loc) · 666 Bytes
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
#include"lib/primesieve.h"
#include<cmath>
#include<iostream>
intlcmOfRange(int upper)
{
int result = 1;
int sqrtLimit = sqrt(upper);
PrimeSieve sieve(upper);
int primes = sieve.countPrimes();
for (int i = 1; i <= primes; ++i) {
int p = sieve.nthPrime(i);
int x = p;
if (p <= sqrtLimit) {
int product = p * p;
do {
x = product;
product *= p;
} while (product <= upper);
}
result *= x; // x = p_i^(floor(log_p_i(upper)))
}
return result;
}
intmain()
{
std::cout << "Answer: " << lcmOfRange(20) << '\n';
return0;
}