Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 0.69 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Efficiently calculating the total number of divisors of integers in a range
  2. typedef unsigned long long ull;
  3. void countDivisors(){
  4.     ull PF_idx=0, PF=0, ans=1, N=0, power;
  5.     for(ull i=2; i<MAX; ++i){
  6.         if (i<SIEVE_SIZE and isPrime[i]) factors[i]=2;
  7.         else{
  8.         PF_idx=0;
  9.         PF=primes[PF_idx];
  10.         ans=1;
  11.         N=i;
  12.         while(N!=1 and (PF*PF<=N)){
  13.             power = 0;
  14.             while(N%PF==0){ N/=PF; ++power;}
  15.             ans*=(power+1);
  16.             PF = primes[++PF_idx];
  17.         }
  18.         if (N!=1) ans*=2;
  19.         factors[i] = ans;
  20.         }
  21.     }
  22. }
  23.        
  24. for (ull i = 1; i < MAX; ++i) {
  25.     for (ull j = i; j < MAX; j += i) {
  26.         factors[j] += i;
  27.     }
  28. }