素数判断方法

转自:http://www.cnitblog.com/jackrain/archive/2005/09/25/2879.aspx

判断素数的几种方法思考
【】判断素数是经常遇到的问题,下面就总结几种方法
1、最简单的从2~sqrt(N)的方法(N>=2,下同)
2、筛选法
3、素数判断法

概念说明:
素数,又叫质数,指除了1和它本身外,没有其他因数。(如果你不知道什么叫因数,建议你去从小学2年级开始学习-_-!);
合数:自然就是除了1和它本身外有其他因数。
需指出一点,1既不是质数也不是合数。
因此,判断N是素数的简单而笨的方法就是看看N有没有因数。

1、最简单的方法:
该算法的思想就是用2~sqrt(N),依次去对N求余,只要有一个余数是0,则N是合数。举例如下:

 1 素数判断方法
 6 素数判断方法
 7 素数判断方法#include  <</span>stdio.h>
 8素数判断方法#include <</span>stdlib.h>
 9素数判断方法#include <</span>math.h>
10素数判断方法
11素数判断方法void main(int argc, char* argv[])
12素数判断方法{
13素数判断方法    int N, i, m,flag = 0;
14素数判断方法    if(argc <</span> 2)
15素数判断方法    {
16素数判断方法        while(1)
17素数判断方法        {
18素数判断方法            printf("Please input non-integer:");
19素数判断方法            if(scanf("%d"&N) != 1)
20素数判断方法            {
21素数判断方法                fflush(stdin);
22素数判断方法                continue;
23素数判断方法            }
24素数判断方法            break;
25素数判断方法        }
26素数判断方法    }
27素数判断方法    else
28素数判断方法        = atoi(argv[1]);
29素数判断方法    =(intsqrt(N*1.0);
30素数判断方法    for(i = 2;i <= m; i++)
31素数判断方法        if(N % ==0)
32素数判断方法        {
33素数判断方法            flag = 1;
34素数判断方法            break;
35素数判断方法        }
36素数判断方法    if(flag)
37素数判断方法        printf("%d is not primer\n"N);
38素数判断方法    else
39素数判断方法        printf("%d is primer\n"N);
40素数判断方法    system("pause");
41素数判断方法}
注:以上代码判断不出1、2、3来。
上 面的代码就是按照这个简单的思路来的,思路是简单了,但是方法效率是不高的。因为我们知道,一个数如果不能被2整除,那么也就不能被4、6、等所有的偶数 整除。但是我们的程序在判断了2之后依然会去判断4、6等。所以我们可以把循环规则改变成先判断2,如果不能被2整除就从3开始判断所有的奇数。即:
1  if (N  %   2   !=   0 )
2  {
3        for =   3 <=  m;  += 2 )
4        // ……此处省略了
5  }
6  else
7      //  合数

2、筛选法判断素数。
这个方法不是用来判断一个素数,而是所有素数的方法。方法的原理是:
2、筛选法判断素数。
这个方法不是用来判断一个素数,而是所有素数的方法。方法的原理是:

[quote]
http://www.math.utah.edu/classes/216/assignment-07.html
The goal of this assignment is to design a program which will produce lists of prime numbers. The method is based on the one discovered by Erastosthenes (276 - 196 BC). His method goes like this. First, write down a list of integers

  2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Then mark all multiples of 2:

  2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
                         x

Move to the next unmarked number, which in this case is 3, then mark all its multiples:

  2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
        x x              x

Continue in this fashion, marking all multiples of the next unmarked number until there are no new unmarked numbers. The numbers which survive this marking process (the Sieve of Eratosthenses) are primes. (You should complete this marking process yourself).
The new part of the C language that you will have to learn in order to do this program is the array.

[/quote]
这段E文没什么难理解的,就是首先生成数组,然后从第一个开始依次标注它的倍数,然后从下一个没有被标注的开始,标注它所有的倍数,这样依次下去,最后没有被标注的都是素数。下面是代码:

 1 
 4  #include  <</span>stdio.h>
 5 #include <</span>stdlib.h>
 6 
 7 void main(int argc, char* argv[])
 8 {
 9     int N, i, *m, = 0,temp;
10     if(argc <</span> 2)
11     {
12         while(1)
13         {
14             printf("Please input non-integer:");
15             if(scanf("%d"&N) != 1)
16             {
17                 fflush(stdin);
18                 continue;
19             
20             break;
21         
22     
23     else
24         = atoi(argv[1]);
25     = (int*)malloc(sizeof(int* (N - 1));
26     // inital the arry
27     for(i = 0<</span> -1i++)
28         *(m+i) = i+2;
29     while(1)
30     {
31         // find the start number-index
32         for(; <</span> - 2;j++)
33         if(*(m+j) != 0){temp = *(m+j);break;}
34 
35         if(j <</span> - 2)
36         {
37             for(i = j+1<</span> - 1i++)
38             if(*(m+i) % temp == 0)
39                 *(m+i) = 0;
40          
41         else break;
42         j++;
43     
44     printf("The primer is:");
45     for(i = 0<</span> N-1i++)
46     if(*(m+i) != 0)printf("%d,"*(m+i));
47     printf("\n");
48     free(m);
49     system("pause");
50      


当然这样占用的空间是相当大的。另外,其实可以省略所有的偶数。

3、素数判断法【简单方法】
考虑到这么一个现实:任何一个合数都可以表现为适当个素数的乘积的形式,所以我们只用素数去除要判断的数即可,比如要判断100以内的素数,只用2,3,5,7就够了,10000以内的数用100以内的素数判断足以。

 1 素数判断方法
 4 素数判断方法#include  <</span>stdio.h>
 5素数判断方法#include <</span>stdlib.h>
 6素数判断方法
 7素数判断方法void main(int argc, char* argv[])
 8素数判断方法{
 9素数判断方法    int N, i,  = 0,size;
10素数判断方法    int a[] = {2,3,5,7};
11素数判断方法    if(argc <</span> 2)
12素数判断方法    {
13素数判断方法        while(1)
14素数判断方法        {
15素数判断方法            printf("Please input non-integer:");
16素数判断方法            if(scanf("%d"&N) != 1)
17素数判断方法            {
18素数判断方法                fflush(stdin);
19素数判断方法                continue;
20素数判断方法            } 
21素数判断方法            break;
22素数判断方法        } 
23素数判断方法    } 
24素数判断方法    else
25素数判断方法        = atoi(argv[1]);
26素数判断方法    size = sizeof(a) / sizeof(int);
27素数判断方法    printf("The primer is:");
28素数判断方法    for(i =2<</span> N; i++)
29素数判断方法    {
30素数判断方法        for(j = 0<</span> size; j++)
31素数判断方法        {
32素数判断方法            if(i == a[j])
33素数判断方法                printf("%d,"i);
34素数判断方法            if(i % a[j] == 0)
35素数判断方法               break;
36素数判断方法        }
37素数判断方法        if(j == size)
38素数判断方法            printf("%d,"i);
39素数判断方法    }

40素数判断方法    printf("\n");
41素数判断方法    system("pause");
42素数判断方法}      

如果是超过了100就要补充a[]的元素,另外循环部分还要做适当改动(比如不用循环size次)。


【总结】:以上3种方法中以第3中效率最高,但是其适用性不高,只适合不大的数。当然,以上3中方法还都可以改进,以后再写。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值