Poj2739_sum of consecutive prime numbers [screening method for prime number] [enumeration]

Source: Internet
Author: User
Sum of consecutive prime numberstime limit: 1000 ms memory limit: 65536 ktotal submissions: 19350 accepted: 10619 description


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.
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 shoshould be composed of lines each corresponding to an input line should t the last zero. an output line des the number of representations for the input Integer as the sum of one or more consecutive prime numbers. no other characters shoshould 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
Source


Japan 2005

Question:

A number can be obtained by summation of several consecutive prime number sequences, for example, 41 = 2 + 3 + 5 + 7 + 11 + 13 = 11 + 13 + 17 = 41

Three different prime number sequences are obtained by summation. Give you a number N, and find the number of solutions that satisfy n = continuous prime number sequences and

Ideas:

This is a simple question, but it may time out to judge the prime number using the common method. Here we use the screening method to find the prime number directly using the array prime

Determine whether it is a prime number. Open an array primenum to store all prime numbers.

Finally, it is enumeration to obtain the number of solutions that meet the requirements.


# Include <stdio. h> # include <string. h> int prime [10010], primenum [10010]; int isprime () // returns the prime number {Prime [0] = prime [1] = 0; for (INT I = 2; I <= 10000; I ++) prime [I] = 1; for (INT I = 2; I <= 10000; I ++) {for (Int J = I + I; j <= 10000; j + = I) prime [J] = 0;} int num = 0; For (INT I = 0; I <= 10000; I ++) if (prime [I]) primenum [num ++] = I; return num;} int main () {int num = isprime (); int N; while (~ Scanf ("% d", & N) & n! = 0) {int COUNT = 0; For (INT I = 0; primenum [I] <= N & I <num; I ++) // enumeration {int sum = 0; For (Int J = I; primenum [J] <= N & J <num; j ++) {sum + = primenum [J]; If (sum = N) {count ++; break;} If (sum> N) break ;}} printf ("% d \ n", count);} return 0 ;}


Poj2739_sum of consecutive prime numbers [screening method for prime number] [enumeration]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.