zoukankan      html  css  js  c++  java
  • Goldbach's Conjecture(哥德巴赫猜想)

    Goldbach's Conjecture

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 5277    Accepted Submission(s): 2022

    点我

    Problem Description
    Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2.
    This conjecture has not been proved nor refused yet. No one is sure whether this conjecture actually holds. However, one can find such a pair of prime numbers, if any, for a given even number. The problem here is to write a program that reports the number of all the pairs of prime numbers satisfying the condition in the conjecture for a given even number.

    A sequence of even numbers is given as input. Corresponding to each number, the program should output the number of pairs mentioned above. Notice that we are interested in the number of essentially different pairs and therefore you should not count (p1, p2) and (p2, p1) separately as two different pairs.
     
    Input
    An integer is given in each input line. You may assume that each integer is even, and is greater than or equal to 4 and less than 2^15. The end of the input is indicated by a number 0.
     
    Output
    Each output line should contain an integer number. No other characters should appear in the output.
     
    Sample Input
    6
    10
    12
    0
     
    Sample Output
    1
    2
    1
     1 #include <iostream>
     2 using namespace std;
     3 int a[32765];
     4 int isprime()
     5 {
     6     int i,k,x;
     7     for(i=2;i<32765;i++)
     8     {
     9         for(k=2;k<=i/2;k++)
    10         {
    11             if(i%k==0)
    12                 break;
    13         }
    14         if(k==i/2+1)
    15             a[i]=1;
    16         else
    17             a[i]=0;
    18     }
    19     return 0;
    20 }
    21 int main()
    22 {
    23     int x,i,j,count=0;
    24     isprime();
    25     while(cin>>x&&x)
    26     {
    27         count=0;
    28         for(i=j=x/2;i>=2;i--,j++)
    29         {
    30             if(a[i]&&a[j])
    31             {
    32                 count++;
    33             }
    34         }
    35         cout<<count<<endl;
    36     }
    37 }
     
  • 相关阅读:
    日志工具——slf4j
    统一建模语言——UML
    Java基础——网络编程
    Java基础——语法基础
    Java基础——NIO(二)非阻塞式网络通信与NIO2新增类库
    Java基础——NIO(一)通道与缓冲区
    动态加载script文件的两种方法
    asp.net 通用的连接数据库实例代码
    Nginx用户认证配置方法详解(域名/目录)
    js冒泡法和数组转换成字符串示例代码
  • 原文地址:https://www.cnblogs.com/a1225234/p/4681245.html
Copyright © 2011-2022 走看看