AKS test for primes: Difference between revisions
Content added Content deleted
Line 2,139: | Line 2,139: | ||
{{works with|Just BASIC|any}} |
{{works with|Just BASIC|any}} |
||
<lang lb> |
<lang lb> |
||
global pasTriMax |
|||
pasTriMax = 61 |
|||
dim pasTri(pasTriMax + 1) |
|||
for n = 0 to 9 |
|||
call expandPoly n |
|||
next n |
|||
for n = 2 to pasTriMax |
|||
if isPrime(n) <> 0 then |
|||
print using("###", n); |
|||
end if |
|||
next n |
|||
print |
|||
end |
|||
sub expandPoly n |
|||
n = int(n) |
|||
dim vz$(1) |
|||
vz$(0) = "+" |
|||
vz$(1) = "-" |
|||
if n > pasTriMax then |
|||
print n; " is out of range" |
|||
end |
|||
end if |
|||
select case n |
|||
case 0 |
|||
print "(x-1)^0 = 1" |
|||
case 1 |
|||
print "(x-1)^1 = x-1" |
|||
case else |
|||
call pascalTriangle n |
|||
print "(x-1)^"; n; " = "; |
|||
print "x^"; n; |
|||
bVz = 1 |
|||
nDiv2 = int(n / 2) |
|||
for j = n - 1 to nDiv2 + 1 step -1 |
|||
print vz$(bVz); pasTri(n - j); "*x^"; j; |
|||
bVz = abs(1 - bVz) |
|||
next j |
|||
for j = nDiv2 to 2 step -1 |
|||
print vz$(bVz); pasTri(j); "*x^"; j; |
|||
bVz = abs(1 - bVz) |
|||
next j |
|||
print vz$(bVz); pasTri(1); "*x"; |
|||
bVz = abs(1 - bVz) |
|||
print vz$(bVz); pasTri(0) |
|||
end select |
|||
end sub |
|||
function isPrime(n) |
|||
n = int(n) |
|||
if n > pasTriMax then |
|||
print n; " is out of range" |
|||
end |
|||
end if |
|||
call pascalTriangle n |
|||
res = 1 |
|||
i = int(n / 2) |
|||
while res and (i > 1) |
|||
res = res and (pasTri(i) mod n = 0) |
|||
i = i - 1 |
|||
wend |
|||
isPrime = res |
|||
end function |
|||
sub pascalTriangle n |
|||
rem Calculate the n'th line 0.. middle |
|||
n = int(n) |
|||
pasTri(0) = 1 |
|||
j = 1 |
|||
while j <= n |
|||
j = j + 1 |
|||
k = int(j / 2) |
|||
pasTri(k) = pasTri(k - 1) |
|||
for k = k to 1 step -1 |
|||
pasTri(k) = pasTri(k) + pasTri(k - 1) |
|||
next k |
|||
wend |
|||
end sub |
|||
</lang> |
</lang> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
(x-1)^0 = 1 |
|||
(x-1)^1 = x-1 |
|||
(x-1)^2 = x^2-2*x+1 |
|||
(x-1)^3 = x^3-3*x^2+3*x-1 |
|||
(x-1)^4 = x^4-4*x^3+6*x^2-4*x+1 |
|||
(x-1)^5 = x^5-5*x^4+10*x^3-10*x^2+5*x-1 |
|||
(x-1)^6 = x^6-6*x^5+15*x^4-20*x^3+15*x^2-6*x+1 |
|||
(x-1)^7 = x^7-7*x^6+21*x^5-35*x^4+35*x^3-21*x^2+7*x-1 |
|||
(x-1)^8 = x^8-8*x^7+28*x^6-56*x^5+70*x^4-56*x^3+28*x^2-8*x+1 |
|||
(x-1)^9 = x^9-9*x^8+36*x^7-84*x^6+126*x^5-126*x^4+84*x^3-36*x^2+9*x-1 |
|||
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 |
|||
</pre> |
</pre> |
||