Advertisement
bai_onzi

aboveTheMainDiagonal.js

Apr 5th, 2023
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //col      0  1  2  3
  2. //row 0 -  1  2  4  8
  3. //row 1 -  2  4  8 16
  4. //row 2 -  4  8 16 32
  5. //row 3 -  8 16 32 64
  6.  
  7. let matN = +gets();
  8. let rows = matN;
  9. let cols = matN;
  10. let matrix = [];
  11. let sum = 0;
  12.  
  13. for(let row = 0; row < rows; row++){
  14.     matrix[row] = [];
  15.     for(let col = 0; col < cols; col++){
  16.         matrix[row][col] = 2**(row + col)
  17.     }
  18. }
  19. console.log(matrix);
  20. for(let firstCount = matN - 1; firstCount > 0; firstCount--){
  21.     for(let row = firstCount; row < matN; row++){
  22.         for(let col = 0; col < matN; col++){
  23.         if((row-col) === firstCount){
  24.             sum = sum + (matrix[row][col]);
  25.         }
  26.         }
  27.     }
  28. }
  29. console.log(sum);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement