RosettaCodeData/Task/AKS-test-for-primes/Yabasic/aks-test-for-primes.basic

86 lines
1.8 KiB
Plaintext

// Does not work for primes above 53, which is actually beyond the original task anyway.
// Translated from the C version, just about everything is (working) out-by-1, what fun.
dim c(100)
sub coef(n)
local i
// out-by-1, ie coef(1)==^0, coef(2)==^1, coef(3)==^2 etc.
c(n) = 1
for i = n-1 to 2 step -1
c(i) = c(i) + c(i-1)
next
end sub
sub is_prime(n)
local i
coef(n+1) // (I said it was out-by-1)
for i = 2 to n-1 // (technically "to n" is more correct)
if int(c(i)/n) <> c(i)/n then
return 0
end if
next
return 1
end sub
sub show(n)
// (As per coef, this is (working) out-by-1)
local ci, ci$, i
for i = n to 1 step -1
ci = c(i)
if ci = 1 then
if mod(n-i, 2) = 0 then
if i = 1 then
if n = 1 then
ci$ = "1"
else
ci$ = "+1"
end if
else
ci$ = ""
end if
else
ci$ = "-1"
end if
else
if mod(n-i, 2) = 0 then
ci$ = "+" + str$(ci)
else
ci$ = "-" + str$(ci)
end if
end if
if i = 1 then // ie ^0
print ci$;
elsif i=2 then // ie ^1
print ci$, "x";
else
print ci$, "x^", i-1;
end if
next
end sub
sub AKS_test_for_primes()
local n
for n = 1 to 10 // (0 to 9 really)
coef(n)
print "(x-1)^", n-1, " = ";
show(n)
print
next
print "\nprimes (<=53): ";
c(2) = 1 // (this manages "", which is all that call did anyway...)
for n = 2 to 53
if is_prime(n) then
print " ", n;
end if
next
print
end sub
AKS_test_for_primes()