问题描述:

main()

{

int prime_array[2339],prime1_count=0,mul1_count=0;

int i, prime, lim_up, lim_low, n,j=0;

int mul,count=0;

int mul_count[65026]={0},number[7096];

printf("\n ENTER THE LOWER LIMIT…: ");

scanf("%d", &lim_low);

printf("\n ENTER THE UPPER LIMIT…: ");

scanf("%d", &lim_up);

for(n=lim_low+1; n<lim_up; n++)

{

prime = 1;

for(i=2; i<n; i++)

if(n%i == 0)

{

prime = 0;

break;

}

if(prime)

{

prime_array[j]=n;

j++;

}

}

for(i=1;i<=255;i++)

{

for(j=1;j<=255;j++)

{

mul = j*i;

mul_count[mul]++;

}

}

for(i=1;i<=65025;i++)

if( mul_count[i]!=2 && mul_count[i]!=0 )

{

number[count]=i;

count++;

}

for(prime1_count=0;prime1_count<2339;prime1_count++)

{

printf("\nprime number used is:%d",prime_array[prime1_count]);

for(mul1_count=0;mul1_count<7096;mul1_count++)

{

printf("\n%d\t",number[mul1_count] % prime_array[prime1_count]);

}

}

}

I want to find the modulus of (number[mul1_count] % prime_array[prime1_count] ), but the output which I get is wrong. What is the mistake here. The prime number should be in the range 40000 to 65025. What changes should i make here?

网友答案:

I don't really know what you are trying to do but when running your program I am getting a Floating Point Exception and thats because of the number[mul1_count] % prime_array[prime1_count] when prime_array[prime1_count] is 0

Wrap your inner for loop with an if(prime_array[prime1_count] != 0)

for(prime1_count=0;prime1_count<2339;prime1_count++)
{
       if(prime_array[prime1_count] != 0)
       {
           printf("\nprime number used is:%d",prime_array[prime1_count]);
           for(mul1_count=0;mul1_count<7096;mul1_count++)
           {
              printf("\n%d\t",number[mul1_count] % prime_array[prime1_count]);
           }
       }
}

It would be good to also try to explain what you want to do, what you expect, what you get, etc...

Edit : Also, as a sidenote. You should slightly change the loop which calculates the prime numbers. The reason is that you do not keep track of how many prime numbers you calculated. and then you for(prime1_count=0;prime1_count<2339;prime1_count++) iterate through the whole prime_array[].

Imagine the situation where you only calculated 5 prime numbers, this means that the remaining array is left with whatever. No reason to do the extra calculation, leaving asside that the prime_array is not initialized to zero nowhere in the code which means it (afaik) contains garbage value, in the indeces where no prime number was allocated with your algorithm. This means that the if(prime_array[prime1_count] != 0) will probably fail if you think that garbage value exist there.

Either initialize the prime_array[2339] = { 0 };

OR

I would do that :

int number_of_primes=0;
for(n=lim_low+1; n<lim_up; n++)
{
    prime = 1;
    for(i=2; i<n; i++)
        if(n%i == 0)
        {
            prime = 0;
            break;
        }
    if(prime)
    {
        if(number_of_primes < 2338)
        {
            prime_array[number_of_primes]=n;
            number_of_primes++;
        }else
        {
            break;
        }
    }
}

....

for(prime1_count=0 ; prime1_count < number_of_primes+1 ; prime1_count++)
{
    printf("\nprime number used is:%d",prime_array[prime1_count]);
    for(mul1_count=0;mul1_count<7096;mul1_count++)
    {
        printf("\n%d\t",number[mul1_count] % prime_array[prime1_count]);
    }
}
相关阅读:
Top