How to resolve the algorithm Magic constant step by step in the C++ programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Magic constant step by step in the C++ programming language

Table of Contents

Problem Statement

A magic square is a square grid containing consecutive integers from 1 to N², arranged so that every row, column and diagonal adds up to the same number. That number is a constant. There is no way to create a valid N x N magic square that does not sum to the associated constant. A 3 x 3 magic square always sums to 15. A 4 x 4 magic square always sums to 34. Traditionally, the sequence leaves off terms for n = 0 and n = 1 as the magic squares of order 0 and 1 are trivial; and a term for n = 2 because it is impossible to form a magic square of order 2.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Magic constant step by step in the C++ programming language

This C++ program calculates and displays magic constants for magic squares. A magic square is a square grid filled with numbers such that the sum of the numbers in each row, column, and diagonal is the same constant. The magic constant for a magic square is the value of this constant.

The program includes several functions:

  • magicConstant(int32_t n): Returns the magic constant for a magic square of the given order n. The magic constant is calculated as n * ( n * n + 1 ) / 2.
  • minimumOrder(int32_t n): Returns the smallest order of a magic square such that its magic constant is greater than 10 to the given power n. The order is calculated as (int) exp( ( LN2 + n * LN10 ) / 3 ) + 1, where LN2 is the natural logarithm of 2 and LN10 is the natural logarithm of 10.
  • order(int32_t index): Returns the order of the magic square at the given index. The order of the first magic square is ORDER_FIRST_MAGIC_SQUARE, and the order of subsequent magic squares increases by 1 for each index.

In the main function:

  • The program displays the first 20 magic constants.
  • The program displays the 1,000th magic constant.
  • The program displays the order of the smallest magic square whose constant is greater than 10 to the given power for powers of 10 from 1 to 20.

The output of the program will look something like this:

The first 20 magic constants:
15 34 65 111 175 260 369 505 671 870 1105 1378 1692 2050 2455 2909 3414 3972 4585 5255

The 1,000th magic constant: 2730164

Order of the smallest magic square whose constant is greater than:
10^1:      4
10^2:      5
10^3:      6
10^4:      7
10^5:      8
10^6:      9
10^7:      10
10^8:      11
10^9:      12
10^10:     13
10^11:     14
10^12:     15
10^13:     16
10^14:     17
10^15:     18
10^16:     19
10^17:     20
10^18:     21
10^19:     22
10^20:     23

Source code in the cpp programming language

#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;
	}
}


  

You may also check:How to resolve the algorithm Subtractive generator step by step in the OCaml programming language
You may also check:How to resolve the algorithm Palindrome detection step by step in the FBSL programming language
You may also check:How to resolve the algorithm Tokenize a string step by step in the Simula programming language
You may also check:How to resolve the algorithm 24 game step by step in the ZX Spectrum Basic programming language
You may also check:How to resolve the algorithm Test integerness step by step in the Ruby programming language