alboig

AboveTheMainDiagonal - JavascriptModule0

Apr 16th, 2020
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Above the Main Diagonal - 100 out of 100, only if N is BigInt :) Otherwise - 93.33 out of 100
  2. // You are given a square matrix of numbers, formed by powers of 2. See example
  3.  
  4. // Example: N = 4
  5.  
  6. //  1  2  4  8
  7. //  2  4  8 16
  8. //  4  8 16 32
  9. //  8 16 32 64
  10. // You task is to find the sum above the main diagonal
  11. // The result is: 2 + 4 + 8 + 8 + 16 + 32 = 70
  12. // Input
  13. // Read from the standard input
  14. // On the first line, read the number N
  15. // The number of rows and columns
  16. // Output
  17. // Print to the standard output
  18. // On the first line, print the sum of the numbers above the main diagonal except the diagonal
  19. // Sample tests
  20. // Input
  21. // 4
  22. // Output
  23. // 70
  24. const input = ['5'];
  25. const print = this.print || console.log;
  26. const gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  27. let n = +gets();
  28. let sum = 0;
  29. let arr;
  30. const a1 = [];
  31. for (let i = 0; i <= n - 1; i++) {
  32.   arr = [];
  33.   for (let j = 0; j <= n - 1; j++) {
  34.     arr.push(Math.pow(2, j));
  35.   }
  36.   a1.push(arr.map(x => x * Math.pow(2, i)));
  37. }
  38. // print(a1); just checking how numbers look like
  39. n = BigInt(n);
  40. sum = BigInt(sum);
  41. for (let i = BigInt(0); i < n - BigInt(1); i++) {
  42.   for (let j = BigInt(i) + BigInt(1); j <= n - BigInt(1); j++) {
  43.     sum += BigInt(a1[i][j]);
  44.   }
  45. }
  46. print(String(sum));
Advertisement
Add Comment
Please, Sign In to add comment