一般素数打表+高效算法

最近几天脑子越来越来笨了,一个简单的素数环问题纠结一天,没这么搞懂回溯的思路,不过涉及素数的话,突然想总结一下常用的素数打表,

一般用的是下面的代码:

#include <math.h>//素数打表
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
#define maxn 1000000
bool p1[maxn];   //判断p[i]中的i是否为素数
int p2[maxn];   //存储素数
void prim_num()
{
    int i,j,n;
    for(i=1; i<=maxn; i++)
        p1[i]=true;
    n=(int)sqrt(maxn);
    for(i=2; i<=n; i++)
    {
        for(j=i+i; j<=maxn; j+=i)   //素数的合肯定不是素数,这就是判断哪些数不是素数
        {
            p1[j]=false;
        }
    }
    j=1;
    for(i=1; i<=maxn; i++)  //把素数存储入pmaxn[N],下表从1开始
    {
        if(p1[i])
        {
            p2[j++]=i;
        }
    }
}
int main()
{
    int i;
    prim_num();
    for(i=1;i<=100;i++) //输出前100个的素数
    {
        printf("%d ",p2[i]);
    }
    return 0;
}

从网上找到了另一种写法,效率明显提高不少,线性筛法,目前还在深究。。。。。

代码如下:

/*
遇到素数需要打表时,先估算素数的个数:
num = n / lnx;
num为大概数字,越大误差越小(只是估计,用于估算素数表数组大小)
线性筛法wwwww
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
#define maxn 10010000
int n;
bool visit[maxn];
int prime[maxn];
void isprime()
{
    memset(visit, true, sizeof(visit));
    int num = 0;
    for (int i = 2; i <= n; ++i)
    {
        if (visit[i] == true)
        {
            num++;
            prime[num] = i;
        }
        for (int j = 1; ((j <= num) && (i * prime[j] <= n));  ++j)
        {
            visit[i * prime[j]] = false;
            if (i % prime[j] == 0) break; //深究之处!
        }
    }
}
int main()
{
    memset(prime, 0, sizeof(prime));
    int count= 0;
    scanf("%d",&n);
    isprime();
    for(int i = 0; i <= n; ++i)
        if(prime[i])
        {
            printf("%d ",prime[i]);
            count++;
        }
    printf("\n");
    printf("the sum of prime num is:%d\n",count);
    return 0;
}

以后要用到素数的话, 直接复制,方便不少

时间: 2024-11-06 16:44:30

一般素数打表+高效算法的相关文章

[ACM] POJ 2635 The Embarrassed Cryptographer (同余定理,素数打表)

The Embarrassed Cryptographer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11978   Accepted: 3194 Description The young and very promising cryptographer Odd Even has implemented the security module of a large system with thousands of

POJ 1365(质因数分级+素数打表)

Prime Land Time Limit: 1000ms                                Memory Limit: 10000KB Everybody in the Prime Land is using a prime base number system. In this system, each positive integer x is represented as follows: Let {pi}i=0,1,2,... denote the incr

计算机图形学 有效边表填充算法(6)

作者:卿笃军 原文地址:http://blog.csdn.net/qingdujun/article/details/40154077 本文通过一个完整的实例,展示多边形有效边表填充算法. 1)创建CAET类 头文件:AET.h // AET.h: interface for the CAET class. // ////////////////////////////////////////////////////////////////////// #if !defined(AFX_AET_

标记素数法(模板)+素数打表

#include <stdio.h> #include <string.h> #define N 3000000 int f[3000000]; int main() { memset(f, 0, sizeof(f)); int i, j; f[0]=1; f[1]=1; for(i=2; i<=N; i++) { if(f[i]==0) { for(j=i*2; j<=N; j+=i) { f[j]=1; //不是素数 } } } // 打印所有发f[i]==0, 即

Fermat’s Chirstmas Theorem (素数打表的)

Fermat’s Chirstmas Theorem Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practice SDUTOJ 2093 Description In a letter dated December 25, 1640; the great mathematician Pierre de Fermat wrote to Marin Mersenne

Ligh OJ 1370 Party All the Time (欧拉函数 +素数打表)

1370 - Bi-shoe and Phi-shoe PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular coach for his success. He needs some bamboos for

CodeForces 385C Bear and Prime Numbers 素数打表

第一眼看这道题目的时候觉得可能会很难也看不太懂,但是看了给出的Hint之后思路就十分清晰了 Consider the first sample. Overall, the first sample has 3 queries. The first query l = 2, r = 11 comes. You need to count f(2) + f(3) + f(5) + f(7) + f(11) = 2 + 1 + 4 + 2 + 0 = 9. The second query comes

HDU 1397 Goldbach&#39;s Conjecture(素数打表)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1397 Problem Description Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2. This conjecture has not

hdu 4715 素数打表

先利用筛法完成素数打表 再从小到大判断即可 #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> using namespace std; const int Max = 1e6 + 50; int n; int isPrime[Max]; int tblPrime[Max];