Ulam spiral (for primes): Difference between revisions
m
→{{header|Wren}}: Minor tidy
m (→{{header|Wren}}: Minor tidy) |
|||
(18 intermediate revisions by 7 users not shown) | |||
Line 484:
<> <> <>
<> <> </pre>
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
<syntaxhighlight lang="algol68">
BEGIN # construct a Ulam spiral for primes #
PR read "primes.incl.a68" PR # include prime utilities #
# prints a Ulam spiral for primes with the specified width, #
# starting from init. Primes will be indicated by prime char and #
# non-primes by composite char. The center of the spiral will be #
# indicated by center char - unless the center value is prime #
PROC print ulam spiral for primes = ( INT width, init, CHAR prime char, composite char, center char )VOID:
IF width >= 1 THEN
INT n = IF ODD width THEN width ELSE width + 1 FI;
[]BOOL prime = PRIMESIEVE ( init + ( n * n ) );
[ 1 : n, 1 : n ]CHAR spiral; FOR i TO n DO FOR j TO n DO spiral[ i, j ] := "_" OD OD;
INT y := ( n + 1 ) OVER 2;
INT x := y;
INT v := init;
spiral[ x, y ] := IF prime[ init ] THEN prime char ELSE center char FI;
INT w := 0;
WHILE x < 1 UPB spiral DO
w +:= 2;
x +:= 1;
y +:= 1;
FOR i TO w DO # right edge #
spiral[ x, y -:= 1 ] := IF prime[ v +:= 1 ] THEN prime char ELSE composite char FI
OD;
FOR i TO w DO # top edge #
spiral[ x -:= 1, y ] := IF prime[ v +:= 1 ] THEN prime char ELSE composite char FI
OD;
FOR i TO w DO # left edge #
spiral[ x, y +:= 1 ] := IF prime[ v +:= 1 ] THEN prime char ELSE composite char FI
OD;
FOR i TO w DO # bottom edge #
spiral[ x +:= 1, y ] := IF prime[ v +:= 1 ] THEN prime char ELSE composite char FI
OD
OD;
FOR v pos TO width DO
FOR h pos TO width DO
print( ( spiral[ h pos, v pos ] ) )
OD;
print( ( newline ) )
OD
FI # ulam spiral for primes # ;
print ulam spiral for primes( 35, 1, "#", " ", "+" )
END
</syntaxhighlight>
{{out}}
<pre>
# # # #
# # # # #
# # # #
# # # # #
# # # # #
# # # # # #
# # # # #
# # # # #
# # # # # # # #
# # # # # #
# # # # #
# # # # #
# # # # # # # #
# # #
# # # # # # # # # #
# # # # # # # # # #
# # # #
# # +## # # # # # #
# # # #
# #
# # # # # # # # # # #
# # # # # # #
# #
# # # # # #
# # # # #
# # # # #
# # # # # #
# # # # # # # # #
# # # #
# # # # # #
# # # # # #
# # # # #
# # # # #
# # # #
# # # # #
</pre>
=={{header|C}}==
Line 1,275 ⟶ 1,361:
img.savePGM("ulam_spiral.pgm");
}</syntaxhighlight>
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
[[File:DelphiUlamPrimeSpiral.png|frame|none]]
<syntaxhighlight lang="Delphi">
procedure DrawMatrixPrimes(Image: TImage; Mat: TMatrix);
{Display spiral, only marking cells that contain prime numbers}
var X,Y: integer;
var S: string;
var Size,Step: integer;
var Off: TSize;
var R: TRect;
begin
{Calculate size of grid}
Size:=Min(Image.Width,Image.Height);
Step:=Size div Length(Mat);
{Draw border rectangle}
Image.Canvas.Brush.Color:=clGreen;
Image.Canvas.Pen.Width:=4;
Image.Canvas.Rectangle(2,2,Length(Mat)*Step,Length(Mat)*Step);
{Setup font}
Image.Canvas.Font.Name:='Arial';
Image.Canvas.Font.Style:=[fsBold];
Image.Canvas.Font.Size:=14;
{Draw grid}
Image.Canvas.Pen.Width:=1;
{Draw vertical lines}
for X:=0 to Length(Mat) do
begin
Image.Canvas.MoveTo(X*Step,0);
Image.Canvas.LineTo(X*Step,Step*Length(Mat));
end;
{Draw horizontal lines}
for Y:=0 to Length(Mat) do
begin
Image.Canvas.MoveTo(0,Y*Step);
Image.Canvas.LineTo(Step*Length(Mat),Y*Step);
end;
{Label cells that contain primes}
for Y:=0 to High(Mat[0]) do
for X:=0 to High(Mat) do
if IsPrime(trunc(Mat[X,Y])) then
begin
{Color cells}
R:=Rect((X*Step)+2,(Y*Step)+2,X*Step+Step,Y*Step+Step);
InflateRect(R,-1,-1);
Image.Canvas.Pen.Width:=4;
Image.Canvas.Pen.Color:=clBlue;
Image.Canvas.Brush.Color:=clLime;
Image.Canvas.Rectangle(R);
{Label cell}
S:=Format('%0.0f',[Mat[X,Y]]);
Off:=Image.Canvas.TextExtent(S);
Off.CX:=(Step-Off.CX) div 2;
Off.CY:=(Step-Off.CY) div 2;
Image.Canvas.TextOut(X*Step+Off.CX,Y*Step+Off.CY,S);
end;
Image.Invalidate;
end;
procedure MakeSqrSpiralMatrix(var Mat: TMatrix; MatSize: integer);
{Create a spiral matrix of specified size}
var Inx: integer;
var R: TRect;
procedure DoTopRect(Off1,Off2: integer);
{Do top part of rectangle}
var X,Y: integer;
begin
for X:=R.Left+Off1 to R.Right+Off2 do
begin
Mat[X,R.Top]:=Inx;
Dec(Inx);
end;
end;
procedure DoRightRect(Off1,Off2: integer);
{Do Right part of rectangle}
var X,Y: integer;
begin
for Y:=R.Top+Off1 to R.Bottom+Off2 do
begin
Mat[R.Right,Y]:=Inx;
Dec(Inx);
end;
end;
procedure DoBottomRect(Off1,Off2: integer);
{Do bottom part of rectangle}
var X,Y: integer;
begin
for X:= R.Right+Off1 downto R.Left+Off2 do
begin
Mat[X,R.Bottom]:=Inx;
Dec(Inx);
end;
end;
procedure DoLeftRect(Off1,Off2: integer);
{Do left part of rectangle}
var X,Y: integer;
begin
for Y:=R.Bottom+Off1 downto R.Top+Off2 do
begin
Mat[R.Left,Y]:=Inx;
Dec(Inx);
end;
end;
procedure DoRect(R: TRect; var Inx: integer);
{Create one rotation of spiral around the rectangle}
begin
{The orientation of spiral is based in the size}
if (MatSize and 1)=0 then
begin
{Handle even sizes}
DoTopRect(0,0);
DoRightRect(1,0);
DoBottomRect(-1,0);
DoLeftRect(-1,1);
end
else
begin
{Handle odd sizes}
DoBottomRect(0,0);
DoLeftRect(-1,0);
DoTopRect(1,0);
DoRightRect(1,-1);
end
end;
begin
{Set matrix size}
SetLength(Mat,MatSize,MatSize);
{create matching rectangle}
R:=Rect(0,0,MatSize-1,MatSize-1);
Inx:=MatSize*MatSize;
{draw spiral around retangle and deflate rectanle until spiral is done}
while (R.Left<=R.Right) and (R.Top<=R.Bottom) do
begin
DoRect(R,Inx);
InflateRect(R,-1,-1);
end;
end;
procedure UlamPrimeSpiral(Image: TImage);
var Mat: TMatrix;
begin
MakeSqrSpiralMatrix(Mat,9);
DrawMatrixPrimes(Image,Mat);
end;
</syntaxhighlight>
{{out}}
<pre>
Elapsed Time: 1.550 ms.
</pre>
=={{header|EasyLang}}==
[https://easylang.dev/show/#cod=bZHdUsMgEIXveYqd8c5OmdTaC2eap0mIoglEIDX49LILi0w1F4HDfucsP9NmBtB+dXoBsy0gBQDoieZXeEKVPqfC5gx0KDMBfS5+velZJXntwX+6gL7iKSGLHYnuyvJdGgdS5qGHU21RqJOQwgAW9vS/dCLmYdxpcYyU/QBidXYAssh6igLlfBLclHxUUTOCsQVjC6Lr2JC/+t9MRr1qkb/RMp2LtuyDWnnLpl7BjrNkfcwykowsF3tTCYnlmPx+TaNBuyE9TCcv3CyoPXj9reAsqn02r3x9k3XwAcHC8ws6cFc4InEob42XiyOi74hi8VhbskUyo5E5lyqb7/xlqbFzAu75Bw== Run it]
<syntaxhighlight>
func isprim num .
if num < 2
return 0
.
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
n = 1
x = 50
y = 50
dx = 1
dy = 0
#
proc turn . .
if dx = 1
dx = 0
dy = 1
elif dy = 1
dy = 0
dx = -1
elif dx = -1
dx = 0
dy = -1
else
dx = 1
dy = 0
.
.
proc step . .
n += 1
x += dx * 1
y += dy * 1
move x y
if isprim n = 1
circle 0.5
.
.
textsize 3
move x y
lng = 0
#
for k to 49
step
lng += 2
turn
for j to lng - 1
step
.
for i to 3
turn
for j to lng
step
.
.
.
</syntaxhighlight>
=={{header|EchoLisp}}==
Line 1,991 ⟶ 2,327:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Ulam_spiral}}
'''Solution'''
[[File:Fōrmulæ - Ulam spiral 01.png]]
'''Test case'''
[[File:Fōrmulæ - Ulam spiral 02.png]]
[[File:Fōrmulæ - Ulam spiral 03.png]]
=={{header|Go}}==
Line 3,063 ⟶ 3,405:
* * *
* * * * </pre>
=={{header|Maxima}}==
[[File:Ulam spiral.png|thumb|Ulam spiral powered by Maxima]]
Using the function defined in the Spiral matrix task
<syntaxhighlight lang="maxima">
/* Adapting the spiral to the problem requirements */
spiral_from_center(n):=(n^2+1)*matrixmap(lambda([x],x+1),zeromatrix(n,n))-spiral(n)$
/* Testing */
spiral_from_center(35)$
matrixmap(lambda([x],if primep(x) then "O" else ""),%);
</syntaxhighlight>
=={{header|Nim}}==
Line 4,181 ⟶ 4,535:
- 43 - - - 47 - - -
73 - - - - - 79 - -
</pre>
=={{header|Quackery}}==
<code>spiral</code> is defined at [[Spiral matrix#Quackery]].
<code>isprime</code> is defined at [[Primality by trial division#Quackery]].
<syntaxhighlight lang="Quackery"> 32 spiral
witheach
[ witheach
[ 1024 swap -
isprime iff
say " o"
else say " ." ]
cr ]</syntaxhighlight>
{{out}}
<pre>
. . . o . o . . . . . o . . . o . . . . . . . . . . . o . . . .
. . . . . . . . . . . . . . o . . . o . o . . . o . . . . . . .
. . . . . . . . . . . . . o . . . o . . . . . . . o . . . o . o
. . o . . . o . . . . . . . . . . . o . o . . . . . o . . . . .
. o . o . . . . . o . o . . . . . o . . . . . o . . . . . . . .
. . . . . . . . . . o . . . . . . . . . . . o . . . o . . . . .
. . . . . o . . . o . . . . . . . o . . . . . o . . . . . . . .
o . . . o . . . . . . . . . o . . . o . o . . . o . o . o . . .
. . . . . . . o . . . . . o . . . . . . . . . o . o . . . o . .
. . o . . . . . o . . . o . o . . . . . . . . . . . . . . . o .
. . . . . . . . . . . . . . . o . o . . . . . o . . . o . . . o
o . . . o . . . o . o . . . o . . . . . . . o . . . o . o . . .
. . . . . . . . . . . . . . . o . o . . . o . . . . . . . . . .
. . . . . . o . . . o . o . . . . . o . o . o . . . . . o . o .
. o . o . o . o . o . o . o . . . o . . . . . . . o . . . . . .
. . . . . . . . . . . . . . o . o . o . . . . . . . . . . . o .
. . . . . . . . . o . . . o . . o o . o . o . o . . . o . o . o
. . o . . . . . . . o . o . o . . . . . . . . . . . . . . . . .
. . . . . . . . . . . o . . . o . . . . . . . . . . . . . . . .
o . . . o . o . . . o . o . . . o . . . o . o . . . o . . . o .
. . . o . . . o . . . o . . . . . o . . . . . o . o . . . o . .
. . . . . . . . . . . . o . . . . . . . . . . . o . . . . . . .
. . . . . . . o . o . . . . . o . . . o . . . o . . . . . . . o
. . . . o . . . o . . . . . . . . . . . o . . . . . . . o . . .
. . . . . o . . . . . o . . . o . o . . . . . . . . . . . . . .
. . . . . . . . . . o . o . . . o . . . . . o . . . o . . . . .
. o . o . o . . . . . . . . . o . o . . . . . o . . . . . o . o
. . o . . . o . . . . . . . . . . . o . o . . . . . . . . . . .
. o . o . . . . . o . . . . . o . . . o . o . . . . . . . . . .
o . . . . . . . o . . . . . . . . . o . . . . . . . o . . . . .
. . . . . . . . . o . o . . . o . o . . . . . . . . . o . . . .
. . . . . . o . . . o . . . . . o . . . . . o . . . . . . . . .
</pre>
Line 5,399 ⟶ 5,805:
{{libheader|Wren-str}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
import "./math" for Int
import "./str" for Char
import "./fmt" for Fmt
var Direction = Enum.create("Direction", ["right", "up", "left", "down"])
Line 5,560 ⟶ 5,966:
http://www.zenkinetic.com/Images/RosettaCode/ulamSpiral.png
=={{header|ZX Spectrum Basic}}==
a simplistic naive procedure that on a real machine will take some DAYS at the max of 176x176 numbers to check
RUN 10 , to start
<syntaxhighlight lang="ZX Spectrum Basic">
1 IF n<max THEN LET n=n+1: FOR p=2 TO n-1: LET r= (INT (n/p)<>n/p): IF r THEN NEXT p
2 IF p=n THEN LET pr=pr+1: LET kx=(255-xx AND k<3)+(xx AND k>2): LET ky=(175-yy AND (k=1 OR k=3)+(yy AND (k=2 OR k=4))): PLOT kx,ky: PRINT #0;AT 0,0;n;" pr";pr
3 RETURN
10 CLS : PRINT "ULAM SPIRAL OF PRIME's"'''"it takes DAYS at max level"'"and 3.5MHz"
12 INPUT "square size= LxL"'"l: ";l: IF l<>INT l OR l<1 OR l>176 THEN GO TO 12
13 LET max=l*l: PRINT ''l;"x";l;"=";max;" positive integers"
15 INPUT "0,0 orientation 1-4: ";k: IF k<1 OR k>4 THEN GO TO 15
20 CLS : LET xx=127: LET yy=88: LET n=0: LET pr=0: PRINT #0;AT 0,15;max
40 FOR q=0 TO l: LET m=INT (q/2)=q/2: LET m=-1*m+NOT m
60 FOR x=0 TO q*m STEP m: GO SUB 1 : LET xx=xx+m: NEXT x
80 FOR y=0 TO q*m STEP m: GO SUB 1 : LET yy=yy+m: NEXT y: NEXT q
|