Question #14079

Given a range of integers by its lower and upper limit, print a list of all even numbers and their Goldbach composition.
Example:
Input lower limit: 9
Input upper limit: 20
Output:
10 = 3 + 7
12 = 5 + 7
14 = 3 + 11
16 = 3 + 13
18 = 5 + 13
20 = 3 + 17

Expert's answer

int i;

int lowerlimit;

int

upperlimit;

onSelfEvent(load){

lowerlimit=9;

upperlimit=20;

for(i=lowerlimit;i<=upperlimit;i++){

if

(i%2==0){

_root.Show(i);

}

}

}

int j;

int N;

int

primes = 0;

int nn=0;

int left=0;

int right=0;

function Show(number

) {

isprime = new Array;

N =

number;

for(j=2;j<N;j++){

isprime[j] =

"1";

}

}

for(j=2;j<=N;j++){

if(i*i<N){

if(isprime[j]=="1"){

for(l=j;l<=N;l++){

isprime[i

* j] = "0" ;

}

}

}

}

for(j=2;j<N;j++){

if(isprime[j]=="1"){

primes =

primes + 1;

}

}

list = new Array[primes]

for(j=0;j<N;j++){

if(isprime[j]=="1"){

nn = nn + 1;

list[nn] =

"i";

}

}

left = 0;

right = primes - 1;

while (left

<= right){

if(list[left] + list[right] = N){

break;

else if

(list[left] + list[right] < N){

left = left + 1;

else

right = right

- 1;

}

}

}

if (list[left] + list[right] = N){

trace(N + " = " +

list[left] + " + " + list[right]);

}

else{

trace(N + " not expressible

as sum of two primes");

}

return 0;

}

int lowerlimit;

int

upperlimit;

onSelfEvent(load){

lowerlimit=9;

upperlimit=20;

for(i=lowerlimit;i<=upperlimit;i++){

if

(i%2==0){

_root.Show(i);

}

}

}

int j;

int N;

int

primes = 0;

int nn=0;

int left=0;

int right=0;

function Show(number

) {

isprime = new Array;

N =

number;

for(j=2;j<N;j++){

isprime[j] =

"1";

}

}

for(j=2;j<=N;j++){

if(i*i<N){

if(isprime[j]=="1"){

for(l=j;l<=N;l++){

isprime[i

* j] = "0" ;

}

}

}

}

for(j=2;j<N;j++){

if(isprime[j]=="1"){

primes =

primes + 1;

}

}

list = new Array[primes]

for(j=0;j<N;j++){

if(isprime[j]=="1"){

nn = nn + 1;

list[nn] =

"i";

}

}

left = 0;

right = primes - 1;

while (left

<= right){

if(list[left] + list[right] = N){

break;

else if

(list[left] + list[right] < N){

left = left + 1;

else

right = right

- 1;

}

}

}

if (list[left] + list[right] = N){

trace(N + " = " +

list[left] + " + " + list[right]);

}

else{

trace(N + " not expressible

as sum of two primes");

}

return 0;

}

## Comments

## Leave a comment