Smallest square that begins with n: Difference between revisions

From Rosetta Code
Content added Content deleted
(Created page with "{{Draft task}} ;Task Find smallest squares that begin with n for 0 < n < 50 =={{header|Ring}}== <lang ring> load "stdlib.ring" see "working..." + nl see "smallest squares t...")
 
(Added Wren)
Line 50: Line 50:
4624 4761 484 49
4624 4761 484 49
done...
done...
</pre>

=={{header|Wren}}==
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/seq" for Lst
import "/fmt" for Fmt

var isSquare = Fn.new { |n|
var s = n.sqrt.floor
return s * s == n
}

var squares = []
for (i in 1..49) {
if (isSquare.call(i)) {
squares.add(i)
} else {
var n = i
var limit = 10
while (true) {
n = n * 10
var found = false
for (i in 0...limit) {
var s = n + i
if (isSquare.call(s)) {
squares.add(s)
found = true
break
}
}
if (found) break
limit = limit * 10
}
}
}
System.print("Smallest squares that begin with 'n' in [1, 49]:")
for (chunk in Lst.chunks(squares, 10)) Fmt.print("$5d ", chunk)</lang>

{{out}}
<pre>
Smallest squares that begin with 'n' in [1, 49]:
1 25 36 4 529 64 729 81 9 100
1156 121 1369 144 1521 16 1764 1849 196 2025
2116 225 2304 2401 25 2601 2704 289 2916 3025
3136 324 3364 3481 35344 36 3721 3844 3969 400
41209 4225 4356 441 45369 4624 4761 484 49
</pre>
</pre>

Revision as of 10:33, 19 March 2021

Smallest square that begins with n is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task

Find smallest squares that begin with n for 0 < n < 50

Ring

<lang ring> load "stdlib.ring"

see "working..." + nl see "smallest squares that begin with n:" + nl

row = 0 limit1 = 49 limit2 = 45369

for n = 1 to limit1

   strn = string(n)
   lenn = len(strn)
   for m = 1 to limit2
       floor = sqrt(m)
       bool = (m % floor = 0)
       strm = string(m)
       if left(strm,lenn) = n and bool = 1
          row = row + 1
          see "" + strm + " "
          if row%5 = 0
             see nl
          ok
          exit
       ok
    next

next

see nl + "done..." + nl </lang>

Output:
working...
smallest squares that begin with n:
1 25 36 4 529 
64 729 81 9 100 
1156 121 1369 144 1521 
16 1764 1849 196 2025 
2116 225 2304 2401 25 
2601 2704 289 2916 3025 
3136 324 3364 3481 35344 
36 3721 3844 3969 400 
41209 4225 4356 441 45369 
4624 4761 484 49 
done...

Wren

Library: Wren-seq
Library: Wren-fmt

<lang ecmascript>import "/seq" for Lst import "/fmt" for Fmt

var isSquare = Fn.new { |n|

   var s = n.sqrt.floor
   return s * s == n

}

var squares = [] for (i in 1..49) {

   if (isSquare.call(i)) {
       squares.add(i)
   } else {
       var n = i
       var limit = 10
       while (true) {
           n = n * 10
           var found = false
           for (i in 0...limit) {
               var s = n + i
               if (isSquare.call(s)) {
                   squares.add(s)
                   found = true
                   break
               }
           }
           if (found) break
           limit = limit * 10
       }
   }

} System.print("Smallest squares that begin with 'n' in [1, 49]:") for (chunk in Lst.chunks(squares, 10)) Fmt.print("$5d ", chunk)</lang>

Output:
Smallest squares that begin with 'n' in [1, 49]:
    1    25    36     4   529    64   729    81     9   100  
 1156   121  1369   144  1521    16  1764  1849   196  2025  
 2116   225  2304  2401    25  2601  2704   289  2916  3025  
 3136   324  3364  3481 35344    36  3721  3844  3969   400  
41209  4225  4356   441 45369  4624  4761   484    49