POJ2262_Goldbach's Conjecture【素数判断】【水题】

Goldbach‘s Conjecture

Time Limit: 1000MS
Memory Limit: 65536K

Total Submissions: 38024
Accepted: 14624

Description

In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conjecture:

Every even number greater than 4 can be

written as the sum of two odd prime numbers.

For example:

8 = 3 + 5. Both 3 and 5 are odd prime numbers.

20 = 3 + 17 = 7 + 13.

42 = 5 + 37 = 11 + 31 = 13 + 29 = 19 + 23.

Today it is still unproven whether the conjecture is right. (Oh wait, I have the proof of course, but it is too long to write it on the margin of this page.)

Anyway, your task is now to verify Goldbach‘s conjecture for all even numbers less than a million.

Input

The input will contain one or more test cases.

Each test case consists of one even integer n with 6 <= n < 1000000.

Input will be terminated by a value of 0 for n.

Output

For each test case, print one line of the form n = a + b, where a and b are odd primes. Numbers and operators should be separated by exactly one blank like in the sample output below. If there is more than one pair of odd primes adding up to n, choose the pair
where the difference b - a is maximized. If there is no such pair, print a line saying "Goldbach‘s conjecture is wrong."

Sample Input

8

20

42

0

Sample Output

8 = 3 + 5

20 = 3 + 17

42 = 5 + 37

Source

Ulm Local 1998

题目大意:给你一个数n,拆分成两个奇素数相加的形式,另这两个素数的距离最大

思路:从三开始枚举奇数,判断数i和n-i是否都为素数,若为素数则输出结果。

#include<stdio.h>

int Prime[1000010];
void IsPrime()
{
    for(int i = 2; i <= 1000000; i++)
        Prime[i] = 1;
    for(int i = 2; i <= 1000000; i++)
    {
        if(Prime[i])
        {
           for(int j = i+i; j <= 1000000; j+=i)
           {
               Prime[j] = 0;
           }
        }

    }
}

int main()
{
    int n;
    IsPrime();
    while(~scanf("%d",&n) && n)
    {
        for(int i = 3; i <=n/2; i+=2)
        {
            if(Prime[i] && Prime[n-i])
            {
                printf("%d = %d + %d\n",n,i,n-i);
                break;
            }
        }
    }
    return 0;
}

POJ2262_Goldbach's Conjecture【素数判断】【水题】

时间: 2024-10-03 12:40:03

POJ2262_Goldbach's Conjecture【素数判断】【水题】的相关文章

POJ 2262 Goldbach&#39;s Conjecture (素数判断)

Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37693   Accepted: 14484 Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conject

第K大素数(水题)

第K大素数 Time Limit: 1000ms   Memory limit: 32768K  有疑问?点这里^_^ 题目描述 大家都知道素数是数学中很有意思的一类数,或许聪明的你已经知道了如何判定一个数是否是素数,但今天不一样了,你的任务是求第K大素数.你能快速完成吗? 输入 输入只有一个整数K. ps:我们只关心那些正整数. 输出 输出只有一行,即第K大素数. 示例输入 5 示例输出 11 提示 来源 示例程序 #include <stdio.h> #include <math.h

HDU 5776 sum (BestCoder Round #85 A) 简单前缀判断+水题

分析:就是判断简单的前缀有没有相同,注意下自身是m的倍数,以及vis[0]=true; #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> #include <map> #include <queue> #include <vect

POJ2909_Goldbach&#39;s Conjecture【素数判断】【水题】

Goldbach's Conjecture Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10116 Accepted: 5973 Description 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 c

蓝桥杯 算法训练 Torry的困惑(基本型)(水题,筛法求素数)

算法训练 Torry的困惑(基本型) 时间限制:1.0s   内存限制:512.0MB 问题描述 Torry从小喜爱数学.一天,老师告诉他,像2.3.5.7--这样的数叫做质数.Torry突然想到一个问题,前10.100.1000.10000--个质数的乘积是多少呢?他把这个问题告诉老师.老师愣住了,一时回答不出来.于是Torry求助于会编程的你,请你算出前n个质数的乘积.不过,考虑到你才接触编程不久,Torry只要你算出这个数模上50000的值. 输入格式 仅包含一个正整数n,其中n<=100

Goldbach&#39;s Conjecture POJ - 2262 线性欧拉筛水题 哥德巴赫猜想

题意 哥德巴赫猜想:任一大于2的数都可以分为两个质数之和 给一个n 分成两个质数之和 线行筛打表即可 可以拿一个数组当桶标记一下a[i]  i这个数是不是素数  在线性筛后面加个装桶循环即可 #include<cstdio> #include<cstring> using namespace std; bool Is_Primes[1000005]; int Primes[1000005]; int cnt; void Prime(int n){ cnt=0; memset(Is_

cdoj 26 遮挡判断(shadow) 水题

遮挡判断(shadow) Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/26 Description 在一个广场上有一排沿着东西方向排列的石柱子,阳光从东边以一定的倾角射来(平行光).有的柱子可能被在他东边的高大的柱子的影子给完全遮挡住了.现在你要解决的问题是求出有多少柱子是没有被完全遮挡住的. 假设每个石柱子是一根细棒,而且都垂直于地面摆放. Input 输入包含多组数据

CodeForces 690C1 Brain Network (easy) (水题,判断树)

题意:给定 n 条边,判断是不是树. 析:水题,判断是不是树,首先是有没有环,这个可以用并查集来判断,然后就是边数等于顶点数减1. 代码如下: #include <bits/stdc++.h> using namespace std; const int maxn =1000 + 5; int p[maxn]; int Find(int x){ return x == p[x] ? x : p[x] = Find(p[x]); } int main(){ int n, m, x, y; cin

HDU ACM 2521 反素数 水题+因子打表

分析:水题,不解释. #include<iostream> using namespace std; int cnt[6000]; void init() //打表 { int i,j; memset(cnt,0,sizeof(cnt)); cnt[1]=1; //1只有他本身 for(i=2;i<=5005;i++) { cnt[i]+=2; //1和他本身 for(j=2;j<=i/2;j++) if(i%j==0) cnt[i]++; } } int main() { int