原创:Summation of Four Primes - PC110705
作者:MilkCu
|
|||||
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.
Each input case consists of one integer n ( n10000000) on its own line. Input is terminated by end of file.
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.
24 36 46
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
Summation of Four Primes - PC110705,布布扣,bubuko.com
Summation of Four Primes - PC110705
原文:http://blog.csdn.net/milkcu/article/details/23599369