Advertisement
Shuva_Dev

Factorial Factors (UVa)

Jan 18th, 2023
979
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int factor[1000000 + 5];
  5. int cum_sum[1000000 + 5];
  6.  
  7. bool isPrime(int n) {
  8.     for(int i = 2; i*i <= n; i++) {
  9.         if(n % i == 0) return false;
  10.     }
  11.     return true;
  12. }
  13.  
  14.  
  15. int main() {
  16.     for(int i = 2, j=1; i <= 1000000; i*=2, j++) {
  17.         factor[i] = j;
  18.     }
  19.     for(int i = 3; i <= 1000000; i++) {
  20.         if(factor[i] == 0) {
  21.             if(isPrime(i)) {
  22.                 factor[i] = 1;
  23.             } else {
  24.                 if(i%2 == 0) {
  25.                     factor[i] = factor[i/2] + 1;
  26.                 } else {
  27.                     for(int j = 3; ;j+=2) {
  28.                         if(i % j == 0) {
  29.                             factor[i] = factor[i/j] + 1;
  30.                             break;
  31.                         }
  32.                     }
  33.                 }
  34.             }
  35.         }
  36.     }
  37.     cum_sum[2] = factor[2];
  38.     for(int i=3; i<=1000000; i++) {
  39.         cum_sum[i] = factor[i] + cum_sum[i-1];
  40.     }
  41.     int n;
  42.     while (cin >> n) {
  43.         cout << cum_sum[n] << endl;
  44.     }
  45.    
  46.    
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement