我創建了一個程序,可以將給定的偶數分解爲兩個素數之和。C++將偶數分解爲質數
#include <iostream>
#include <stdio.h>
using namespace std;
int i(int x, int y)
{
if (x > y)
{
if (x % y)
return i(x, y + 1);
else
return 0;
}
else
return (x > 1);
}
int main()
{
int a, b;
do
{
cout << "Please input a positive even number: ";
cin >> a;
if (a % 2 == 0 && a >= 1)
{
for (b = a/2; b > 1; b--)
{
if ((i(b, 2) && i(a-b, 2)) &&
printf("%i + %i\n", b, a-b));
}
}
else if (a % 2 != 0 && a >= 1)
{
cout << a << "="<< a << endl;
}
else
break;
}
while(a >= 4);
return 0;
}
不過,我想打破偶數爲一個素因子相乘首要因素,例如,12 = 2 * 2 * 3。修改程序的任何提示?感謝您的幫助
https://www.google.com/search?q=prime+factorization+c%2B%2B – IdeaHat 2014-12-03 14:18:15
您所提供的代碼中有共同的幾乎沒有與你的目標。你所要求的是所謂的分解,並且是無止境的重新審視。四處挖掘,並提出更具體的問題。 – MariusSiuram 2014-12-03 14:23:01