Goldbach Conjecture

Highlight the following code and save it as a text file called GOLBACH.BAS
Put it in the same folder as your GW-BASIC program. Load it and run it.

10 'Goldbach conjecture
20 CLS :KEY OFF :CLEAR
30 PRINT "GOLDBACH CONJECTURE" :PRINT
40 PRINT "Every even number greater than 2 can be written as the sum of two primes." :PRINT
50 'Count and list Goldbach Prime Pairs
60 INPUT "Enter a test number ";T
70 T=2*INT(T/2)                   'Ensure test number is even
80 N=0
90 FOR K=3 TO T-3 STEP 2
100   X=SQR(K)
110   Z=0
120   FOR J=3 TO X STEP 2
130     IF K/J-INT(K/J)=0 THEN LET Z=Z+1
140   NEXT J
150   IF Z=0 THEN GOSUB 180       'found a prime, check for 2nd prime
160 NEXT K
170 END
180 'SUBROUTINE to test if other component is also prime
190 L=T-K
200 P=SQR(L)
210 Y=0
220 FOR M=3 TO P STEP 2
230   IF L/M-INT(L/M)=0 THEN LET Y=Y+1
240 NEXT M
250 IF Y=0 AND L>K THEN LET N=N+1 :PRINT,,USING"######";N;K;L
260 RETURN

Previous | Top | Home | Next