Magic constant: Difference between revisions

m
Added Easylang
m (Minor code improvement.)
m (Added Easylang)
 
(3 intermediate revisions by 3 users not shown)
Line 489:
next e
end</syntaxhighlight>
 
 
=={{header|C#}}==
{{trans|Java}}
<syntaxhighlight lang="C#">
using System;
 
public class MagicConstant {
 
private const int OrderFirstMagicSquare = 3;
 
public static void Main(string[] args) {
Console.WriteLine("The first 20 magic constants:");
for (int i = 1; i <= 20; i++) {
Console.Write(" " + MagicConstantValue(Order(i)));
}
Console.WriteLine("\n");
 
Console.WriteLine("The 1,000th magic constant: " + MagicConstantValue(Order(1_000)) + "\n");
 
Console.WriteLine("Order of the smallest magic square whose constant is greater than:");
for (int i = 1; i <= 20; i++) {
string powerOf10 = "10^" + i + ":";
Console.WriteLine($"{powerOf10,6}{MinimumOrder(i),8}");
}
}
 
// Return the magic constant for a magic square of the given order
private static int MagicConstantValue(int n) {
return n * (n * n + 1) / 2;
}
 
// Return the smallest order of a magic square such that its magic constant is greater than 10 to the given power
private static int MinimumOrder(int n) {
return (int)Math.Exp((Math.Log(2.0)+ n * Math.Log(10.0)) / 3) + 1;
}
 
// Return the order of the magic square at the given index
private static int Order(int index) {
return OrderFirstMagicSquare + index - 1;
}
}
</syntaxhighlight>
{{out}}
<pre>
The first 20 magic constants:
15 34 65 111 175 260 369 505 671 870 1105 1379 1695 2056 2465 2925 3439 4010 4641 5335
 
The 1,000th magic constant: 503006505
 
Order of the smallest magic square whose constant is greater than:
10^1: 3
10^2: 6
10^3: 13
10^4: 28
10^5: 59
10^6: 126
10^7: 272
10^8: 585
10^9: 1260
10^10: 2715
10^11: 5849
10^12: 12600
10^13: 27145
10^14: 58481
10^15: 125993
10^16: 271442
10^17: 584804
10^18: 1259922
10^19: 2714418
10^20: 5848036
 
</pre>
 
=={{header|C++}}==
Line 643 ⟶ 716:
 
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
func a n .
n += 2
return n * (n * n + 1) / 2
.
func inva x .
while k * (k * k + 1) / 2 + 2 < x
k += 1
.
return k
.
write "The first 20 magic constants: "
for n to 20
write a n & " "
.
print ""
print ""
print "The 1,000th magic constant: " & a 1000
print ""
print "Smallest magic square with constant greater than:"
for e to 10
print "10^" & e & ": " & inva pow 10 e
.
</syntaxhighlight>
 
=={{header|Factor}}==
Line 954 ⟶ 1,053:
10^20 5848036
</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">function magic (x)
return x * (1 + x^2) / 2
end
 
print("Magic constants of orders 3 to 22:")
for i = 3, 22 do
io.write(magic(i) .. " ")
end
 
print("\n\nMagic constant 1003: " .. magic(1003) .. "\n")
 
print("Orders of smallest magic constant greater than...")
print("-----\t-----\nValue\tOrder\n-----\t-----")
local order = 1
for i = 1, 20 do
repeat
order = order + 1
until magic(order) > 10 ^ i
print("10^" .. i, order)
end</syntaxhighlight>
{{out}}
<pre>Magic constants of orders 3 to 22:
15 34 65 111 175 260 369 505 671 870 1105 1379 1695 2056 2465 2925 3439 4010 4641 5335
 
Magic constant 1003: 504514015
 
Orders of smallest magic constant greater than...
----- -----
Value Order
----- -----
10^1 3
10^2 6
10^3 13
10^4 28
10^5 59
10^6 126
10^7 272
10^8 585
10^9 1260
10^10 2715
10^11 5849
10^12 12600
10^13 27145
10^14 58481
10^15 125993
10^16 271442
10^17 584804
10^18 1259922
10^19 2714418
10^20 5848036</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Line 1,493 ⟶ 1,644:
{{libheader|Wren-fmt}}
This uses Julia's approach for the final parts.
<syntaxhighlight lang="ecmascriptwren">import "./seq" for Lst
import "./fmt" for Fmt
1,980

edits