Note that there are some explanatory texts on larger screens.

plurals
  1. POsomething with scopes
    primarykey
    data
    text
    <p>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. </p> <pre><code>/*a program to find out 25th magical number. magical number means it has no prime factor except 2,3,5*/ #include&lt;stdio.h&gt; #include&lt;math.h&gt; 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]&lt;sqrt(f)+1 &amp;&amp;count&lt;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&lt;serial;c++){ if(fmod(magic_number,primes[c])==0){ loop_breaker=1; break; } } if(loop_breaker==0){ while(prime_divisor&lt;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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload