Ubasic Subroutines. C. Rivera, 2002
Strongpseudoprimes
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 B=irnd@11998+2
220 T=Q-1:A=0
230 for II=1 to 10000
240 if even(T)=0 then cancel for:goto 270
250 T=T\2:A=A+1
260 next II
270 W=modpow(B,T,Q)
280 if or{W=1,W=Q-1} then PRIMO=1:goto 350
290 for II=1 to A-1
300 W=(W*W)@Q
310 if W=Q-1 then PRIMO=1:cancel for:goto 350
320 next II
350 return
Reversible
800 *REVERSI:NR=0
810 D=N-10*(N\10):NR=NR*10+D:N=N\10
820 if N>10 then goto 810
830 NR=NR*10+N:return
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
910 *NB10NB2:input N
920 B=2: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 80
970 next I
980 print N;"base 10=";NB;"base";B
990 return
P=4k+1=a^2+b^2
500 input P:'prime:P=4k+1=a^2+b^2.'PSUM2SQR.ub
510 if P@4<>1 then print P;"is wrong. It is not 4k+1":end
520 Sqrtp=isqrt(P):if (res=0) then print P;"is a square of";Sqrtp:end
530 T=1:repeat inc T until kro(T,P)=-1
540 G=modpow(T,(P-1)\4,P):if P\2<G then G=P-G endif
550 A=P@G
560 if A=1 then B=G else
570 :while Sqrtp<A:Te=G:G=A:A=Te@G wend
580 :if A=0 then B=0 else:B=G@A endif endif
590 print P;"=";A;"^2+";B;"^2":end
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