Created
February 28, 2019 05:57
-
-
Save iamcrypticcoder/addca349080b58dac0c4f6bdbe1fc537 to your computer and use it in GitHub Desktop.
Sigma calculation in naive approach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <math.h> | |
using namespace std; | |
#define READ(x) freopen(x, "r", stdin) | |
#define WRITE(x) freopen(x, "w", stdout) | |
#define PB push_back | |
typedef unsigned long long ULL; | |
typedef vector<int> VI; | |
#define MAX_PRIME 10000000 | |
#define MAX 10000000 | |
ULL bitFlag[MAX_PRIME / 64 + 7]; | |
VI primes; | |
uint t[MAX + 7]; | |
ULL sigma[MAX + 7]; | |
void sieveBitwise() { | |
for (int i = 3; i*i <= MAX_PRIME; i += 2) { | |
if ((bitFlag[i/64] & (1LL << (i%64))) == 0) { | |
for (ULL j = i*i; j <= MAX_PRIME; j += 2*i) { | |
bitFlag[(j/64)] |= (1L << (j%64)); | |
} | |
} | |
} | |
primes.PB(2); | |
for (ULL i = 3; i <= MAX_PRIME; i += 2) | |
if ((bitFlag[i/64] & (1L << (i%64))) == 0) primes.PB(i); | |
} | |
uint countDivisors(ULL n) { | |
if(n == 0) return 0; | |
uint ret = 1, root = (int)sqrt((double)n); | |
for(int i=0; primes[i] <= root; i++) { | |
int power = 0; | |
while(!(n % primes[i])) { n /= primes[i]; power++; } | |
if(power > 0) ret *= (power + 1); | |
} | |
if(n > 1) ret *= 2; | |
return ret; | |
} | |
ULL sumDivisors(ULL n) { | |
if(n == 0) return 0; | |
ULL ret = 1, root = (int)sqrt((double)n); | |
for(int i=0; primes[i] <= root; i++) { | |
int pi = primes[i]; | |
ULL tmp = pi; | |
while(!(n % pi)) { n /= pi; tmp *= pi; } | |
if(tmp > pi) ret *= (tmp - 1) / (pi - 1); | |
} | |
if(n > 1) ret *= (n*n - 1) / (n - 1); | |
return ret; | |
} | |
void preCal() { | |
for (int i = 1; i <= MAX; ++i) { | |
t[i] = countDivisors(i); | |
sigma[i] = sumDivisors(i); | |
} | |
} | |
int main() { | |
WRITE("output.txt"); | |
double cl = clock(); | |
sieveBitwise(); | |
preCal(); | |
for (int i = 1; i <= 30; ++i) { | |
printf("%d: %lu %llu\n", i, t[i], sigma[i]); | |
} | |
cl = clock() - cl; | |
fprintf(stderr, "Total Execution Time = %lf seconds\n", cl / CLOCKS_PER_SEC); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment