Jump to content

Magic constant: Difference between revisions

New post.
(Used logarithm to avoid overflow in the stretch task.)
(New post.)
Line 706:
19 2714418 10000004237431278525
20 5848036 100000026858987459346</syntaxhighlight>
 
=={{header|J}}==
<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 aIndex + 2;
}
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}}==
876

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.