代码需要使用c99编译

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//是否为素数
//从2到x-1测试是否可以整除
//时间复杂度O(n-2),n趋向正无穷
int isPrime(int x)
{
int ret = ;
for(int i = ; i < x; i++)
{
if(x % i == )
{
ret = ;
break;
}
}
return ret;
} //除了2以外,所有的偶数都不是素数,从3到x-1,每次加2
//x为偶数时间复杂度O((n-3)/2+1)
//x很大时时间复杂度接近于(n/2)
int isPrime2(int x)
{
int ret = ;
if(x == || (x % == && x != ))
ret = ; for (int i = ; i < x; i += )
{
if( x % i == )
{
ret = ;
break;
}
} return ret;
} //无须到x-1,到sqrt(x)
//时间复杂度O(Log2n) 即sqrt(x)
int isPrime3(int x)
{
int ret = ; if(x == || x % == && x != )
ret = ;
for(int i = ; i < sqrt(x); i += )
{
if( x % i == )
{
ret = ;
break;
}
} return ret;
} //判断是否能被已知的且<x的素数整除
int isPrime4(int x, int knownPrimes[], int numberOfKnownPrimes)
{
int ret = ;
int i; for( i = ; i < numberOfKnownPrimes; i++)
{
if (x % knownPrimes[i] == )
{
ret = ;
break;
}
} return ret;
} int main()
{
const int number = ;
int prime[number] = {};
int count = ;
int i = ; while( count < number )
{
if(isPrime4(i ,prime, count))
{
prime[count++] = i;
}
i++;
} for( i = ; i < number; i++)
{
printf("%d",prime[i]);
if( (i + ) % ) printf("\t");
else printf("\n");
}
return ;
}

c语言求素数以及改进算法的更多相关文章

  1. C语言求素数的算法

    前言 最后一次是出了素数的问题C语言解决题目(面试),当时用了最粗暴的算法.回来细致參考资料,事实上答案有非常多种: 1,小学生版本号: 推断 x 是否为质数,就从 2 一直算到 x-1. stati ...

  2. c语言-求完全数的一个算法

    #include<iostream> #include<math.h> using namespace std; int main() { int n,i,j=0,k,a[10 ...

  3. 蓝桥杯 算法训练 Torry的困惑(基本型)(水题,筛法求素数)

    算法训练 Torry的困惑(基本型) 时间限制:1.0s   内存限制:512.0MB      问题描述 Torry从小喜爱数学.一天,老师告诉他,像2.3.5.7……这样的数叫做质数.Torry突 ...

  4. c语言求回文数的三种算法的描述

    c语言求回文数的三种算法的描述 题目描述 注意:(这些回文数都没有前导0) 1位的回文数有0,1,2,3,4,5,6,7,8,9 共10个: 2位的回文数有11,22,33,44,55,66,77,8 ...

  5. 转载 筛子算法之golang实现求素数解析

    package main import "fmt" // Send the sequence 2, 3, 4, ... to channel 'ch'. func generate ...

  6. 【转】C语言快速幂取模算法小结

    (转自:http://www.jb51.net/article/54947.htm) 本文实例汇总了C语言实现的快速幂取模算法,是比较常见的算法.分享给大家供大家参考之用.具体如下: 首先,所谓的快速 ...

  7. 如何写出高质量的Python代码--做好优化--改进算法点滴做起

    小伙伴你的程序还是停留在糊墙吗?优化代码可以显示程序员的素质欧! 普及一下基础了欧: 一层for简写:y = [1,2,3,4,5,6],[(i*2) for i in y ]       会输出  ...

  8. c语言求平面上2个坐标点的直线距离、求俩坐标直线距离作为半径的圆的面积、递归、菲波那次数列、explode

    #include <stdio.h> #include <math.h> #include <string.h> char explode( char * str ...

  9. 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 29046   Accepted: 7342 Case ...

随机推荐

  1. bootstrap 表单验证 dem

    地址:http://www.jq22.com/yanshi522 一些api详解:http://blog.csdn.net/u013938465/article/details/53507109 ht ...

  2. cursor:not-allowed

    今天发现了一个鼠标样式:not-allowed,是一个红色圈加一个斜杠,表示禁止的意思,似乎IE ,chrome firefox 都可以正常显示,很好用

  3. Girls Like You--Maroon 5

    Girls Like You Spent 24 hours, I need more hours with you (24小时过去 还想和你 相处更久) You spent the weekend g ...

  4. C. Vasily the Bear and Sequence Codeforces 336C(枚举,思维)

    C. Vasily the Bear and Sequence time limit per test 1 second memory limit per test 256 megabytes inp ...

  5. python 前置程序窗口,还原最小化的窗口

    python 前置程序窗口,还原最小化的窗口 在网上找了比较久,大多是: win32gui.FindWindow(class_name, window_name) win32gui.SetForegr ...

  6. AGC刷题记

    已经刷不了几天了... AGC001 A-BBQ Easy 排个序就过了 B-Mysterious Light 手膜一下,你会发现魔改一下\(gcd\)就行了 C-Shorten Diameter 刚 ...

  7. hdfs冷热数据分层存储

    hdfs如何让某些数据查询快,某些数据查询慢? hdfs冷热数据分层存储 本质: 不同路径制定不同的存储策略. hdfs存储策略 hdfs的存储策略 依赖于底层的存储介质. hdfs支持的存储介质: ...

  8. Neo4j入门博客分享

    Neo4j学习参考博客:https://www.cnblogs.com/ljhdo/p/5516793.html Neo4j Cypher查询语言详解 http://www.ttlsa.com/nos ...

  9. Python序列构成的数组

    1.内置序列类型 容器序列:list,tuple,collections.deque (能存放不同类型) 扁平序列:str,bytes,bytearray,memoryview,array.array ...

  10. Delphi 实现Ping命令

    Delphi  实现Ping命令 unit FtPing; interface uses Windows, SysUtils, Classes, Controls, Winsock, StdCtrls ...