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.