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