Easy Tutorial
❮ C Exercise Example26 C Examples Write File ❯

C Exercise Example 84

C Language Classic 100 Examples

Topic: An even number can always be expressed as the sum of two prime numbers.

Program Analysis: What on earth is this question? Do they want me to prove this? I really don't know how to prove it. So let's just decompose an even number into two prime numbers.

Example

//  Created by www.tutorialpro.org on 15/11/9.
//  Copyright © 2015 tutorialpro.org. All rights reserved.
//

#include<stdio.h>
#include<stdlib.h>
int Isprimer(unsigned int n);
int main()
{
    unsigned int n,i;
    do{
        printf("Please enter an even number:\n");
        scanf("%d",&n);
    }while(n%2!=0);
    for(i=1;i&lt;n;i++)
        if(Isprimer(i)&&Isprimer(n-i))
            break;
    printf("The even number %d can be decomposed into the sum of the prime numbers %d and %d\n",n,i,n-i);

    return 0;
}
int Isprimer(unsigned int n)
{
    int i;
    if(n&lt;4)return 1;
    else if(n%2==0)return 0;
    else
        for(i=3;i&lt;sqrt(n)+1;i++)
            if(n%i==0)return 0;

    return 1;
}

The above example outputs the following result when run:

Please enter an even number:
4
The even number 4 can be decomposed into the sum of the prime numbers 1 and 3

C Language Classic 100 Examples

❮ C Exercise Example26 C Examples Write File ❯