Smallest square that begins with n: Difference between revisions

Content added Content deleted
m (→‎{{header|Free Pascal}}: already know the power of 10 to divide with.)
(→‎{{header|Wren}}: Added a Pascal translation.)
Line 3,096: Line 3,096:
=={{header|Wren}}==
=={{header|Wren}}==
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
===Version 1===
<syntaxhighlight lang="wren">import "./fmt" for Fmt
<syntaxhighlight lang="wren">import "./fmt" for Fmt


Line 3,137: Line 3,138:
3136 324 3364 3481 35344 36 3721 3844 3969 400
3136 324 3364 3481 35344 36 3721 3844 3969 400
41209 4225 4356 441 45369 4624 4761 484 49
41209 4225 4356 441 45369 4624 4761 484 49
</pre>

===Version 2===
{{trans|Pascal}}
<syntaxhighlight lang="wren">import "./fmt" for Fmt

var lowSquareStartN = Fn.new { |n|
var sqrtN = n.sqrt
var sqrtN10 = (n * 10).sqrt
while (true) {
for (i in [sqrtN.truncate, sqrtN10.truncate]) {
for (j in 0..1) {
var mySqr = i * i
while (mySqr > n) mySqr = (mySqr/10).floor
if (mySqr == n) return i
i = i + 1
}
}
sqrtN = sqrtN * 10
sqrtN10 = sqrtN10 * 10
if (sqrtN > 10 * n) break
}
}

System.print("Test 1 .. 49")
for (i in 1..49) {
var t = lowSquareStartN.call(i)
Fmt.write("$7d", t * t)
if (i % 10 == 0) System.print()
}
System.print("\n")
System.print("Test 999,991 .. 1,000,000")
for (i in 999991..1e6) {
var t = lowSquareStartN.call(i)
Fmt.print("$10d : $10d -> $14d", i, t, t * t)
}</syntaxhighlight>

{{out}}
<pre>
Identical to Pascal entry.
</pre>
</pre>