C Exercise Example 84

100 Classic C Examples

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

Program analysis:Damn, what kind of problem is this? Do they want me to prove this? I really don't know how to prove it. Fine, let's just decompose an even number into two prime numbers.

Example

// Created by www.example.com on 15/11/9. // Copyright © 2015 Example. 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<n;i++) if(Isprimer(i)&&Isprimer(n-i)) break; printf("The even number %d can be decomposed into the sum of two prime numbers %d and %d\n",n,i,n-i); return 0; } int Isprimer(unsigned int n) { int i; if(n<4)return 1; else if(n%2==0)return 0; else for(i=3;i<sqrt(n)+1;i++) if(n%i==0)return 0; return 1; }

The output result of running the above example is:

请输入一个偶数:
4
偶数4可以分解成1和3两个素数的和

100 Classic C Examples

other extensions