double powll(double x, long long int n) { if(x==0.0) { return 0.0; } int neg = (x>0)? 1:-1; if(n==0) { return 1.0; } if(n<0) { return 1.0/powll(x,-n); } if(n%2==1) { double t = powll(x, n/2); return t*t*x; } else { double t = powll(x, n/2); return t*t; } }
int primelister(unsigned int primes) { // Init //unsigned long *primeArr = calloc(primes, sizeof(unsigned long)); primeArr[0] = 2; vec **arr = calloc(primes, sizeof(vec *)); if (NULL == arr) {printf("Memory error!"); exit(0);} unsigned int pos = 1; arr[0] = vec_init(2llu); unsigned long long prevnum = 0llu, num = 1llu; //printf("%u, %u \n", pos, primes); printf("2, "); // Algorithm while (pos < primes) { prevnum = num; num = 1llu; for (int p = 0; arr[p] != NULL; ++p) { unsigned int exp = vec_getNextExp(arr[p]); num *= powll(arr[p]->prime, exp); //printf(" ..:: %llu^%u, thus num = %llu.\n", arr[p]->prime, exp, num); } if (1 == num) { // We found a new prime with value prevnum+1 unsigned long long newPrime = prevnum + 1; //printf("this is what we have, prevnum:%llu . num:%llu . newPrime:%llu and %llu\n", prevnum, num, prevnum+1, newPrime); num = newPrime; arr[pos] = vec_init(newPrime); (void) vec_getNextExp(arr[pos]); ++pos; // Print new prime printf("%llu, ", newPrime); } //printf("prevnum: %llu, num; %llu - - \n", prevnum, num); } puts(""); // Fin for (unsigned long i = 0; i < primes; ++i) { free(arr[i]->arr); free(arr[i]); } free(arr); return 0; }
double pow(double x, int n1) { long long int n = n1; return powll(x, n); }