素数求和

打表:先把结果存在数组里

 

找素数 速度筛选

For(i=2;i<=n;i++)

For(j=2;j<i;j++)  //时间复杂度太高

 

Memset(prime,0,sizeof(prime));int型只能初始化成-1或0;

给char型初始化一个字节以内的数值量

初始化变量的地址,初始化的数,字节数

Sqrt开平方

Const Int max=1e6+7;1000000+7

Int prime[max];

//做标记

Void int(){

Memset(prime,0,sizeof(prime));

Int n=sqrt(max);

For(int i=2;i<=n;i++)

{

If(prime[i]==0 )

For(j=i*i;j<=max;j+=i)

Prime[j]=1;

}

//存数组

for(int i=2;i<=max;i++)

{

    if(prime[i]==0)

        prime[++prime[0]]=i;

    for(int j=1;j<=prime[0]&&prime[j]<=max/i;j++)

    {

        prime[prime[j]*i]=1;

        if(i%prime[j]==0)

            break

    }

}

 

 

//0,1既不是素数也不是非素数

Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53. The integer 41 has three representations 2+3+5+7+11+13, 11+13+17, and 41. The integer 3 has only one representation, which is 3. The integer 20 has no such representations. Note that summands must be consecutive prime 
numbers, so neither 7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20. 
Your mission is to write a program that reports the number of representations for the given positive integer.

一些正整数可以由一个或多个连续素数的和来表示。一个给定的正整数有多少个这样的表示?例如,整数53有两个表示5+7+11+13+17和53。整数41有三个表示2+3+5+7+11+13, 11+13+17,41。整数3只有一个表示,即3。整数20没有这样的表示。请注意,必须是连续素数。

数字,因此,无论是7+13,还是3+5+5+7都不是整数20的有效表示。

您的任务是编写一个程序,报告给定的正整数的表示数。

Input

The input is a sequence of positive integers each in a separate line. The integers are between 2 and 10 000, inclusive. The end of the input is indicated by a zero.

Output

The output should be composed of lines each corresponding to an input line except the last zero. An output line includes the number of representations for the input integer as the sum of one or more consecutive prime numbers. No other characters should be inserted in the output.

Sample Input

2
3
17
41
20
666
12
53
0

Sample Output

1
1
2
3
0
0
1
2

给定一个数,可用用连续素数求和表示出来吗?输出N可以被表示的素数链条数。

#include <iostream>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
const int maxn=1e6+7;//数组下标不能是变量 所以要定义成常量
int prime[maxn];
int primes[maxn];
int main()
{
    memset(prime,0,sizeof(prime));
    memset(primes,0,sizeof(primes));
    double max_n = maxn;
    int q=(int)sqrt(max_n);//sqrt的返回值为double
    int n,j,i,sum=0,num=0,k;
    prime[1]=1;
    prime[0]=1;
    for(int i=2; i<=q; i++)
    {
        if(prime[i]==0 )
            for(j=i*i; j<maxn; j+=i)
                prime[j]=1;
    }
    j=0;
    for(i=2; i<=maxn; i++)
    {
        if(prime[i]==0)
        {
            primes[j]=i;
            j++;
        }
    }
    while(1)
    {
        sum=num=0;
        cin>>n;
        if(n==0)
            break;
        if(n>=2&&n<=10000)
        {
            for(i=0; i<j; i++)
            {
                sum=0;
                for(k=i; k<j; k++)
                {
                    if(sum<n)
                    {
                        sum+=primes[k];
                    }
                    else  if(sum>n)
                        break;
                    else if(sum==n)

                    {
                        num++;
                        break;
                    }
                }
            }
        }

        cout<<num<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sinat_39654987/article/details/81112043
今日推荐