i think there is some problem with global scope and local scopes in this code below. if anyone please help me, why this code is not working!!
this is a program to find 25th magical number.magical number means it has no prime factor except 2,3,5. i have started from 16 which is 12th magical number. and then checking the numbers if they are divisible with any prime number from 7. so i list the prime numbers in a global array. and check the numbers with those prime in the arrays. and if this checking is complete, the program tries to find next prime number by using the next_prime function and list them into the global array. but i am getting wrong result. it is always giving the result input+=4 which is not expected.
/*a program to find out 25th magical number. magical number
means it has no prime factor except 2,3,5*/
#include<stdio.h>
#include<math.h>
double primes[10000]={2,3,5};
int serial=3;
double next_prime(double f)//function, when called, returns the next prime number
{
int j=0,loop_breaker=0;
int count;
while(j==0){
for(count=1;primes[count]<sqrt(f)+1 &&count<serial;count++){
if(fmod(f,primes[count])==0){
f+=2;
loop_breaker=1;
break;
}
}
if(loop_breaker==0){
primes[serial]=f;
serial++;
j=1;
}
}
return f;
}
int main()
{
double f=7,prime_divisor,magic_serial=12,magic_number=16;
int c,loop_breaker,is_magic;
prime_divisor=next_prime(f);
f+=2;//this line always comes after the previous line so that everytime the value of f gets changed
while(magic_serial!=25){
is_magic=0;
loop_breaker=0;
for(c=3;c<serial;c++){
if(fmod(magic_number,primes[c])==0){
loop_breaker=1;
break;
}
}
if(loop_breaker==0){
while(prime_divisor<sqrt(magic_number)+1){
prime_divisor=next_prime(f);
f+=2;
if(fmod(magic_number,prime_divisor)==0){
is_magic=1;
break;
}
}
}
if(is_magic==0){
magic_serial++;
}
magic_number++;
}
printf("%lf",magic_number);
return 0;
}
I guess the problem is about next_prime function,
maybe this one works better:
I also thought that there is no need for function to take a value, so I removed it
also it seems that main function does not do what you expect, so I think this one works better:
I hope this helps.