zoukankan      html  css  js  c++  java
  • Summation of Four Primes

    欢迎访问我的新博客:http://www.milkcu.com/blog/

    原文地址:http://www.milkcu.com/blog/archives/uva10168.html

    原创:Summation of Four Primes - PC110705

    作者:MilkCu

    题目描述

    Summation of Four Primes

     

    Waring's prime number conjecture states that every odd integer is either prime or the sum of three primes. Goldbach's conjecture is that every even integer is the sum of two primes. Both problems have been open for over 200 years.

    In this problem you have a slightly less demanding task. Find a way to express a given integer as the sum of exactly four primes.

    Input

    Each input case consists of one integer n ( n$ le$10000000) on its own line. Input is terminated by end of file.

    Output

    For each input case n, print one line of output containing four prime numbers which sum up to n. If the number cannot be expressed as a summation of four prime numbers print the line ``Impossible." in a single line. There can be multiple solutions. Any good solution will be accepted.

    Sample Input

    24
    36
    46
    

    Sample Output

    3 11 3 7
    3 7 13 13
    11 11 17 7

    解题思路

    该题假定题目给出的两个猜想是正确的。

    若n <= 7,则n不可能拆分为4个素数之和;

    若n >= 8,
    当n为偶数时,
    n - 2 - 2为偶数,可写成两偶数的和,
    所以n可以写成2和2,还有两个质数的和;
    当n为奇数时,
    n - 2 - 3为偶数,可写成两偶数的和,
    所以n可以写成2和3,还有两个质数的和。

    代码实现

    #include <iostream>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    int isPrime(int x) {
    	int s = sqrt(x);
    	for(int i = 2; i <= s; i++) {
    		if(x % i == 0) {
    			return 0;
    		}
    	}
    	return 1;
    }
    void twopart(int x, int & a, int & b) {
    	for(int i = 2; i <= x / 2; i++) {
    		if(isPrime(i) && isPrime(x - i)) {
    			a = i;
    			b = x - i;
    			return;
    		}
    	}
    	return;
    }
    int main(void) {
    	int n;
    	while(cin >> n) {
    		if(n <= 7) {
    			cout << "Impossible." << endl;
    			continue;
    		}
    		if(n % 2) {
    			int a, b;
    			twopart(n - 2 - 3, a, b);
    			cout << "2 3 " << a << " " << b << endl;
    		} else {
    			int a, b;
    			twopart(n - 2 - 2, a, b);
    			cout << "2 2 " << a << " " << b << endl;
    		}
    	}
    	return 0;
    }

    (全文完)

    本文地址:http://blog.csdn.net/milkcu/article/details/23599369

  • 相关阅读:
    理解爬虫原理
    中文词频统计与词云生成
    复合数据类型,英文词频统计
    字符串操作、文件操作,英文词频统计预处理
    了解大数据的特点、来源与数据呈现方式
    作业四-结对项目
    大数据应用期末总评
    分布式文件系统HDFS 练习
    安装Hadoop
    《恶魔人crybaby》豆瓣短评爬取
  • 原文地址:https://www.cnblogs.com/milkcu/p/3808848.html
Copyright © 2011-2022 走看看