Data update
This commit is contained in:
parent
61b93a2cd1
commit
5af6d93694
858 changed files with 20572 additions and 2082 deletions
38
Task/Magic-constant/Java/magic-constant.java
Normal file
38
Task/Magic-constant/Java/magic-constant.java
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
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);
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue