Sum of Consecutive Prime Numbers

怼烎@ 2023-07-08 02:52 136阅读 0赞

4.Sum of Consecutive Prime Numbers

  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
  2. numbers, so neither 7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20.
  3. Your mission is to write a program that reports the number of representations for the given positive integer.

Input

  1. 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

  1. 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

  1. 2
  2. 3
  3. 17
  4. 41
  5. 20
  6. 666
  7. 12
  8. 53
  9. 0

Sample Output

  1. 1
  2. 1
  3. 2
  4. 3
  5. 0
  6. 0
  7. 1
  8. 2

思路:

先找出范围内的素数存在p[i]中,在接受输入数据n,在p[i]中找出n在数组内的位置,返回来双层循环找和相等的素数。
下面这个java时间过不去

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc=new Scanner(System.in);
  5. int a,b,i,j,n,t,r,k=1;
  6. int[] p=new int[10001];
  7. int l=0;
  8. for(a=2;a<10000;a++) {
  9. for(b=a-1;b>1;b--) {
  10. if(a%b!=0)
  11. continue;
  12. else
  13. break;
  14. }
  15. if(b==1) {
  16. p[l]=a;
  17. l++;
  18. }
  19. }
  20. while(k>0) {
  21. n=sc.nextInt();
  22. if(n!=0) {
  23. int f=0;
  24. r=0;
  25. p[l]=10002;
  26. for(i=0;i<l;i++) {
  27. if(p[i]<=n&&p[i+1]>n) {
  28. f=i;
  29. break;
  30. }
  31. }
  32. for(i=f;i>=0;i--) {
  33. int sum=0;
  34. for(j=i;j>=0;j--) {
  35. sum+=p[j];
  36. if(sum==n) {
  37. r++;
  38. break;
  39. }
  40. if(sum>n) break;
  41. }
  42. }
  43. System.out.println(r);
  44. }
  45. }
  46. }
  47. }

C++解法

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <cmath>
  5. using namespace std;
  6. bool vis[20000];
  7. int prime[3000];
  8. int top;
  9. void Init(){
  10. memset(vis,false,sizeof(vis));
  11. for(int i=2;i<=10000;i++){
  12. if(vis[i])
  13. continue;
  14. prime[top++]=i;
  15. for(int j=i*2;j<=10000;j+=i){
  16. vis[j]=true;
  17. }
  18. }
  19. }
  20. int main(){
  21. top=0;
  22. Init();
  23. int t,sum,k;
  24. while(1){
  25. scanf("%d",&t);
  26. if(t==0)
  27. break;
  28. k=0;
  29. for(int i=0;i<top;i++){
  30. sum=0;
  31. for(int j=i;j<top;j++){
  32. sum+=prime[j];
  33. if(sum==t){
  34. k++;
  35. break;
  36. }
  37. else if(sum>t){
  38. break;
  39. }
  40. }
  41. }
  42. cout<<k<<endl;
  43. }

这个能过,哈哈哈哈!!!!!,优化了一下打表

  1. import java.util.Scanner;
  2. public class Main{
  3. static boolean [] vis=new boolean[20000];
  4. static int [] prime=new int[3000];
  5. static int top;
  6. public static void fn() {
  7. for(int i=2;i<=10000;i++){
  8. if(vis[i])
  9. continue;
  10. prime[top++]=i;
  11. for(int j=i*2;j<=10000;j+=i){
  12. vis[j]=true;
  13. }
  14. }
  15. }
  16. public static void main(String[] args) {
  17. top=0;
  18. Scanner sc=new Scanner(System.in);
  19. fn();
  20. int t,sum,k;
  21. while(true){
  22. t=sc.nextInt();
  23. if(t==0)
  24. break;
  25. k=0;
  26. for(int i=0;i<top;i++){
  27. sum=0;
  28. for(int j=i;j<top;j++){
  29. sum+=prime[j];
  30. if(sum==t){
  31. k++;
  32. break;
  33. }
  34. else if(sum>t){
  35. break;
  36. }
  37. }
  38. }
  39. System.out.println(k);
  40. }
  41. }
  42. }

发表评论

表情:
评论列表 (有 0 条评论,136人围观)

还没有评论,来说两句吧...

相关阅读