Magic constant: Difference between revisions

m
Added Easylang
(add RPL)
m (Added Easylang)
(13 intermediate revisions by 7 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++}}==
<syntaxhighlight lang="c++">
#include <cmath>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <string>
 
constexpr int32_t ORDER_FIRST_MAGIC_SQUARE = 3;
constexpr double LN2 = log(2.0);
constexpr double LN10 = log(10.0);
 
// Return the magic constant for a magic square of the given order
int32_t magicConstant(int32_t 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
int32_t minimumOrder(int32_t n) {
return (int) exp( ( LN2 + n * LN10 ) / 3 ) + 1;
}
 
// Return the order of the magic square at the given index
int32_t order(int32_t index) {
return ORDER_FIRST_MAGIC_SQUARE + index - 1;
}
 
int main() {
std::cout << "The first 20 magic constants:" << std::endl;
for ( int32_t i = 1; i <= 20; ++i ) {
std::cout << " " << magicConstant(order(i));
}
std::cout << std::endl << std::endl;
 
std::cout << "The 1,000th magic constant: " << magicConstant(order(1'000)) << std::endl << std::endl;
 
std::cout << "Order of the smallest magic square whose constant is greater than:" << std::endl;
for ( int32_t i = 1; i <= 20; ++i ) {
std::string power_of_10 = "10^" + std::to_string(i) + ":";
std::cout << std::setw(6) << power_of_10 << std::setw(8) << minimumOrder(i) << std::endl;
}
}
</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|Delphi}}==
Line 570 ⟶ 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 706 ⟶ 878:
19 2714418 10000004237431278525
20 5848036 100000026858987459346</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
public final class MagicConstant {
 
public static void main(String[] aArgs) {
System.out.println("The first 20 magic constants:");
for ( int i = 1; i <= 20; i++ ) {
System.out.print(" " + magicConstant(order(i)));
}
System.out.println(System.lineSeparator());
System.out.println("The 1,000th magic constant: " + magicConstant(order(1_000)) + System.lineSeparator());
System.out.println("Order of the smallest magic square whose constant is greater than:");
for ( int i = 1; i <= 20; i++ ) {
String powerOf10 = "10^" + i + ":";
System.out.println(String.format("%6s%8s", powerOf10, minimumOrder(i)));
}
}
// Return the magic constant for a magic square of the given order
private static int magicConstant(int aN) {
return aN * ( aN * aN + 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 aN) {
return (int) Math.exp( ( LN2 + aN * LN10 ) / 3 ) + 1;
}
// Return the order of the magic square at the given index
private static int order(int aIndex) {
return ORDER_FIRST_MAGIC_SQUARE + aIndex - 1;
}
private static final int ORDER_FIRST_MAGIC_SQUARE = 3;
private static final double LN2 = Math.log(2.0);
private static final double LN10 = Math.log(10.0);
 
}
</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|jq}}==
Line 810 ⟶ 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 877 ⟶ 1,172:
10^49 27144176165949066
10^50 58480354764257322</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">import std/[math, unicode]
 
func magicConstant(n: int): int =
## Return the magic constant for a magic square of order "n".
n * (n * n + 1) div 2
 
func minOrder(n: int): int =
## Return the smallest order such as the magic constant is greater than "10^n".
const Ln2 = ln(2.0)
const Ln10 = ln(10.0)
result = int(exp((Ln2 + n.toFloat * Ln10) / 3)) + 1
 
const First = 3
const Superscripts: array['0'..'9', string] = ["⁰", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹"]
 
template order(idx: Positive): int =
## Compute the order of the magic square at index "idx".
idx + (First - 1)
 
func superscript(n: Natural): string =
## Return the Unicode string to use to represent an exponent.
for d in $n:
result.add(Superscripts[d])
 
echo "First 20 magic constants:"
for idx in 1..20:
stdout.write ' ', order(idx).magicConstant
echo()
 
echo "\n1000th magic constant: ", order(1000).magicConstant
 
echo "\nOrder of the smallest magic square whose constant is greater than:"
for n in 1..20:
let left = "10" & n.superscript & ':'
echo left.alignLeft(6), ($minOrder(n)).align(7)
</syntaxhighlight>
 
{{out}}
<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
 
1000th magic constant: 503006505
 
Order of the smallest magic square whose constant is greater than:
10¹: 3
10²: 6
10³: 13
10⁴: 28
10⁵: 59
10⁶: 126
10⁷: 272
10⁸: 585
10⁹: 1260
10¹⁰: 2715
10¹¹: 5849
10¹²: 12600
10¹³: 27145
10¹⁴: 58481
10¹⁵: 125993
10¹⁶: 271442
10¹⁷: 584804
10¹⁸: 1259922
10¹⁹: 2714418
10²⁰: 5848036
</pre>
 
=={{header|Pascal}}==
==={{header|Free Pascal}}===
<syntaxhighlight lang="pascal">
program MagicConst;
{$IFDEF FPC}{$MODE DELPHI}{$OPTIMIZATION ON,ALL}{$ENDIF}
{$IFDEF WINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
 
function MagicSum(n :Uint32):Uint64; inline;
var
k : Uint64;
begin
k := n*Uint64(n);
result := (k*k+k) DIV 2;
end;
 
function MagSumPerRow(n:Uint32):Uint32;
begin
//result := MagicSum(n) DIV n;
//(n^3 + n) /2
result := ((Uint64(n)*n+1)*n) DIV 2;
end;
var
s : String[31];
i : Uint32;
lmt,rowcnt : extended;
Begin
writeln('First Magic constants 3..20');
For i := 3 to 20 do
write(MagSumPerRow(i),' ');
writeln;
writeln('1000.th ',MagSumPerRow(1002));
 
writeln('First Magic constants > 10^xx');
//lmt = (rowcnt^3 + rowcnt) /2 -> rowcnt > (lmt*2 )^(1/3)
lmt := 2.0 * 10.0;
For i := 1 to 50 do
begin
rowcnt := Int(exp(ln(lmt)/3))+1.0;//+1 suffices
str(trunc(rowcnt),s);
writeln('10^',i:2,#9,s:18);
f := 10.0*lmt;
end;
end.</syntaxhighlight>
{{out}}
<pre>
First Magic constants 3..20
15 34 65 111 175 260 369 505 671 870 1105 1379 1695 2056 2465 2925 3439 4010
1000.th 503006505
First Magic constants > 10^xx
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
... same as Mathematica
10^46 2714417616594907
10^47 5848035476425733
10^48 12599210498948732
10^49 27144176165949066
10^50 58480354764257322</pre>
 
=={{header|Perl}}==
Line 1,143 ⟶ 1,581:
10²⁰: 5,848,036</pre>
 
=={{header|RPL}}==
This task can be solved through a few one-liners, by using algebraic and equation-solving features.
 
Line 1,153 ⟶ 1,592:
And finally, look for a<sup>-1</sup>( 10 ^ j ), for j=1 to 10:
≪ {} 1 10 '''FOR''' j 10 j ^ '''A6003→''' + '''NEXT''' ≫ EVAL
which is a one-minute job for a basic HP-28S.
{{out}}
<pre>
Line 1,204 ⟶ 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
2,078

edits