AKS test for primes: Difference between revisions

Content deleted Content added
m →‎{{header|J}}: whitespace in code
Hansoft (talk | contribs)
Moved uBasic/4tH version
Line 93:
 
Function maxes out at i = 61 as AutoHotkey supports up to 64-bit signed integers.
=={{header|BASIC}}==
{{works with|uBasic/4tH}}
<lang ubasic>For n = 0 To 9
Push n : Gosub _coef : Gosub _drop
Print "(x-1)^";n;" = ";
Push n : Gosub _show
Print
Next
 
Print
Print "primes (never mind the 1):";
 
For n = 1 To 34
Push n : Gosub _isprime
If Pop() Then Print " ";n;
Next
 
Print
End
 
' show polynomial expansions
_show ' ( n --)
Do
If @(Tos()) > -1 Then Print "+";
Print @(Tos());"x^";Tos();
While (Tos())
Push Pop() - 1
Loop
 
Gosub _drop
Return
 
' test whether number is a prime
_isprime ' ( n --)
Gosub _coef
 
i = Tos()
@(0) = @(0) + 1
@(i) = @(i) - 1
 
 
Do While (i) * ((@(i) % Tos()) = 0)
i = i - 1
Loop
 
Gosub _drop
Push (i = 0)
Return
 
' generate coefficients
_coef ' ( n -- n)
If (Tos() < 0) + (Tos() > 34) Then End
' gracefully deal with range issue
i = 0
@(i) = 1
 
Do While i < Tos()
j = i
@(j+1) = 1
 
Do While j > 0
@(j) = @(j-1) - @(j)
j = j - 1
Loop
 
@(0) = -@(0)
i = i + 1
Loop
Return
 
' drop a value from the stack
_drop ' ( n --)
If Pop() Endif
Return</lang>
{{out}}
<pre>
(x-1)^0 = +1x^0
(x-1)^1 = +1x^1-1x^0
(x-1)^2 = +1x^2-2x^1+1x^0
(x-1)^3 = +1x^3-3x^2+3x^1-1x^0
(x-1)^4 = +1x^4-4x^3+6x^2-4x^1+1x^0
(x-1)^5 = +1x^5-5x^4+10x^3-10x^2+5x^1-1x^0
(x-1)^6 = +1x^6-6x^5+15x^4-20x^3+15x^2-6x^1+1x^0
(x-1)^7 = +1x^7-7x^6+21x^5-35x^4+35x^3-21x^2+7x^1-1x^0
(x-1)^8 = +1x^8-8x^7+28x^6-56x^5+70x^4-56x^3+28x^2-8x^1+1x^0
(x-1)^9 = +1x^9-9x^8+36x^7-84x^6+126x^5-126x^4+84x^3-36x^2+9x^1-1x^0
 
primes (never mind the 1): 1 2 3 5 7 11 13 17 19 23 29 31
</pre>
 
=={{header|Bracmat}}==
Bracmat automatically normalizes symbolic expressions with the algebraic binary operators <code>+</code>, <code>*</code>, <code>^</code> and <code>\L</code> (logartithm). It can differentiate such expressions using the <code>\D</code> binary operator. (These operators were implemented in Bracmat before all other operators!). Some algebraic values can exist in two evaluated forms. The equivalent <code>x*(a+b)</code> and <code>x*a+x*b</code> are both considered "normal", but <code>x*(a+b)+-1</code> is not, and therefore expanded to <code>-1+a*x+b*x</code>. This is used in the <code>forceExpansion</code> function to convert e.g. <code>x*(a+b)</code> to <code>x*a+x*b</code>.