Help with Prime Factorization Program
Go to solution
Solved by Ciccioo,
you missed a multiplication here, hence the segmentation faults [another edit: nevermind, i didn't see how it was handled later]
double *primes = malloc(sizeof(double)); /* Primes up to sqrt of const */
also, here
for (n = 0; n <= sqrtConstant; n++) { /* Convert sieve to array of primes */ if ((sieve[n] = TRUE)) { /* Segfault */ primes[i] = n; temp = realloc(primes, ((i++)+2) * sizeof(double)); if (temp !=NULL) { primes = temp; } else { free(primes); printf("Error allocating memory!\n"); return 1; } free(sieve); free(temp); } }
you free sieve while you're actually still using it
it should be freed at the end, with primes, i believe
edit:
regarding the program itself, it's a good program to find all the prime factors of a given number, but you just need the bigger factor
wouldn't it be enough to just iterate through all the numbers from sqrtConstant down and stop when you find a factor?
edit again:
long sqrtConstant = floor(sqrt(CONSTANT));
try to print out sizeof(long), i think it's usually the same as int
for working with big numbers, you want a long long, that's the biggest integer type provided by the standard
actually, unsigned long long would be even better
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now