0

和 17 一样,是质数,反过来,71 也是质数。

我们设法得到了这个代码,但我们无法完成它。

#include <stdio.h>
main()
{
    int i = 10, j, c, sum, b, x, d, e, z, f, g;

    printf("\nPrime numbers from 10 to 99 are the follwing:\n");

    while (i <= 99)
    {
        c=0;

        for (j = 1; j <= i; j++)
        {
            if (i % j == 0) c++;
        }

        if (c == 2)
        {
            b = i;
            d = b / 10;
            e = b - (10 * d);
            x = (e * 10) + d;

            {
                z = 0;
                f = x;

                for (j = 1; j <= f; j++)
                {
                    if (f % j == 0) z++;
                }

                if (z == 2)
                {
                    printf("%d %d\n", i, f);
                }
            }
        }

        i++;
    }

    getch();
}

我的问题是如何添加所有的f s ..

答案应该是 429。

我怎样才能添加所有的f?

4

4 回答 4

7

你为什么不把代码分解成一些函数:

  • bool isPrime(int number)检查一个数字是否为素数。
  • int reverse(int number)反转数字。

那么算法将是:

sum = 0;
for (i = 2; i <= 99; ++i)
   if (isPrime(i) && isPrime(reverse(i)))
      sum += i;
于 2009-02-25T10:32:38.280 回答
1

一开始初始化sum = 0;. 然后,在您的素数旁边printfsum += i;。然后你可以在最后打印它。

于 2009-02-25T10:53:06.700 回答
1

If this is a basic programming class and you are just interested in the result then this will get you there, however if it is an algorithms class then you may want to look at the Sieve of Eratosthenes.

You may also want to think about what makes a 2 digit number the reverse of another 2 digit number and how you would express that.

于 2009-02-25T11:12:19.527 回答
1

There are many problems in your code. None of them will prevent compilation and none of them will cause problems in getting the output. I'll first tell you how to get the result you want and then highlight the problems.

Here is your code, modified to sum fs. You just need to add f to sum every time you print a prime satisfying the condition. Finally, you should print the sum of all fs.

#include <stdio.h>

//Use int as the return type explicitly!
int main()
{
         int i=10,j,c,sum,b,x,d,e,z,f,g;
         printf("\nPrime numbers from 10 to 99 are the follwing:\n");
         //Set the sum of all primes whose reverse are also primes to zero
         sum = 0;
         while(i<=99)
         {
            //c is tyhe number of factors.
            c=0;
            for(j=1;j<=i;j++)
            {
                if(i%j==0)
                    c++;
            }
            //If there are only two factors.
            //Two factors (1 and itself) => Prime
            if(c==2) 
            {
                //Reverse i and store it in x
                b=i;
                d=b/10; 
                e=b-(10*d);
                x=(e*10)+d;
                //Curly braces unnecessary
                {
                    //Check if the reverse i.e. x is prime

                    //z is the number of factors
                    z=0;
                    //f is the number being tested for primality.
                    f=x;
                    for(j=1;j<=f;j++)
                    {
                        if(f%j==0)
                            z++;
                    }
                    //If f i.e. x i.e. the reverse has only two factors
                    //Two factors (1 and itself) => Prime
                    if(z==2)
                    {
                        //print the prime number.
                        printf("%d %d  \n",i,f); 
                        //Add f to the sum
                        sum += f;
                    }//if(z==2)                                                         
                }//Unnecessary braces               
            }//if(c==2)
            i++;
        }//end while          

        //print the number of reversed primes!!
        //That is sum of the reversed values of numbers satisfying the 
        //condition!
        printf("\nThe sum is:%d\n", sum); 

        //getch() is non standard and needs conio.h
        //Use getchar() instead.
        //Better solution needed!!
        getchar();

        //Return 0 - Success
        return 0;
} 

Output

...@...-desktop:~$ gcc -o temp temp.c  
...@...-desktop:~$ ./temp

Prime numbers from 10 to 99 are the follwing:
11 11  
13 31  
17 71  
31 13  
37 73  
71 17  
73 37  
79 97  
97 79  

The sum is:429

...@...desktop:~$

Do take note of all the comments made in the code (above). In addition, consider doing the following:

  1. Removing the unnecessary braces.
  2. Using one variable for one thing. (x could have been used instead of f).
  3. Using better variable names like number and numberOfFactors.
  4. Breaking up your code into functions as Mehrdad Afshari has suggested.
  5. Consider testing primality by checking if there is a divisor of the number (num) being tested up to sqrt(num) (Square root of the number).
  6. Consider this:
    • For numbers upto 99, the reversed numbers are also 2 digit numbers.
    • If the number is in the set of primes already found, you can verify easily.
    • This will reduce the number of checks for primality. (which are expensive)
    • To do the above, maintain a list of primes that have been identified (primeList) as well as a list of reversed primes (revList). Any item in revList that is also in primeList satisfies your condition. You can then easily obtain the sum (429) that you need.
  7. Look at sweet61's answer, the use of the Sieve of Eratosthenes with my method will definitely be much more efficient. You can reverse primes at the end of the sieve and populate the revList (at the end).

On a personal level, I try to find the best solution. I hope you will also attempt to do the same. I have tried to help you out without giving it all away.

I hope this helps.

Note
I had suggested checking for divisors up to num/2. I fixed it to sqrt(num) on vartec's suggestion.

于 2009-02-25T11:13:03.407 回答