Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Above the Main Diagonal - 100 out of 100, only if N is BigInt :) Otherwise - 93.33 out of 100
- // You are given a square matrix of numbers, formed by powers of 2. See example
- // Example: N = 4
- // 1 2 4 8
- // 2 4 8 16
- // 4 8 16 32
- // 8 16 32 64
- // You task is to find the sum above the main diagonal
- // The result is: 2 + 4 + 8 + 8 + 16 + 32 = 70
- // Input
- // Read from the standard input
- // On the first line, read the number N
- // The number of rows and columns
- // Output
- // Print to the standard output
- // On the first line, print the sum of the numbers above the main diagonal except the diagonal
- // Sample tests
- // Input
- // 4
- // Output
- // 70
- const input = ['5'];
- const print = this.print || console.log;
- const gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
- let n = +gets();
- let sum = 0;
- let arr;
- const a1 = [];
- for (let i = 0; i <= n - 1; i++) {
- arr = [];
- for (let j = 0; j <= n - 1; j++) {
- arr.push(Math.pow(2, j));
- }
- a1.push(arr.map(x => x * Math.pow(2, i)));
- }
- // print(a1); just checking how numbers look like
- n = BigInt(n);
- sum = BigInt(sum);
- for (let i = BigInt(0); i < n - BigInt(1); i++) {
- for (let j = BigInt(i) + BigInt(1); j <= n - BigInt(1); j++) {
- sum += BigInt(a1[i][j]);
- }
- }
- print(String(sum));
Advertisement
Add Comment
Please, Sign In to add comment