Magic constant: Difference between revisions

Used logarithm to avoid overflow in the stretch task.
(Created Nim solution.)
(Used logarithm to avoid overflow in the stretch task.)
Line 885:
n * (n * n + 1) div 2
 
func minOrder(valuen: int): int =
## Return the smallest order such as the magic constant is greater than "value10^n".
letconst startLn2 = intln(cbrt(value2.toFloat * 2)0)
resultconst Ln10 = startln(10.0)
result = int(exp((Ln2 + n.toFloat * Ln10) / 3)) + 1
while result.magicConstant < value:
inc result
 
const First = 3
Line 912 ⟶ 911:
 
echo "\nOrder of the smallest magic square whose constant is greater than:"
for n in 1..1820:
let left = "10" & n.superscript & ':'
echo left.alignLeft(6), ($minOrder(10^n)).align(7)
</syntaxhighlight>
 
{{out}}
For the stretch task, as integers are 64 bits long, we are limited to 10^18. To go further, we would have to use external libraries such as <code>bigints</code>, <code>bignum</code> or <code>integers</code>.
<pre>First 20 magic constants:
15 34 65 111 175 260 369 505 671 870 1105 1379 1695 2056 2465 2925 3439 4010 4641 5335
Line 943 ⟶ 941:
10¹⁷: 584804
10¹⁸: 1259922
10¹⁹: 2714418
10²⁰: 5848036
</pre>
 
256

edits