Magic constant: Difference between revisions
Content added Content deleted
(→{{header|Wren}}: Updated version - now almost instant (13 ms).) |
(→{{header|Wren}}: Now uses new BigInt cube root function rather than Num version - still quick (17 ms).) |
||
Line 226: | Line 226: | ||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
{{libheader|Wren-big}} |
{{libheader|Wren-big}} |
||
This uses Julia's approach for the final parts |
This uses Julia's approach for the final parts. |
||
<lang ecmascript>import "./seq" for Lst |
<lang ecmascript>import "./seq" for Lst |
||
import "./fmt" for Fmt |
import "./fmt" for Fmt |
||
import "./big" for BigInt |
import "./big" for BigInt |
||
var magicConstant = Fn.new { |n| (n*n + 1) * n / 2 } |
var magicConstant = Fn.new { |n| (n*n + 1) * n / 2 } |
||
var ss = ["\u2070", "\u00b9", "\u00b2", "\u00b3", "\u2074", |
var ss = ["\u2070", "\u00b9", "\u00b2", "\u00b3", "\u2074", |
||
"\u2075", "\u2076", "\u2077", "\u2078", "\u2079"] |
"\u2075", "\u2076", "\u2077", "\u2078", "\u2079"] |
||
var superscript = Fn.new { |n| (n < 10) ? ss[n] : (n < 20) ? ss[1] + ss[n - 10] : ss[2] + ss[0] } |
var superscript = Fn.new { |n| (n < 10) ? ss[n] : (n < 20) ? ss[1] + ss[n - 10] : ss[2] + ss[0] } |
||
System.print("First 20 magic constants:") |
System.print("First 20 magic constants:") |
||
var mc20 = (3..22).map { |n| magicConstant.call(n) }.toList |
var mc20 = (3..22).map { |n| magicConstant.call(n) }.toList |
||
for (chunk in Lst.chunks(mc20, 10)) Fmt.print("$5d", chunk) |
for (chunk in Lst.chunks(mc20, 10)) Fmt.print("$5d", chunk) |
||
Fmt.print("\n1,000th magic constant: $,d", magicConstant.call(1002)) |
Fmt.print("\n1,000th magic constant: $,d", magicConstant.call(1002)) |
||
System.print("\nSmallest order magic square with a constant greater than:") |
System.print("\nSmallest order magic square with a constant greater than:") |
||
for (i in 1..20) { |
for (i in 1..20) { |
||
var goal = |
var goal = BigInt.ten.pow(i) |
||
var order = |
var order = (goal * 2).icbrt + 1 |
||
// accuracy check as we're using Num arithmetic to calculate the order |
|||
var bigGoal = BigInt.ten.pow(i) |
|||
var bigOrder = BigInt.new(order) |
|||
if (!(magicConstant.call(bigOrder) > bigGoal && magicConstant.call(bigOrder-1) <= bigGoal)) { |
|||
System.print("Order for 10^%(i) is inaccurate.") |
|||
} |
|||
Fmt.print("10$-2s : $,9i", superscript.call(i), order) |
Fmt.print("10$-2s : $,9i", superscript.call(i), order) |
||
}</lang> |
}</lang> |