Complete Goldbach conjecture with control statement in C # (an even number greater than two must be the sum of two prime numbers)

Source: Internet
Author: User

Idea: first analysis and judgment, the entire program is repeated input numbers, so the whole process is in a dead loop statement, which is the use of a whil statement, the condition is true, followed by the user input of the number of the judgment, the removal of less than equals 2 and greater than 2 of the cardinality, The rest of the numbers will satisfy the condition; then find a prime number between 1-input I; this is the definition of a k,k equals input-i; then I, if I is also a prime number, can output input=i+k, and finally complete the calculation. (When coding too much, it's best to create intervals to separate them, which is easy to parse and get wrong)

The code is as follows:

while (true)
{

Console.Write ("Please enter an even number greater than 2:");
int input = Int. Parse (Console.ReadLine ());
#region \ \ Remove the number less than or equal to 2
if (Input <= 2)
{
Console.Write ("Input is wrong, please press ENTER to re-enter!") ");
Console.ReadLine ();
Console.clear ();
}
#endregion
Else
{
#region//Remove cardinality
if (input% 2! = 0)
{
Console.Write ("Input is wrong, please press ENTER to re-enter!") ");
Console.ReadLine ();
Console.clear ();
}
#endregion

Else
{


for (int i = 2; i < (INPUT/2) +1; i++)
{
BOOL Falg = false;
for (int j = 2; J < I; J + +)
{
if (i% j = 0)
{
Falg = true;
}

}
if (!FALG)
{
int k = input-i;
if (k>1) {
BOOL ABC = FALSE;
for (int x = 2; x < K; × x + +)
{
if (k% x = = 0)
{
ABC = TRUE;
}
}
if (!ABC)
{
Console.Write (input + "=" + i + "+" + K);
Console.ReadLine ();
Console.clear ();
}
}

}
}


}

}
}

Complete Goldbach conjecture with control statement in C # (an even number greater than two must be the sum of two prime numbers)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.