Guest User

Untitled

a guest
Aug 31st, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. // returns the exponent of the largest power of
  5. // p that is <= n
  6. int find_last_power(int n, int p){
  7.     return (int) std::pow(n, (double) 1/p);
  8. }
  9.  
  10. void sieve(bool *primeList, int n){
  11.     primeList[0] = false; primeList[1] = false;
  12.     for (int p = 2; p<=(int)std::sqrt(n); p++){
  13.         if (primeList[p]){
  14.             for (int i = p*p; i<n; i+=p){
  15.                 primeList[i] = false;
  16.             }
  17.         }
  18.     }
  19. }
  20.  
  21. // returns the smallest number evenly divisible by
  22. // integers (1,n]
  23. int solve(int n){
  24.     bool primes[n+1]; for (int i=0; i<n+1; i++) primes[i] = true;
  25.     sieve(primes, n+1);
  26.     long long solution = 1;
  27.  
  28.     for (int i=2; i<=n; i++){
  29.         if (primes[i]){
  30.             std::cout << "p" << i << " : " << std::pow(i, find_last_power(n, i)) << std::endl;
  31.             solution *= std::pow(i, find_last_power(n, i));
  32.         }
  33.     }
  34.     return solution;
  35. }
  36. int main(){
  37.     std::cout << solve(20);
  38.     return 0;
  39. }
Add Comment
Please, Sign In to add comment