17 lines
576 B
Text
17 lines
576 B
Text
local fmt = require "fmt"
|
|
|
|
local magic_constant = |n| -> (n * n + 1) * n / 2
|
|
|
|
print("First 20 magic constants:")
|
|
local mc20 = range(3, 22):map(|n| -> magic_constant(n))
|
|
for mc20:chunk(10) as chunk do fmt.tprint("%5d", chunk) end
|
|
|
|
fmt.print("\n1,000th magic constant: %,s", magic_constant(1002))
|
|
|
|
print("\nSmallest order magic square with a constant greater than:")
|
|
for i = 1, 20 do
|
|
local goal = 10 ^ i
|
|
local order = math.floor(math.cbrt(goal * 2)) + 1
|
|
local len = (i <= 3) ? -3 : (4 <= i <= 9) ? -4 : -2
|
|
fmt.print($"10%{len}s : %,9s", fmt.super(i), order)
|
|
end
|