Data update
This commit is contained in:
parent
72eb4943cb
commit
4d5544505c
2347 changed files with 62432 additions and 16731 deletions
25
Task/Magic-constant/ANSI-BASIC/magic-constant.basic
Normal file
25
Task/Magic-constant/ANSI-BASIC/magic-constant.basic
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
100 REM Magic constant
|
||||
110 DECLARE EXTERNAL FUNCTION A
|
||||
120 DECLARE EXTERNAL FUNCTION InvA
|
||||
130 PRINT "The first 20 magic constants are";
|
||||
140 FOR N = 1 TO 20
|
||||
150 PRINT A(N);
|
||||
160 NEXT N
|
||||
170 PRINT
|
||||
180 PRINT "The 1,000th magic constant is"; A(1000)
|
||||
190 LET E = 1
|
||||
200 FOR N = 1 TO 20
|
||||
210 LET E = E * 10
|
||||
220 PRINT "10^";
|
||||
230 PRINT USING "##: #########": N, InvA(E)
|
||||
240 NEXT N
|
||||
250 END
|
||||
260 REM Returns the magic constant of a magic square of order N + 2
|
||||
270 EXTERNAL FUNCTION A(N)
|
||||
280 LET N2 = N + 2
|
||||
290 LET A = IP((N2 * ((N2 * N2) + 1)) / 2)
|
||||
300 END FUNCTION
|
||||
310 REM Returns the order of the magic square whose magic constant is at least X
|
||||
320 EXTERNAL FUNCTION InvA(X)
|
||||
330 LET InvA = IP((2 * X) ^ (1 / 3)) + 1
|
||||
340 END FUNCTION
|
||||
32
Task/Magic-constant/C/magic-constant.c
Normal file
32
Task/Magic-constant/C/magic-constant.c
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
/* Magic constant */
|
||||
#include<stdio.h>
|
||||
#include<math.h>
|
||||
|
||||
/* returns the magic constant of a magic square of order n + 2 */
|
||||
long a(int n)
|
||||
{
|
||||
long n2 = n + 2;
|
||||
return (n2 * ((n2 * n2) + 1)) / 2;
|
||||
}
|
||||
|
||||
/* returns the order of the magic square whose magic constant is at least x */
|
||||
long inv_a(double x)
|
||||
{
|
||||
return (long)pow((2. * x), 1. / 3.) + 1;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
double e = 1.; /* final values can be greater than MAX_LONG */
|
||||
printf("The first 20 magic constants are ");
|
||||
for (n = 1; n <= 20; n++)
|
||||
printf("%d ", a(n));
|
||||
printf("\n");
|
||||
printf("The 1,000th magic constant is %d\n", a(1000));
|
||||
for (n = 1; n <= 20; n++)
|
||||
{
|
||||
e *= 10;
|
||||
printf("10^%2d: %9d\n", n, inv_a(e));
|
||||
}
|
||||
}
|
||||
24
Task/Magic-constant/GW-BASIC/magic-constant.basic
Normal file
24
Task/Magic-constant/GW-BASIC/magic-constant.basic
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
100 REM Magic constant
|
||||
110 PRINT "First 20 magic constants:";
|
||||
120 FOR N = 3 TO 20 + 3 - 1
|
||||
130 PRINT (N * N * N + N) / 2;
|
||||
140 NEXT N
|
||||
150 PRINT
|
||||
160 PRINT "1000th magic constant: ";
|
||||
170 N# = 1000! + 3 - 1
|
||||
180 MC1000# = INT((N# * N# * N# + N#) / 2)
|
||||
190 PRINT USING "#########"; MC1000#
|
||||
200 PRINT "Smallest order magic square with a constant greater than:"
|
||||
210 THRESH# = 10
|
||||
220 M# = 3
|
||||
230 FOR X = 1 TO 20
|
||||
240 MC# = (M# * M# * M# + M#) / 2
|
||||
250 M# = M# + 1
|
||||
260 IF MC# <= THRESH# THEN 240
|
||||
270 PRINT "10^";
|
||||
280 PRINT USING "##:"; X;
|
||||
290 PRINT USING "########"; M# - 1;
|
||||
300 THRESH# = THRESH# * 10
|
||||
310 IF X MOD 5 = O THEN PRINT ELSE PRINT "| ";
|
||||
320 NEXT X
|
||||
330 END
|
||||
35
Task/Magic-constant/Gambas/magic-constant.gambas
Normal file
35
Task/Magic-constant/Gambas/magic-constant.gambas
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
Function a(n As Integer) As Long
|
||||
|
||||
n += 2
|
||||
Return n * (n ^ 2 + 1) / 2
|
||||
|
||||
End Function
|
||||
|
||||
Function inv_a(x As Float) As Long
|
||||
|
||||
Dim k As Long = 0
|
||||
|
||||
While k * (k ^ 2 + 1) / 2 + 2 < x
|
||||
k += 1
|
||||
Wend
|
||||
Return k
|
||||
|
||||
End Function
|
||||
|
||||
Public Sub Main()
|
||||
|
||||
Dim n As Long
|
||||
|
||||
Print "The first 20 magic constants are: "
|
||||
|
||||
For n = 1 To 20
|
||||
Print a(n); " ";
|
||||
Next
|
||||
Print
|
||||
Print "The 1,000th magic constant is "; a(1000)
|
||||
|
||||
For e As Integer = 1 To 20
|
||||
Print "10^"; Format(e, "##"); ": "; Format(inv_a(10 ^ e), "#########")
|
||||
Next
|
||||
|
||||
End Sub
|
||||
46
Task/Magic-constant/Modula-2/magic-constant.mod2
Normal file
46
Task/Magic-constant/Modula-2/magic-constant.mod2
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
MODULE MagicConstant;
|
||||
|
||||
FROM STextIO IMPORT WriteString, WriteLn;
|
||||
FROM SWholeIO IMPORT WriteInt;
|
||||
FROM RealMath IMPORT exp, ln;
|
||||
|
||||
VAR
|
||||
N: CARDINAL;
|
||||
E: REAL;
|
||||
|
||||
(* Returns the magic constant of a magic square of order N + 2 *)
|
||||
PROCEDURE A(N: CARDINAL): CARDINAL;
|
||||
VAR
|
||||
N2: CARDINAL;
|
||||
BEGIN
|
||||
N2 := N + 2;
|
||||
RETURN (N2 * ((N2 * N2) + 1)) DIV 2
|
||||
END A;
|
||||
|
||||
(* Returns the order of the magic square whose magic constant is at least X *)
|
||||
PROCEDURE InvA(X: REAL): CARDINAL;
|
||||
BEGIN
|
||||
RETURN VAL(INTEGER, exp(ln((2. * X)) / 3.) + 1.)
|
||||
(* Use of power(2. * X, 1. / 3.) + 1. does not give enough precision due to rounded exponent *)
|
||||
END InvA;
|
||||
|
||||
BEGIN
|
||||
WriteString("The first 20 magic constants are ");
|
||||
FOR N := 1 TO 20 DO
|
||||
WriteInt(A(N), 1);
|
||||
WriteString(" ")
|
||||
END;
|
||||
WriteLn;
|
||||
WriteString("The 1,000th magic constant is ");
|
||||
WriteInt(A(1000), 1);
|
||||
WriteLn;
|
||||
E := 1.;
|
||||
FOR N := 1 TO 20 DO (* The results are equal to these from Mathematica for N <= 23 *)
|
||||
E := E * 10.;
|
||||
WriteString("10^");
|
||||
WriteInt(N, 2);
|
||||
WriteString(": ");
|
||||
WriteInt(InvA(E), 9);
|
||||
WriteLn
|
||||
END
|
||||
END MagicConstant.
|
||||
27
Task/Magic-constant/PHP/magic-constant.php
Normal file
27
Task/Magic-constant/PHP/magic-constant.php
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
// Magic constant
|
||||
|
||||
// Returns the magic constant of a magic square of order $n + 2
|
||||
function a($n) {
|
||||
$n2 = $n + 2;
|
||||
return floor(($n2 * (($n2 * $n2) + 1)) / 2);
|
||||
}
|
||||
|
||||
// Returns the order of the magic square whose magic constant is at least $x
|
||||
function inv_a($x) {
|
||||
return floor(pow(2 * $x, 1 / 3) + 1);
|
||||
}
|
||||
|
||||
echo("The first 20 magic constants are ");
|
||||
for ($n = 1; $n <= 20; $n++)
|
||||
echo(a($n)." ");
|
||||
echo(PHP_EOL);
|
||||
echo("The 1,000th magic constant is ".a(1000).PHP_EOL);
|
||||
$e = 1;
|
||||
for ($n = 1; $n <= 20; $n++) {
|
||||
$e *= 10;
|
||||
echo("10^".str_pad($n, 2, ' ', STR_PAD_LEFT));
|
||||
echo(": ".str_pad(inv_a($e), 9, ' ', STR_PAD_LEFT));
|
||||
echo(PHP_EOL);
|
||||
}
|
||||
?>
|
||||
62
Task/Magic-constant/REXX/magic-constant.rexx
Normal file
62
Task/Magic-constant/REXX/magic-constant.rexx
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
-- 8 May 2025
|
||||
include Settings
|
||||
|
||||
say 'MAGIC CONSTANT'
|
||||
say version
|
||||
say
|
||||
numeric digits 23
|
||||
call Getmagics 6e8
|
||||
call Task1
|
||||
call Task2
|
||||
call Task3
|
||||
exit
|
||||
|
||||
Getmagics:
|
||||
call Time('r')
|
||||
arg xx
|
||||
say 'Get magic constants up to' xx
|
||||
say Magics(xx) 'collected'
|
||||
say Format(Time('e'),,3) 'seconds'
|
||||
say
|
||||
return
|
||||
|
||||
Task1:
|
||||
call Time('r')
|
||||
say 'The first 25 magic constants are'
|
||||
do i = 3 to 28
|
||||
call Charout ,magi.i' '
|
||||
end
|
||||
say
|
||||
say Format(Time('e'),,3) 'seconds'
|
||||
say
|
||||
return
|
||||
|
||||
Task2:
|
||||
call Time('r')
|
||||
say 'The 1000th magic constant is'
|
||||
say magi.1002
|
||||
say Format(Time('e'),,3) 'seconds'
|
||||
say
|
||||
return
|
||||
|
||||
Task3:
|
||||
call Time('r')
|
||||
say 'Smallest magic square with constant > 10^n is'
|
||||
say Left('n',5) Right('order',8)
|
||||
say '--------------'
|
||||
e = 1
|
||||
do i = 3 to 6e7
|
||||
if Magic(i) > 1'E'e then do
|
||||
say Left('10^'e,5) Right(i,8)
|
||||
e = e+1
|
||||
end
|
||||
end
|
||||
say '--------------'
|
||||
say Format(Time('e'),,3) 'seconds'
|
||||
say
|
||||
return
|
||||
|
||||
include Functions
|
||||
include Sequences
|
||||
include Numbers
|
||||
include Abend
|
||||
21
Task/Magic-constant/Tiny-BASIC/magic-constant.basic
Normal file
21
Task/Magic-constant/Tiny-BASIC/magic-constant.basic
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
10 REM MAGIC CONSTANT
|
||||
20 PRINT "FIRST 20 MAGIC CONSTANTS:"
|
||||
30 LET N=3
|
||||
40 IF N>20+3-1 GOTO 80
|
||||
50 PRINT (N*N*N+N)/2,
|
||||
60 LET N=N+1
|
||||
70 GOTO 40
|
||||
80 PRINT
|
||||
90 PRINT "SMALLEST ORDER MAGIC SQUARE WITH A CONSTANT GREATER THAN:"
|
||||
100 LET T=10
|
||||
110 LET M=3
|
||||
120 LET X=1
|
||||
130 LET L=(M*M*M+M)/2
|
||||
140 LET M=M+1
|
||||
150 IF L<=T GOTO 130
|
||||
160 PRINT "10^";X;":",M-1
|
||||
170 LET X=X+1
|
||||
180 IF X>4 GOTO 210
|
||||
190 LET T=T*10
|
||||
200 GOTO 130
|
||||
210 END
|
||||
Loading…
Add table
Add a link
Reference in a new issue