Ulam spiral (for primes): Difference between revisions

no edit summary
m (syntax highlighting fixup automation)
No edit summary
Line 2,941:
██ ██ ██ ██ ██
</pre>
=={{header|M2000 Interpreter}}==
{{trans|VBScript}}
<syntaxhighlight lang="m2000 interpreter">
Module Ulam_Spiral {
build_spiral(9)
Sub build_spiral(n)
Local matrix(n,n) as string
Local x = (n-1)/2, y = (n-1)/2
Local x_max = 1, y_max = 1, count = 1
Local dir = "R", i, l=Len(n*n+"")
For i = 1 To n*n
If @IsPrime(i) Then
matrix(x,y) = Right$("000"+i,l)
Else
matrix(x,y) = String$("-", l) // this is different from VbScript
End If
Select Case dir
Case "R"
{
If x_max > 0 Then
x++:x_max--
Else
dir = "U" : y_max = count
y--:y_max--
End If
}
Case "U"
{
If y_max > 0 Then
y-- :y_max--
Else
dir = "L" : count++: x_max = count
x--:x_max--
End If
}
Case "L"
{
If x_max > 0 Then
x--:x_max--
Else
dir = "D" : y_max = count
y++:y_max--
End If
}
Case "D"
{
If y_max > 0 Then
y++:y_max--
Else
dir = "R" : count++: x_max = count
x++:x_max--
End If
}
End Select
Next
For y = 0 To n - 1
For x = 0 To n - 1
If x = n - 1 Then
Print matrix(x,y)
Else
print matrix(x,y)+" ";
End If
Next
print
Next
End Sub
Function IsPrime(n)
If n = 2 Then
= True
Else.If n <= 1 Or n Mod 2 = 0 Then
= False
Else
= True
local i
For i = 3 To Int(Sqrt(n)) Step 2
If n Mod i = 0 Then
= False
Exit For
End If
Next
End If
End Function
}
Ulam_Spiral
</syntaxhighlight>
{{out}}
same as VBScript
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
404

edits