Ubasic Subroutines. C. Rivera, 2002
Strongpseudoprimes
From p.77 "Factorization and primality testing" D. M. Bressoud
200 *SPSP:PRIMO=0: PDQ=prmdiv(Q)
202 if Q=PDQ then PRIMO=1:goto 350
204 if and{1<PDQ,PDQ<Q} then goto 350
210 BB=irnd@11998+2
220 TT=Q-1:AA=0
230 for II=1 to 10000
240 if even(TT)=0 then cancel for:goto 270
250 TT=TT\2:AA=AA+1
260 next II
270 W=modpow(BB,TT,Q)
280 if or{W=1,W=Q-1} then PRIMO=1:goto 350
290 for II=1 to AA-1
300 W=(W*W)@Q
310 if W=Q-1 then PRIMO=1:cancel for:goto 350
320 next II
350 return
Reversible
900 *REVERSI:L=alen(N):NR=0
910 for I=L to 1 step -1:D=val(mid(str(N),I+1,1))
920 NR=NR*10+D:next I
930 return
900 *REVERSI:Z=N:LZ=alen(Z):ZR=0
910 for I=1 to LZ:ZR=ZR*10+Z@10:Z=Z\10:next I
920 return
Related issues:
a) LDZ=the leftmost digit of Z if LZ=alen(Z):
LDZ = Z\(10^(LZ-1))
b) KDZ =the kth digit of Z counting from left to right;
KDZ = (Z\(10^(LZ-K))@10
c) SODZ = sum of digits of Z if LZ=alen(Z)
900 *SODZ:Z=N:LZ=alen(Z):SODZ=0
910 for I=1 to LZ:SODZ=SODZ+Z@10:Z=Z\10:next I
920 return
d) Similar things can be made for the subroutines Pandigitals
Palindrome
600 *PALI
610 PAL=1:L=alen(Z)
620 if even(L)=1 then PAL=0:goto 690
630 for I=1 to L\2
640 if val(mid(str(Z),I+1,1))=val(mid(str(Z),L-I+2,1)) then goto 660
650 PAL=0:cancel for:goto 690
660 next I
690 return
Prime Factors
1000 *PFACTORS:M=N:I=1
1100 P=prmdiv(M):P(I)=P:E=1
1200 M=M\P:if M=1 then E(I)=E:K=I:return
1300 if prmdiv(M)=P then E=E+1:goto 120
1400 E(I)=E:I=I+1:goto 110
Sigma
500 *SIGMA:M=NN:I=1:SIGMA=1
510 P=prmdiv(M):P(I)=P:E=1
520 M=M\P:if M=1 then E(I)=E:K=I:SIGMA=SIGMA*(P^(E+1)-1)\(P-1):return
530 if prmdiv(M)=P then E=E+1:goto 520
540 E(I)=E:I=I+1:SIGMA=SIGMA*(P^(E+1)-1)\(P-1):goto 510
Nb10 --> Nb2, see http://www.tools4noobs.com/online_tools/base_convert/
900 'NB10MBB.ub
910 print "Convierte N base 10 en NB en base B. Meter N y B separados por coma":input
N,B
920 NN=N:NB=0
930 for I=1 to 5000
940 NB=NB+(NN@B)*10^(I-1)
950 NN=NN\B
960 if NN=0 then cancel for:goto 980
970 next I
980 print N;"base 10=";NB;"base";B
990 return
P=4k+1=a^2+b^2
500 print "Prime=4k+1=A^2+B^2,twosumcr.ub, 28/9/16"
510 print "Original subroutine 'twosum' by Professor Donald E. G. Malm"
520 print "Available at ftp://ftp.bu.edu/mirrors/simtelnet/msdos/ubasic/"
530 print "No attempt here to verify the primality of P"
535 '{P, (SQRTP, K1, K2, T, G, TE) -> A, B}
540 input P:if P@4<>1 then print "P is not 4k+1":goto 540
550 SQRTP=isqrt(P):if res=0 then print "P is a square":goto 540
560 clr time:K1=0:K2=0:T=1
570 T=T+1:K1=K1+1:if kro(T,P)<>-1 then goto 570
580 G=modpow(T,(P-1)\4,P):if P\2<G then G=P-G
590 A=P@G:if A=1 then B=G:goto 640
600 if SQRTP>=A then goto 620
610 TE=G:G=A:A=TE@G:K2=K2+1:goto 600
620 if A=0 then B=0:goto 640
630 B=G@A
640 print P;"=";A;"^2+";B;"^2"
650 print K1,K2,time
660 if P<>A*A+B*B then print "Check the primality of P":end
670 print "Result correct!!!":end
Pandigitals
1070 *PAN9:PAN9=0:F=1
1080 for I=1 to 9:D=val(mid(str(N),I+1,1)):if D=0 then cancel for:return
1090 if F@prm(D)=0 then cancel for:return
1100 F=F*prm(D):next I
1110 PAN9=1:return
1070 *PAN10:PAN10=0:F=1
1080 for I=1 to 10:D=val(mid(str(N),I+1,1)):
1090 if F@prm(D+1)=0 then cancel for:return
1100 F=F*prm(D+1):next I
1110 PAN10=1:return
Given Y find X&n such that Y=X^n
10 print "Given Y find X&E such that Y=X^E,2<=E<=10, YXE.ub"
20 print "input a value Y"
30 input Y
40 for E=2 to 10
50 X=int(Y^(1/E))
60 if Y=X^E then print Y,X,E,0:cancel for:goto 110
70 X=X+1
80 if Y=X^E then print Y,X,E,1:cancel for:goto 110
90 next E
100 print "Not found as X^E, 2<=E<=10"
110 end
Palindromes multiplying two pandigitals (1-9)
(An example of using .txt files as input and output)
10 P=123456789:K=0:JM=0:clr time:'pa3p9q9.ub
20 open "palip9q9.txt" for create as #1
30 open "pan9.txt" for input as #2
40 for I=1 to 362880
50 input #2,Q:Q=val(Q)
60 if Q>P then P=Q:close #2:cancel for:goto 30
70 C=P*Q:gosub *PALI
80 if PALI=1 then K=K+1:print #1,K;C;P;Q:print K;C;P;Q;time
90 next I
100 close:print time:end
200 *PALI:PALI=0:L=alen(C)
210 for J=1 to L\2
220 if val(mid(str(C),J+1,1))<>val(mid(str(C),L-J+2,1)) then cancel for:return
230 next J
240 PALI=1:return
Sum of digits
1000 *SOD
2000 S=0:M=N
3000 A=M@10:if A=M then S=S+A:print S:return
4000 S=S+A:M=M\10:goto 3000
Binary Sum
10 L=16:dim D(L)
20 gosub *ADDONE
500 *ADDONE:C=1
510 for I=L to 1 step -1
520 if C=0 then cancel for:return
530 if D(I)=0 then D(I)=1:C=0:S=S+1:goto 550
540 D(I)=0:C=1:S=S-1
550 next I
560 return
600 *IMPRBINSET:print S;":";
610 for I=1 to L:print D(I);:next I
620 print:return
Factorizar con algoritmos de SPF y Fermat
Ejemplo de outoput:
542364564562446241 is composite with some SPF (<131101) but the last is a
compo
site cofactor-Fermat algorithm is used for the last two factors. Check primality
of these last two
0:00:03 digits = 18
7 * 11 * 41 * 233 * 1819759 * 405179
10 dim P(100):gosub *LEGENDS:'factorizer.ub
20 'Complete factorization by SPF if N<17187472201 (11 digits)
30 'Fails is N=> 17187472201 and no prime factor less than 2^17
40 '17187472201=131101^2; 131101=nxtprm(2^17)
50 ' SPF+Fermat algorithm is suitable for composites with 20 or less digits
60 input N:clr time:if or{even(N)=1,alen(N)>20} then goto 60
70 gosub *FACTORIZER:end
80 *FACTORIZER:K=0:Q=N
90 gosub *SPSP:if PRIMO=1 then L=L1:goto 150
100 gosub *SPF
110 if M=1 then L=L2:goto 150
120 if M=N then L=L3:Q=N:gosub *FERMAT:goto 150
130 Q=M:gosub *SPSP:if PRIMO=1 then K=K+1:P(K)=Q:L=L4:goto 150
140 L=L5:Q=M:gosub *FERMAT
150 gosub *IMPR:return
160 *LEGENDS
170 L1="is prime"
180 L2="is composite factorizable with all SPF"
190 L3="is composite with any known factor"
200 L4="is composite with some SPF (<131101) and the last one a major
primecofactor"
210 L5="is composite with some SPF (<131101) but the last is a composite
cofactor":
220 L6="Fermat algorithm is used for the last two factors. Check primality of
these last two"
230 return
240 *SPF:M=N
250 P=prmdiv(M):if P=0 then return
260 K=K+1:P(K)=P:M=M\P:if M=1 then return
270 goto 250
280 *IMPR:print:print N;L:print time;" digits = ";alen(N):if or{L=L1,L=L3} then
goto 340
290 for I=1 to K-1:print P(I);"*";:next I
300 print P(K);
310 if L=L2 then print "(all SPF)":goto 340
320 if L=L4 then print "(last BPF)":goto 340
330 if L=L5 then print "(last BC)"
340 print
350 return
360 *SPSP:PRIMO=0
370 if Q=prmdiv(Q) then PRIMO=1:goto 510
380 if and{1<prmdiv(Q),prmdiv(Q)<Q} then goto 510
390 B=irnd@11998+2
400 T=Q-1:A=0
410 for II=1 to 10000
420 if even(T)=0 then cancel for:goto 450
430 T=T\2:A=A+1
440 next II
450 W=modpow(B,T,Q)
460 if or{W=1,W=Q-1} then PRIMO=1:goto 510
470 for II=1 to A-1
480 W=(W*W)@Q
490 if W=Q-1 then PRIMO=1:cancel for:goto 510
500 next II
510 return
520 *FERMAT
530 print "Looking factors by Fermat algorithm for ";Q
540 if alen(Q)>15 then print "For more than 15 digits it may take a long time to
end"
550 print "Do you want to continue (1) or stop(0)?":input SS
560 if SS=0 then end
570 if (isqrt(Q))^2=N then A=isqrt(Q):B=A:goto 660
580 SQR=isqrt(Q)
590 SQR=SQR+1:U=2*SQR+1:V=1:R=SQR^2-Q
600 while R<>0
610 if R>0 then goto 630
620 R=R+U:U=U+2:goto 640
630 R=R-V:V=V+2
640 wend
650 A=(U+V-2)\2:B=(U-V)\2
660 K=K+1:P(K)=A:K=K+1:P(K)=B:L=L+"-"+L6
670 return
Permutaciones de N objetos por algoritmo de Heap
10 'All the permutations of N elements by Heap algorithm
20 'reference https://es.wikipedia.org/wiki/Algoritmo_de_Heap
30 input N:dim A(N),C(N):clr time:K=0
40 for J=1 to N:A(J-1)=J:next J
50 clr block C(0..N-1)
60 K=K+1:gosub *PRINTA:
70 I=0
80 while I<N
90 if C(I)<I then if even(I)=1 then swap A(0),A(I) else swap A(C(I)),A(I):endif:K=K+1:gosub
*PRINTA:C(I)=C(I)+1:I=0 else C(I)=0:I=I+1:endif
100 wend
110 print time,K,!(N):end
200 *PRINTA
210 for J=1 to N:print A(J-1);:next J
220 print K:return
Ordenamiento por Inserción, en orden ascendente
10 'Sort by insertion, Increasing order.
sortins.ub
20 'https://es.wikipedia.org/wiki/Ordenamiento_por_inserci%C3%B3n
30 dim A(100):print "Sort by Insertion, by increasing order"
40 gosub *INPUTVECTOR2SORT
50 print "Vector Non-sorted":gosub *IMPR
60 gosub *2SORT
70 print "Vector sorted":gosub *IMPR:end
100 *INPUTVECTOR2SORT
110 input "Input the Qty of elements to sort<=100 to sort";K
120 for I=1 to K
130 input A
135 A(I)=A:next
140 return
200 *IMPR:print K;"elements"
210 for I=1 to K:print A(I);:next I
220 print:return
300 *2SORT
310 for I=1 to K
320 VALUE=A(I)
330 for J=I-1 to 1 step -1
340 if A(J)>VALUE then A(J+1)=A(J):goto 360
350 cancel for:goto 370
360 next J
370 A(J+1)=VALUE:next I
380 return
Spelling integers in spanish
10 dim A(30),T(4):NMAX=2^32-20
20 input N:if N>NMAX then end
30 gosub *SPELLN:print S:end
40 'gosub *COUNTS
50 'gosub *TESTCOUNTS
60 'if T=1 then print N,S
70 'goto 20
99 '***********************************
100 *SPELLN:S=""
110 gosub *TN
120 for I=K to 1 step -1
130 Z=T(I):gosub *SPELLZ:S=S+ZS
140 next I
150 print N,S:return
199 '**********************************
200 *TN:M=10^10+N
210 for I=1 to 4
220 Z=M-(M\1000)*1000
230 if Z=0 then K=I-1:cancel for:return
240 T(I)=Z:M=M\1000
250 next I
260 return
299 '*********************************
300 *SPELLZ:ZZ=1000+Z
310 C=val(mid(str(ZZ),3,1)):D=val(mid(str(ZZ),4,1));U=val(mid(str(ZZ),5,1)):DU=D*10+U
319 if Z=100 then CS="cien":goto 330
320 if C=0 then CS="":goto 330
321 if C=1 then CS="ciento":goto 330
322 if C=2 then CS="doscientos":goto 330
323 if C=3 then CS="trescientos":goto 330
324 if C=4 then CS="cuatrocientos":goto 330
325 if C=5 then CS="quinientos":goto 330
326 if C=6 then CS="seiscientos":goto 330
327 if C=7 then CS="setecientos":goto 330
328 if C=8 then CS="ochocientos":goto 330
329 if C=9 then CS="novecientos":goto 330
330 if DU=10 then DS="diez":goto 350
331 if DU=11 then DS="once":goto 350
332 if DU=12 then DS="doce":goto 350
333 if DU=13 then DS="trece":goto 350
334 if DU=14 then DS="catorce":goto 350
335 if DU=15 then DS="quince":goto 350
336 if DU=20 then DS="veinte":goto 350
340 if D=0 then DS="":goto 350
341 if D=1 then Ds="dieci":goto 350
342 if D=2 then Ds="veinti":goto 350
343 if D=3 then Ds="treinta":goto 350
344 if D=4 then Ds="cuarenta":goto 350
345 if D=5 then Ds="cincuenta":goto 350
346 if D=6 then Ds="sesenta":goto 350
347 if D=7 then Ds="setenta":goto 350
348 if D=8 then Ds="ochenta":goto 350
349 if D=9 then Ds="noventa":goto 350
350 if and{D>2,DU>0} then YS=" y " else YS=""
360 if or{I=2,I=4} then goto 409
370 if I=1 then goto 390
378 if and{DU>9,DU<16} then US="":goto 500
379 if U=1 then US="un":goto 500
380 if U=0 then US="":goto 500
381 if U=1 then US="uno":goto 500
382 if U=2 then US="dos":goto 500
383 if U=3 then US="tres":goto 500
384 if U=4 then US="cuatro":goto 500
385 if U=5 then US="cinco":goto 500
386 if U=6 then US="seis":goto 500
387 if U=7 then US="siete":goto 500
388 if U=8 then US="ocho":goto 500
389 if U=9 then US="nueve":goto 500
390 if and{DU>9,DU<16} then US="":goto 500
391 if U=0 then US="":goto 500
392 if U=1 then US="uno":goto 500
393 if U=2 then US="dos":goto 500
394 if U=3 then US="tres":goto 500
395 if U=4 then US="cuatro":goto 500
396 if U=5 then US="cinco":goto 500
397 if U=6 then US="seis":goto 500
398 if U=7 then US="siete":goto 500
399 if U=8 then US="ocho":goto 500
400 if U=9 then US="nueve":goto 500
409 if or{Z=1,and{DU>9,DU<16}} then US="":goto 500
410 if U=0 then US="":goto 500
411 if U=1 then US="uno":goto 500
412 if U=2 then US="dos":goto 500
413 if U=3 then US="tres":goto 500
414 if U=4 then US="cuatro":goto 500
415 if U=5 then US="cinco":goto 500
416 if U=6 then US="seis":goto 500
417 if U=7 then US="siete":goto 500
418 if U=8 then US="ocho":goto 500
419 if U=9 then US="nueve":goto 500
500 if I=1 then APS="":goto 600
510 if and{I=2,Z=0} then APS="" goto 600
511 if I=2 then APS="mil":goto 600
520 if and{I=3,Z=0} then APS="":goto 600
521 if and{I=3,T(4)=0,Z=1} then APS="millon":goto 600
522 if I=3 then APS="millones":goto 600
523 if and{I=4,Z=0} then APS="":goto 600
524 if I=4 then APS="mil"
600 ZS=CS+DS+YS+US+APS:return
699 '***************************
Selfness Test by Kaprekar Effective Test
10 'Selfness Test.selftst1.ub
15 print "https://en.wikipedia.org/wiki/Self_number"
20 print "Effective Kaprekar Test"
25 print "n=self if SOD(|n-DR*(n)-9*i|)<>(DR*(n)+9*i), i=0 to d(n)"
30 print "DR*(n)={DR(n)/2, (DR(n)+9)/2}, if DR(n) is even or odd"
35 print "DR(n)=1+(n-1)@9, d(n)=alen(N)"
40 input "Enter an integer";N:clr time:LN=alen(N):print N;LN
50 gosub *SELFNESS
60 print "DR(n)=";DRN;"DR*(n)=";DDRN,time:end
100 *SELFNESS:LN=alen(N)
110 DRN=1+((N-1)@9):if odd(DRN)=1 then goto 130
120 DDRN=DRN\2:goto 140
130 DDRN=(DRN+9)\2
140 for I=0 to LN
150 Z=abs(N-DDRN-9*I):gosub *SODZ:Z1=SODZ
160 Z2=DDRN+9*I
170 if Z1=Z2 then cancel for:print "is NOT a Self integer. Fails with i=";I:return
180 next I
190 print "This is a Self Integer!!!":return
200 *SODZ:LZ=alen(Z):SODZ=0
210 for J=1 to LZ:SODZ=SODZ+val(mid(str(Z),J+1,1)):next J
220 return
Selfness Reduction Test for integers type A*10^B+C
10 'Selfness Test.selftst2.ub
15 print "https://en.wikipedia.org/wiki/Self_number"
20 print "Reduction Test for Q=A*10^B+C"
25 print "Q=self iff and{m1=C-SOD(A),m2=SOD(A-1)+9*B-(C+1)}={self or negative}"
40 input "Enter A,B,C";A,B,C:clr time:Q=A*10^B+C:print A,B,C
50 gosub *SELFNESS2:print time:end
100 *SELFNESS2
110 Z=A:gosub *SODZ:SODA=SODZ:M1=C-SODA
120 Z=A-1:gosub *SODZ:SODA1=SODZ:M2=SODA1+9*B-C-1
130 N=M1:gosub *SELFNESS1:if T=0 then print Q;"is NOT SELFM1":return
140 N=M2:gosub *SELFNESS1:if T=0 then print Q;"is NOT SELFM2":return
150 print Q;"Is SELF!!!":return
200 *SELFNESS1:LN=alen(N):T=0:if N<0 then T=1:return
210 DRN=1+((N-1)@9):if odd(DRN)=1 then goto 230
220 DDRN=DRN\2:goto 240
230 DDRN=(DRN+9)\2
240 for I=0 to LN
250 Z=abs(N-DDRN-9*I):gosub *SODZ:Z1=SODZ
260 Z2=DDRN+9*I
270 if Z1=Z2 then cancel for:return
280 next I
290 T=1:return
300 *SODZ:LZ=alen(Z):SODZ=0
310 for J=1 to LZ:SODZ=SODZ+val(mid(str(Z),J+1,1)):next J
320 return
Commas sequence
10 'The Commma's sequence, A121805. commas01.ub
20 K=1:C=1
30 print K,C
40 gosub *NEXTC:if NC=0 then end
50 K=K+1:C=NC:goto 30
100 *NEXTC
110 DC=C@10*10
120 for I=1 to 9
130 DC=DC+1:NC=C+DC
150 if val(mid(str(NC),2,1))=DC@10 then cancel for:return
160 next I
170 NC=0:return
N>0 as algebraic sum of the first K primes.
10 print "Suma algebraica de los primeros k primos, igual a un entero
N":'sumpton2.ub
20 Q=0:R=0:T=0
30 print "Introduzca entero N=>0":input N:if N<0 then goto 30
40 if N=6 then PMAX=5:S=10:Q=2:gosub *IMPR:end
50 gosub *PMAX:gosub *GBACH:gosub *IMPR:end
60 *PMAX:S=0:P=1
70 P=nxtprm(P):S=S+P
80 if or{S<N,odd(S)<>odd(N)} then goto 70
90 if S=N then PMAX=P:return
100 SD=(S-N)\2
110 if odd(SD)=1 then goto 140
120 if SD<8 then goto 70
130 PMAX=P:return
140 if SD<19 then goto 70
150 PMAX=P:return
160 *GBACH:F=1
170 if S=N then return
180 if odd(SD)=1 then goto 200
190 E=SD:goto 230
200 if SD-2>PMAX then goto 220
210 if (SD-2)=prmdiv(SD-2) then Q=2:R=SD-2:return
220 E=SD-3:Q=3:F=3
230 for I=2 to 12000:if F@prm(I)=0 then goto 270
240 if prm(I)>=E\2 then print N,S,SD,PMAX,"Check code":end
250 if E-prm(I)>PMAX then goto 270
260 if (E-prm(I))=prmdiv(E-prm(I)) then R=prm(I):T=E-prm(I):cancel
for:return
270 next I
280 print N,S,SD,PMAX,"Check code":end
290 *IMPR
300 print N;"={2+...+";PMAX;"}-2*{";Q;"+
310 return

***

Fibonacci Calculator (Free of overflows in Ubasic)
10 'Calculador
Fibonacci.cr254d.ub
20 print "Calculador Fibonacci":FI=(1+sqrt(5))/2
30 print "If you have an index and want the Fib, press 1"
40 print "If you have an Fib, or an integer value, and want the Index,
press 2"
50 print "Or press zero to quit this program"
60 input S
70 if S=1 then gosub *INDEX:goto 20
80 if S=2 then gosub *FIB:goto 20
90 end
100 *INDEX
110 print "Input the 3<=IND<=12306 for which you want to know the FIB"
120 input IND:if or{IND<3,IND>12306} then goto 110
130 print "Approx. FIB =";int(FI^IND/sqrt(5)+1/2),
135 print "FIB qty. of digits=";ceil((IND*log(FI)-log(
140 F1=1F2=1:K=2
150 F3=F1+F2:K=K+1
160 if K=IND then print "Exact FIB=";F3;alen(F3);IND:return
170 F1=F2:F2=F3:goto 150
180 *FIB
190 print "Input your 3<=FIB integer<=10^2590, for which you want to know
its Index"
200 input FIB:if or{FIB<3,FIB>10^2590} then goto 190
205 if FIB<=10^2590 then print "Approx. IND=";int(log(FIB*sqrt(5)+1/2)
210 if FIB>10^1300 then print "Fibonacciness Test omitted":goto 260
220 print:print "Test of Fibonacciness"
230 X1=5*FIB^2+4:X2=X1-8:RX1=
240 if or{RX1*RX1=X1,RX2*RX2=X2} then print FIB;alen(FIB);"certainly IS
Fibonacci":goto 260
250 print FIB;alen(FIB);"IS NOT Fibonacci"
260 stop:F1=1:F2=1:K=2
270 F3=F1+F2:K=K+1
280 if F3<FIB then F1=F2:F2=F3:goto 270
290 print "Closest real values are":print K-1,F2,alen(F2):print K,F3,alen(F3):print
K+1,F2+F3,alen(F2+F3)
300 return
Partitioning the first N=odd>1 primes in two disjoint sets, each with the same sum of primes
10 'Partitioning the first N primes, N>1, odd in two disjoint sets:'ivan004.ub
20 'with the same sum of primes for each.
25 '*************************
30 input "Enter the qty of primes to partition, an odd integer =>3:";N
40 if or{odd(N)<>1,N<3} then goto 30
45 '*************************
50 gosub *SKP2
60 print SKP2;"is the semisum of all the";K; "primes from 2 to";P2
65 '*************************
70 S=0:P=P2:R=SKP2:print:print "Set A = {";
75 '*************************
80 R=R-P
90 if and{R<>4,R<>6} then goto 110
100 Z=P2:gosub *PREVIOUSP:P2=PZ:print R;"Attempt aborted":goto 70
105 '************************
110 if R=0 then S=S+P:print P;"};S=";S;"Eureka!":goto 160
120 if and{R>1,R=prmdiv(R),R<P} then S=S+P+R:print P;R;"};S=";S;"Eureka!":goto
160
125 '*************************
130 if R<2 then R=R+P:goto 150
140 S=S+P:print P;
145 '*************************
150 Z=P:gosub *PREVIOUSP:P=PZ:goto 80
155 '*************************
160 if S<>SKP2 then print "Error in the sum":stop
170 N=N+2:goto 50
175 '*************************
200 *PREVIOUSP
210 if Z=2 then then print "Solution not found!!!":end
220 if Z=3 then PZ=2:return
230 Z=Z-2
240 if and{Z>1,Z=prmdiv(Z)} then PZ=Z:return
250 goto 230
255 '************************
300 *SKP2:SKP=0:K=0:P=1
310 P=nxtprm(P):K=K+1:SKP=SKP+P
320 if K<N then goto 310
330 P2=P:SKP2=SKP\2:return
Examples:
530 is the semisum of all the 25 primes from 2 to 97
Set A = { 97 89 83 79 73 71 31 7 };S= 530 Eureka!
740 is the semisum of all the 29 primes from 2 to 109
Set A = { 109 107 103 101 97 89 83 4 Attempt aborted
Set A = { 107 103 101 97 89 83 79 73 5 3 };S= 740 Eureka!
2219 is the semisum of all the 47 primes from 2 to 211
Set A = { 211 199 197 193 191 181 179 173 167 163 157 151 4 Attempt
aborted
Set A = { 199 197 193 191 181 179 173 167 163 157 151 149 6 Attempt
aborted
Set A = { 197 193 191 181 179 173 167 163 157 151 149 139 137 37 5
};S= 2219 Eureka!
