POJ 2739 Sum of Consecutive Prime Numbers-number theory-(continuous Prime number and), pojnumbers-

Source: Internet
Author: User

POJ 2739 Sum of Consecutive Prime Numbers-number theory-(continuous Prime number and), pojnumbers-

Question: How many continuous prime numbers are there and the range of n is: 2 ~ 10000

Analysis: Pre-processes the prime number in 10000, and then finds the number of sum = n every time an input of n, because the prime number is about 1200, O (n ^ 2) no timeout

Code:

# Include <iostream> # include <string> # include <cstring> # include <algorithm> # define max (a, B) a> B? A: busing namespace std; int n, prim [5000], vis [10010]; int k; void is_prim () {k = 0; memset (vis, 0, sizeof (vis); for (int I = 2; I <10010; I ++) {if (! Vis [I]) {prim [k ++] = I; for (int j = 2; j * I <10010; j ++) {vis [j * I] = 1 ;}}} int main () {is_prim (); while (cin >>> n) {if (! N) break; int tot = 0; for (int I = 0; I <k; I ++) {int sum = 0; for (int j = I; j <k; j ++) {sum + = prim [j]; if (sum = n) {tot ++; break;} if (sum> n) break ;}} cout <tot <endl ;}}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.