Ulam spiral (for primes): Difference between revisions

Content deleted Content added
Petelomax (talk | contribs)
m →‎{{header|Phix}}: second bit
SqrtNegInf (talk | contribs)
m →‎{{header|Raku}}: tweaks for code clarity
Line 4,265: Line 4,265:
<lang perl6>sub MAIN($max = 160, $start = 1) {
<lang perl6>sub MAIN($max = 160, $start = 1) {
(my %world){0}{0} = 0;
(my %world){0}{0} = 0;
my $loc = 0+0i;
my ($n, $dir, $side, $loc) = $start, 1, 0, 0+0i;
my $dir = 1;
my $n = $start;
my $side = 0;


while ++$side < $max {
while ++$side < $max {
step for ^$side;
step for ^$side; turn-left;
turn-left;
step for ^$side; turn-left;
step for ^$side;
turn-left;
}
}


Line 4,284: Line 4,279:
}
}


sub turn-left { $dir *= -i; }
sub turn-left { $dir ×= -i }
sub turn-right { $dir *= i; }
sub turn-right { $dir ×= i }

}
}


sub braille-graphics (%a) {
sub braille-graphics (%a) {
my ($ylo, $yhi, $xlo, $xhi);
my ($y-lo, $y-hi, $x-lo, $x-hi);
for %a.keys -> $y {
for %a.keys.map(+*) -> \y {
for %a{y}.keys.map(+*) -> \x {
$ylo min= +$y; $yhi max= +$y;
$y-lo min= y; $y-hi max= y;
for %a{$y}.keys -> $x {
$xlo min= +$x; $xhi max= +$x;
$x-lo min= x; $x-hi max= x;
}
}
}
}


for $ylo, $ylo + 4 ...^ * > $yhi -> \y {
for $y-lo, $y-lo + 4 ...^ $y-hi -> \y {
for $xlo, $xlo + 2 ...^ * > $xhi -> \x {
for $x-lo, $x-lo + 2 ...^ $x-hi -> \x {
my $cell = 0x2800;
my $cell = 0x2800;
$cell += 1 if %a{y + 0}{x + 0};
$cell += 2⁰ if %a{y + 0}{x + 0};
$cell += 2 if %a{y + 1}{x + 0};
$cell += 2¹ if %a{y + 1}{x + 0};
$cell += 4 if %a{y + 2}{x + 0};
$cell += if %a{y + 2}{x + 0};
$cell += 8 if %a{y + 0}{x + 1};
$cell += if %a{y + 0}{x + 1};
$cell += 16 if %a{y + 1}{x + 1};
$cell += 2⁴ if %a{y + 1}{x + 1};
$cell += 32 if %a{y + 2}{x + 1};
$cell += 2⁵ if %a{y + 2}{x + 1};
$cell += 64 if %a{y + 3}{x + 0};
$cell += 2⁶ if %a{y + 3}{x + 0};
$cell += 128 if %a{y + 3}{x + 1};
$cell += 2⁷ if %a{y + 3}{x + 1};
print chr($cell);
print chr($cell);
}
}
print "\n";
print "\n";