Smallest square that begins with n
- Task
Find smallest squares that begin with n for 0 < n < 50
Go
<lang go>package main
import (
"fmt" "math"
)
func isSquare(n int) bool {
s := int(math.Sqrt(float64(n))) return s*s == n
}
func main() {
var squares []int
outer:
for i := 1; i < 50; i++ { if isSquare(i) { squares = append(squares, i) } else { n := i limit := 10 for { n *= 10 for j := 0; j < limit; j++ { s := n + j if isSquare(s) { squares = append(squares, s) continue outer } } limit *= 10 } } } fmt.Println("Smallest squares that begin with 'n' in [1, 49]:") for i, s := range squares { fmt.Printf("%5d ", s) if ((i + 1) % 10) == 0 { fmt.Println() } } if (len(squares) % 10) != 0 { fmt.Println() }
}</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
Phix
<lang Phix>constant lim = 49 sequence res = repeat(0,lim) integer n = 1, found = 0 while found<lim do
integer n2 = n*n while n2 do if n2<=lim and res[n2]=0 then found += 1 res[n2] = n end if n2 = floor(n2/10) end while n += 1
end while res = columnize({tagset(lim),sq_power(res,2),apply(true,sprintf,{{"(%d^2)"},res})}) printf(1,"Smallest squares that begin with 1..%d:\n%s\n",
{lim,join_by(apply(true,sprintf,{{"%2d: %5d %-8s"},res}),10,5)})</lang>
- Output:
Smallest squares that begin with 1..49: 1: 1 (1^2) 11: 1156 (34^2) 21: 2116 (46^2) 31: 3136 (56^2) 41: 41209 (203^2) 2: 25 (5^2) 12: 121 (11^2) 22: 225 (15^2) 32: 324 (18^2) 42: 4225 (65^2) 3: 36 (6^2) 13: 1369 (37^2) 23: 2304 (48^2) 33: 3364 (58^2) 43: 4356 (66^2) 4: 4 (2^2) 14: 144 (12^2) 24: 2401 (49^2) 34: 3481 (59^2) 44: 441 (21^2) 5: 529 (23^2) 15: 1521 (39^2) 25: 25 (5^2) 35: 35344 (188^2) 45: 45369 (213^2) 6: 64 (8^2) 16: 16 (4^2) 26: 2601 (51^2) 36: 36 (6^2) 46: 4624 (68^2) 7: 729 (27^2) 17: 1764 (42^2) 27: 2704 (52^2) 37: 3721 (61^2) 47: 4761 (69^2) 8: 81 (9^2) 18: 1849 (43^2) 28: 289 (17^2) 38: 3844 (62^2) 48: 484 (22^2) 9: 9 (3^2) 19: 196 (14^2) 29: 2916 (54^2) 39: 3969 (63^2) 49: 49 (7^2) 10: 100 (10^2) 20: 2025 (45^2) 30: 3025 (55^2) 40: 400 (20^2)
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
<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 (j in 0...limit) { var s = n + j 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