素数法

// 1 ~ 1000 素数
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
using namespace std;
bool shu[10000];
void sushu1 (int n)//O(n*sqrt(n));
{
    bool flag;
    for(int i=2 ;i <= n ;i++)
    {
        flag = 0;
        for(int j=2;j<=sqrt(i);j++)
            if( i%j==0 ) {
                flag = 1; break;}
        if(!flag) cout<<i<<endl;
    }
    cout<<endl;
}
void sushu2(int n)//筛法O(nloglogn);
{
    int k = sqrt(n) + 1;
    memset(shu,1,sizeof(shu));
    shu[1] = 0;
    for(int i=2;i<=k;i++)
    {
        int z = n / i + 1;
        for(int j=1;j<=z;j++)
        {
            shu[j*i]=0;
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(shu[i]==1)
        cout<<i<<endl;
    }
}
int main()
{
    int n = 1000 ;
    sushu1(n);
    return 0;
}
时间: 2024-08-17 14:49:12

素数法的相关文章

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

#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, 即

筛素数法小结

筛选素数方法小结: 最简单的筛素数法方法就是从2开始,将所以2的倍数去掉,然后从3开始,将3的倍数去掉,依次进行下去即可.根据这样很容易写出代码,下面代码就是是筛素数法得到100以内的素数并保存到primes[]数组中. 1 const int MAXN = 100; 2 bool flag[MAXN]; 3 int primes[MAXN / 3], pi; 4 void GetPrime_1() 5 { 6 int i, j; 7 pi = 0; 8 memset(flag, false,

hdu 2136 (Largest prime factor)就是简单 的筛选素数法

Largest prime factor Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7009    Accepted Submission(s): 2482 Problem Description Everybody knows any number can be combined by the prime number. Now

模板:筛素数法

参考:http://blog.csdn.net/liukehua123/article/details/5482854 1.开一个大的bool型数组prime[],大小就是n+1就可以了.先把所有的下标为奇数的标为true,下标为偶数的标为false. 2.然后: for( i=3; i<=sqrt(n); i+=2 ) {   if(prime[i]) for( j=i+i; j<=n; j+=i ) prime[j]=false; } 3.最后输出bool数组中的值为true的单元的下标,

最一般的筛素数法

1 int countPrimes(int n) { 2 vector<bool> prime(n,true); 3 prime[1]=prime[0]=false; 4 for(int i=3;i<n;i++) 5 if(i%2==0) 6 prime[i]=false; 7 8 for(int i=3;i<sqrt(n);i++) 9 if(prime[i]) 10 for(int j=i*i;j<n;j+=i) 11 prime[j]=false; 12 13 retu

lightoj-1028 - Trailing Zeroes (I)(素数法求因子个数)

1028 - Trailing Zeroes (I) PDF (English) Statistics ForumTime Limit: 2 second(s) Memory Limit: 32 MBWe know what a base of a number is and what the properties are. For example, we use decimal number system, where the base is 10 and we use the symbols

HDU 2138 How many prime numbers(Miller_Rabin法判断素数 【*模板】 用到了快速幂算法 )

How many prime numbers Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12955    Accepted Submission(s): 4490 Problem Description Give you a lot of positive integers, just to find out how many pr

codevs 5790 素数序数

5790 素数序数(筛素数版) 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题目描述 Description 给定一个整数n,保证n为正整数且在int范围内,输出n是第几个质数 建议使用筛素数法 输入描述 Input Description 一行,一个整数 输出描述 Output Description 一行,一个整数 样例输入 Sample Input 3 样例输出 Sample Output 2 数据范围及提示 Data Size & Hint int就够

bjfu1211 推公式,筛素数

题目是求fun(n)的值 fun(n)= Gcd(3)+Gcd(4)+…+Gcd(i)+…+Gcd(n).Gcd(n)=gcd(C[n][1],C[n][2],……,C[n][n-1])C[n][k] means the number of way to choose k things from n things. n最大一百万,马上反映到可能是递推打表. 首先肯定是推公式了,fun(n)其实就是Gcd(n)的一个前n项和,没意义,直接看Gcd(n),把前几项列出来,发现公式是Gcd(n) =