
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
JavaScript Program to Generate a matrix having sum of secondary diagonal equal to a perfect square
We will write a JavaScript program that generates a matrix with the sum of the secondary diagonal being a perfect square. Our program will use nested loops to traverse the matrix and calculate the sum of the secondary diagonal elements. We will then use the Math.sqrt() method to find the square root of the sum and check if it is a whole number. If it is, we will consider the sum to be a perfect square.
Approach
The approach to generate a matrix with the sum of secondary diagonal equal to a perfect square is as follows −
Create a two-dimensional array of size n x n where n is the size of the square matrix.
Fill the matrix with random numbers between 1 and 100.
Calculate the sum of the secondary diagonal of the matrix.
Check if the sum is a perfect square or not. If it's not a perfect square, generate a new matrix and repeat the steps 2 to 4.
Return the matrix with the secondary diagonal sum equal to a perfect square.
To check if the number is a perfect square, you can use the Math.sqrt() function and compare its result with the integer value of the square root.
Example
Here is an example of a JavaScript program that generates a matrix with the sum of its secondary diagonal equal to a perfect square −
function generateMatrix(n) { let matrix = []; for (let i = 0; i < n; i++) { matrix[i] = []; for (let j = 0; j < n; j++) { matrix[i][j] = i * n + j + 1; } } let sum = 0; for (let i = 0; i < n; i++) { sum += matrix[i][n - i - 1]; } let squareRoot = Math.floor(Math.sqrt(sum)); if (squareRoot * squareRoot !== sum) { return null; } return matrix; } const n = 1; console.log(generateMatrix(n));
Explanation
The generateMatrix function takes in a single argument n which represents the size of the matrix to be generated.
The function initializes an empty 2D array matrix and loops through each row and column to fill the matrix with numbers i * n + j + 1, where i is the row number and j is the column number.
The function calculates the sum of the secondary diagonal of the matrix by looping through each row and column, adding up the values at the indices (i, n - i - 1), where i is the row number.
The function calculates the square root of the sum and rounds down to the nearest integer. If the square of this integer is not equal to the sum, the function returns null, indicating that the sum is not a perfect square.
If the sum is a perfect square, the function returns the generated matrix.
- Related Articles
- Program to find diagonal sum of a matrix in Python
- Python Program to Remove First Diagonal Elements from a Square Matrix
- Golang program to calculate the sum of left diagonal matrix
- Python Program to calculate the sum of right diagonal the matrix
- Diagonal product of a matrix - JavaScript
- Program to convert given Matrix to a Diagonal Matrix in C++
- Swift Program to calculate the sum of right diagonal of the matrix
- Swift Program to calculate the sum of left diagonal of the matrix
- Python Program to calculate the sum of left diagonal of the matrix
- Filling diagonal to make the sum of every row, column and diagonal equal of 3×3 matrix using c++
- Program to print a matrix in Diagonal Pattern.
- Find Largest & Smallest Element in Primary and Secondary Diagonal of a Matrix in Java
- JavaScript Program for Mirror of matrix across diagonal
- 8086 program to find the square root of a perfect square root number
- C Program to find sum of perfect square elements in an array using pointers.
