Goldbach's Conjecture
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 41582   Accepted: 15923

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

心得:自己写的筛法时间复杂度是O(n^2),利用数组下标省去一个循环后变成了O(n);妙不可言!!!
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 1000000
int n,a[N];
bool pr[N];
void pdd()
{
int i,j;
for(i=;i<;i++) a[i]=;
a[]=; a[]=;
for(i=;i<;i++)
{
if(a[i]==)
{
for(j=i*;j<;j+=i) a[j]=;
}
}
}
int main()
{
int num;
int i,flag;
pdd();
while(scanf("%d",&num)!=EOF&&num!=)
{
for(flag=,i=;i<num;i++)
{
if(a[i]==&&a[num-i]==)
{
printf("%d = %d + %d\n",num,i,num-i);
flag=;
break;
}
}
if(flag==)
printf("Goldbach's conjecture is wrong.\n");
}
return ;
}

poj 2262 筛法求素数(巧妙利用数组下标!)的更多相关文章

  1. POJ 2689 筛法求素数

    DES:给出一个区间[L, U].找出这个区间内相邻的距离最近的两个素数和距离最远的两个素数.其中1<=L<U<=2147483647 区间长度不超过1000000. 思路:因为给出 ...

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

    算法训练 Torry的困惑(基本型) 时间限制:1.0s   内存限制:512.0MB      问题描述 Torry从小喜爱数学.一天,老师告诉他,像2.3.5.7……这样的数叫做质数.Torry突 ...

  3. Algorithm --> 筛法求素数

    一般的线性筛法 genPrime和genPrime2是筛法求素数的两种实现,一个思路,表示方法不同而已. #include<iostream> #include<math.h> ...

  4. POJ2739_Sum of Consecutive Prime Numbers【筛法求素数】【枚举】

    Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19350 Ac ...

  5. 初等数论-Base-1(筛法求素数,欧拉函数,欧几里得算法)

    前言 初等数论在OI中应用的基础部分,同机房的AuSquare和zhou2003君早就写完了,一直划水偷懒的Hk-pls表示很方,这才开始了这篇博客. \(P.S.\)可能会分部分发表. Base-1 ...

  6. hdu 4548 筛法求素数 打表

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4548 Problem Description 小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题 ...

  7. UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)

      Carmichael Numbers  An important topic nowadays in computer science is cryptography. Some people e ...

  8. 筛法求素数Java

    输出:一个集合S,表示1~n以内所有的素数 import java.util.Scanner; public class 筛法求素数 { public static void main(String[ ...

  9. JD 题目1040:Prime Number (筛法求素数)

    OJ题目:click here~~ 题目分析:输出第k个素数 贴这么简单的题目,目的不清纯 用筛法求素数的基本思想是:把从1開始的.某一范围内的正整数从小到大顺序排列, 1不是素数,首先把它筛掉.剩下 ...

随机推荐

  1. Bzoj2832 / Bzoj3874 宅男小C

    Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 124  Solved: 26 Description 众所周知,小C是个宅男,所以他的每天的食物要靠外 ...

  2. Mayor's posters(线段树+离散化+区间染色)

    题目链接:http://poj.org/problem?id=2528 题目: 题意:将n个区间进行染色(对于同一个区间,后一次染色会覆盖上一次的染色),问最后可见的颜色有多少种. 思路:由于区间长度 ...

  3. ubuntu tomcat的安装与配置

    一.下载jdk 大概是tomat大部分是由java写的, 所以一开始安装tomcat必须得配置好jdk http://www.oracle.com/technetwork/java/javase/do ...

  4. 64_g2

    gettext-libs-0.19.8.1-9.fc26.x86_64.rpm 15-Mar-2017 14:15 305038 gf2x-1.1-9.fc26.i686.rpm 11-Feb-201 ...

  5. 【LOJbeta round1】ZQC的手办

    NOI2012-超级钢琴的升级版. 用线段树维护最小值及其出现位置,接下来就跟超级钢琴一个做法了. #include<bits/stdc++.h> #define N 500010 #de ...

  6. 网站服务器压力Web性能测试(4):服务器压力Web性能测试小结

    1.Apache Bench,Webbench,http_load对网站压力Web性能进行测试时,为了得到更加客观和准确的数值,应该从远程访问.局域网访问和本地等多个方面进行全方位的测试.一般用127 ...

  7. 苹果电脑Mac OS系统重装图文详解

    苹果电脑Mac OS系统重装图文详解 本文来自于[系统之家] www.xp85.com现在电脑都很强大,可是也很脆弱,常常需要你去维护,甚至经常需要你重装系统,那么Mac OS又如何重装系统呢?刚刚使 ...

  8. leetcode 之Reorder List(25)

    找到中间结点,将后半部分反转接入即可. ListNode *reoderList(ListNode* head) { if (head == nullptr || head->next == n ...

  9. P2885

    2885 code[class*="language-"] { padding: .1em; border-radius: .3em; white-space: normal; b ...

  10. hihocoder 1178 : 计数

    #1178 : 计数 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Rowdark是一个邪恶的魔法师.在他阅读大巫术师Lich的传记时,他发现一类黑魔法来召唤远古生物, ...