Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- // returns the exponent of the largest power of
- // p that is <= n
- int find_last_power(int n, int p){
- return (int) std::pow(n, (double) 1/p);
- }
- void sieve(bool *primeList, int n){
- primeList[0] = false; primeList[1] = false;
- for (int p = 2; p<=(int)std::sqrt(n); p++){
- if (primeList[p]){
- for (int i = p*p; i<n; i+=p){
- primeList[i] = false;
- }
- }
- }
- }
- // returns the smallest number evenly divisible by
- // integers (1,n]
- int solve(int n){
- bool primes[n+1]; for (int i=0; i<n+1; i++) primes[i] = true;
- sieve(primes, n+1);
- long long solution = 1;
- for (int i=2; i<=n; i++){
- if (primes[i]){
- std::cout << "p" << i << " : " << std::pow(i, find_last_power(n, i)) << std::endl;
- solution *= std::pow(i, find_last_power(n, i));
- }
- }
- return solution;
- }
- int main(){
- std::cout << solve(20);
- return 0;
- }
Add Comment
Please, Sign In to add comment