Summation of Four Primes - PC110705

欢迎访问我的新博客: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

转载于:https://www.cnblogs.com/milkcu/p/3808848.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值