Smallest square that begins with n

# 20210319 Raku programming solution

my @needles  = (1..49);
my @haystack = (1..*) Z× (1..*);
# my @haystack = ( 1, 4, -> \a, \b { 2*b - a + 2 } ... * );
# my @haystack = ( 1, { (++$)² } ... * );
for @needles -> \needle {
   for @haystack -> \hay {
      { say needle, " => ", hay and last } if hay.starts-with: needle
   }
}

Output:

1 => 1
2 => 25
3 => 36
4 => 4
5 => 529
6 => 64
7 => 729
8 => 81
9 => 9
10 => 100
11 => 1156
12 => 121
13 => 1369
14 => 144
15 => 1521
16 => 16
17 => 1764
18 => 1849
19 => 196
20 => 2025
21 => 2116
22 => 225
23 => 2304
24 => 2401
25 => 25
26 => 2601
27 => 2704
28 => 289
29 => 2916
30 => 3025
31 => 3136
32 => 324
33 => 3364
34 => 3481
35 => 35344
36 => 36
37 => 3721
38 => 3844
39 => 3969
40 => 400
41 => 41209
42 => 4225
43 => 4356
44 => 441
45 => 45369
46 => 4624
47 => 4761
48 => 484
49 => 49

As the desired range is so small, there is not much gained by caching the squares. Less efficient, but less verbose:

say $_ => ^Inf .map(*²).first: *.starts-with: $_ for 1..49;

Same output.

Last updated