Smallest square that begins with n: Difference between revisions

Add Zig
(Add Cowgol)
(Add Zig)
Line 1,667:
41209 4225 4356 441 45369 4624 4761 484 49
</pre>
 
=={{header|Zig}}==
<lang zig>pub fn beginsWith(a: u16, b: u16) bool {
var aa = a;
while (aa > b) aa /= 10;
return aa == b;
}
 
pub fn smallestSquare(n: u16) u16 {
var sqn: u16 = 1;
while (true) : (sqn += 1) {
var sq = sqn * sqn;
if (beginsWith(sq, n)) return sq;
}
}
 
pub fn main() !void {
const stdout = @import("std").io.getStdOut().writer();
var n: u16 = 1;
while (n < 50) : (n += 1) {
try stdout.print("{d}\n", .{smallestSquare(n)});
}
}</lang>
{{out}}
<pre style='height:50ex;'>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>
2,119

edits