Smallest square that begins with n: Difference between revisions
Content added Content deleted
(added Raku programming solution) |
(→{{header|REXX}}: added the computer programming language REXX.) |
||
Line 156: | Line 156: | ||
48 => 484 |
48 => 484 |
||
49 => 49 |
49 => 49 |
||
</pre> |
|||
=={{header|REXX}}== |
|||
A little extra code was added to display the results in a table, the width of the table can be specified. |
|||
Also, the output display was generalized so that if a larger number than expected won't be truncated. |
|||
<lang rexx>/*REXX program finds and displays (positive integers) squares that begin with N. */ |
|||
numeric digits 20 /*ensure that large numbers can be used*/ |
|||
parse arg n cols . /*get optional number of primes to find*/ |
|||
if n=='' | n=="," then n= 50 /*Not specified? Then assume default.*/ |
|||
if cols=='' | cols=="," then cols= 10 /* " " " " " */ |
|||
w= 10 /*width of a number in any column. */ |
|||
say ' index │'center(" smallest squares that begin with N", 1 + cols*(w+1) ) |
|||
say '───────┼'center("" , 1 + cols*(w+1), '─') |
|||
#= 0 /*initialize the count of found numbers*/ |
|||
idx= 1 |
|||
$= /*a list of additive primes (so far). */ |
|||
do j=1 while #<n /*keep searching 'til enough nums found*/ |
|||
do k=1 until pos(j, k * k)==1 /*compute a square of some number. */ |
|||
end /*k*/ |
|||
#= # + 1 /*bump the count of numbers found. */ |
|||
c= commas(k*k); L= length(c) /*calculate K**2 (with commas) and L */ |
|||
$= $ right( c, max(w, L) ) /*add square to $ list, allow for big N*/ |
|||
if #//cols\==0 then iterate /*have we populated a line of output? */ |
|||
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */ |
|||
idx= idx + cols |
|||
end /*j*/ |
|||
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/ |
|||
exit 0 /*stick a fork in it, we're all done. */ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?</lang> |
|||
{{out|output|text= when using the default inputs:}} |
|||
<pre> |
|||
index │ smallest squares that begin with N |
|||
───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
|||
1 │ 1 25 36 4 529 64 729 81 9 100 |
|||
11 │ 1,156 121 1,369 144 1,521 16 1,764 1,849 196 2,025 |
|||
21 │ 2,116 225 2,304 2,401 25 2,601 2,704 289 2,916 3,025 |
|||
31 │ 3,136 324 3,364 3,481 35,344 36 3,721 3,844 3,969 400 |
|||
41 │ 41,209 4,225 4,356 441 45,369 4,624 4,761 484 49 5,041 |
|||
</pre> |
</pre> |
||