Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8352573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:59:54+00:00 2026-06-09T08:59:54+00:00

i think there is some problem with global scope and local scopes in this

  • 0

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;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T08:59:55+00:00Added an answer on June 9, 2026 at 8:59 am

    I guess the problem is about next_prime function,
    maybe this one works better:

    double next_prime()
    {
        double f = primes[serial-1] + 2;
        int count;
        for(count=1; primes[count]<=sqrt(f) && count<serial; ++count)
        {
            if(fmod(f, primes[count])==0)
            {
                f += 2;
                count = 1;
            }
        }
        return primes[serial++] = f;
    }
    

    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:

    int main()
    {
        double prime_divisor=next_prime(),magic_serial=12,magic_number=16;
        int c, loop_breaker;
        while(magic_serial!=25)
        {
            ++magic_number;
            loop_breaker = 1;
            while(prime_divisor<magic_number/2)
                prime_divisor = next_prime();
            for(c=3; c<serial; ++c)
            {
                if(fmod(magic_number, primes[c])==0)
                {
                    loop_breaker = 0;
                    break;
                }
            }
            if(loop_breaker)
                ++magic_serial;
        }
        printf("%lf\n",magic_number);
        return 0;
    }
    

    I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit I think there is some confusion, I am not using both of the
I think there is a simple answer to this, but for some reason I
I have inherited some code that is not working as I think it should:
I face a recurring problem in the code I write: modifying some global value
After messing about with F# there are some really nice features that I think
I think there is a problem when I echo $whereArray and orderByArray. If I
I think there should be a tool to do so ? is anyone here
hey there, assuming I have a problem where each thread calculates something (reading some
I apologise for the length, and I know there are some answers on this
I think there's a gap in my mental model of WCF authentication, hoping someone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.