可以理解c程序中关于判断数字是否为素数的方法 [英] can understand a method in a c program about judging whether a number is prime

查看:99
本文介绍了可以理解c程序中关于判断数字是否为素数的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi guys





这是交流计划,我需要你的帮助!





查找100到200之间的所有素数:



程序如下:

hi guys


Here is a c program and I need your help!


For finding all prime Numbers between 100 to 200:

Procedure is as follows:

#include<stdio.h>
#include<math.h>
void main()
{
  int m,k,i,n=0;
  for(m=101;m<=200;m=m+2)
   {
     k=sqrt(m);
     for(i=2;i<=k;i++)
        if(m%i==0)  break;
     if(i>=k+1)
        {
          printf("%d",m);
          n=n+1;
        }
     if(n%10==0)
        printf("\n");
    }
  printf("\n");
}



我的问题是:程序第6行,为什么m = m + 2,而不是m ++?为什么不一个一个地判断m的值?





我希望你能帮助我!





谢谢!


my question is:Program line 6, why m = m + 2, rather than m++? Why not judgment the value of m one by one?


I hope you coule help me !


Thanks!

推荐答案

这是一个数学问题,而不是C编程问题。



m 增加2,因为除了数字2之外,每个素数都是奇数。例如,在下面的素数序列处获取战利品(注意1不被视为素数):



2,3,5,7,11 ...... 。



你可以清楚地看到所有素数都是奇数期望2,这是偶数。



那么如果你将 m 增加1会有什么好处呢? 答案为无。
This is a mathematical question rather a C programming question.

m is incremented by 2 because, every prime number is an odd number except number 2. For example take a loot at the prime numbers sequence below (Note that 1 is not considered prime):

2, 3, 5, 7, 11, ....

You can clearly see that all the prime numbers are odd expect 2, which is even.

So what advantage is there if you increment m by 1 ? Answer is None.


请参阅: http://en.wikipedia.org/wiki/Prime_number [ ^ ]



它声明:

See this:http://en.wikipedia.org/wiki/Prime_number[^]

It states that:
引用:

素数是一个大于1的自然数,除了1和它本身之外没有正除数。大于1的自然数不是素数,称为复合数。例如,5是素数,因为只有1和5均匀分割它,而6是复合因为它除了1和6还有除数2和3

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime number is called a composite number. For example, 5 is prime because only 1 and 5 evenly divide it, whereas 6 is composite because it has the divisors 2 and 3 in addition to 1 and 6



注意: 唯一的素数是2.



问候..


Note: The only even prime number is 2.

Regards..


这篇关于可以理解c程序中关于判断数字是否为素数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆