Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Exercise 84

C Exercise 84

100 Classic C Language Examples 100 Classic C Language Examples

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

**Program Analysis:** Wow, what kind of problem is this? Do you want me to prove this? I really don't know how to prove it. Well then, let's just decompose an even number into two prime numbers.

Example

// Created by www.runoob.com on 15/11/9.

// Copyright (c) 2015 Runoob Tutorial. 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 the 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 above example produces the following output:



Please enter an even number:

4

偶数4可以分解成1和3两个素数的和

100 Classic C Language Examples 100 Classic C Language Examples