GeezerVison is showing github.com — npx standard --fix · Algorithms-Learn/JavaScript@856dc2f
File treeExpand file treeCollapse file tree Open diff view settings linear-algebra-javascript
Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +21-21Lines changed: 21 additions & 21 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -function euclideanGCDRecursive(first, second) { | 2 | | - /* | | 1 | +function euclideanGCDRecursive (first, second) { | | 2 | + /* | 3 | 3 | Calculates GCD of two numbers using Euclidean Recursive Algorithm | 4 | 4 | :param first: First number | 5 | 5 | :param second: Second number | 6 | 6 | :return: GCD of the numbers | 7 | 7 | */ | 8 | | - if (second === 0) { | 9 | | - return first; | 10 | | - } else { | 11 | | - return euclideanGCDRecursive(second, (first % second)); | 12 | | - } | | 8 | + if (second === 0) { | | 9 | + return first | | 10 | + } else { | | 11 | + return euclideanGCDRecursive(second, (first % second)) | | 12 | + } | 13 | 13 | } | 14 | 14 |
| 15 | | -function euclideanGCDIterative(first, second) { | 16 | | - /* | | 15 | +function euclideanGCDIterative (first, second) { | | 16 | + /* | 17 | 17 | Calculates GCD of two numbers using Euclidean Iterative Algorithm | 18 | 18 | :param first: First number | 19 | 19 | :param second: Second number | 20 | 20 | :return: GCD of the numbers | 21 | 21 | */ | 22 | | - while (second !== 0) { | 23 | | - let temp = second; | 24 | | - second = first % second; | 25 | | - first = temp; | 26 | | - } | 27 | | - return first; | | 22 | + while (second !== 0) { | | 23 | + const temp = second | | 24 | + second = first % second | | 25 | + first = temp | | 26 | + } | | 27 | + return first | 28 | 28 | } | 29 | 29 |
| 30 | | -function main() { | 31 | | - let first = 20; | 32 | | - let second = 30; | 33 | | - console.log('Recursive GCD for %d and %d is %d', first, second, euclideanGCDRecursive(first, second)); | 34 | | - console.log('Iterative GCD for %d and %d is %d', first, second, euclideanGCDIterative(first, second)); | | 30 | +function main () { | | 31 | + const first = 20 | | 32 | + const second = 30 | | 33 | + console.log('Recursive GCD for %d and %d is %d', first, second, euclideanGCDRecursive(first, second)) | | 34 | + console.log('Iterative GCD for %d and %d is %d', first, second, euclideanGCDIterative(first, second)) | 35 | 35 | } | 36 | 36 |
| 37 | | -main(); | | 37 | +main() |
Collapse file Copy file name to clipboard +17-17Lines changed: 17 additions & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | function KadaneAlgo (array) { | 2 | | - let cummulativeSum = 0 | 3 | | - let maxSum = 0 | 4 | | - for (var i = 0; i < array.length; i++) { | 5 | | - cummulativeSum = cummulativeSum + array[i] | 6 | | - if(cummulativeSum < 0 ) { | 7 | | - cummulativeSum = 0 | 8 | | - } | 9 | | - if (maxSum < cummulativeSum) { | 10 | | - maxSum = cummulativeSum | 11 | | - } | | 2 | + let cummulativeSum = 0 | | 3 | + let maxSum = 0 | | 4 | + for (var i = 0; i < array.length; i++) { | | 5 | + cummulativeSum = cummulativeSum + array[i] | | 6 | + if (cummulativeSum < 0) { | | 7 | + cummulativeSum = 0 | 12 | 8 | } | 13 | | - return maxSum | 14 | | - // This function returns largest sum contigous sum in a array | | 9 | + if (maxSum < cummulativeSum) { | | 10 | + maxSum = cummulativeSum | | 11 | + } | | 12 | + } | | 13 | + return maxSum | | 14 | + // This function returns largest sum contigous sum in a array | 15 | 15 | } | 16 | | -function main() { | 17 | | - var myArray = [1,2,3,4,-6] | 18 | | - var result = KadaneAlgo(myArray) | 19 | | - console.log(result) | | 16 | +function main () { | | 17 | + var myArray = [1, 2, 3, 4, -6] | | 18 | + var result = KadaneAlgo(myArray) | | 19 | + console.log(result) | 20 | 20 | } | 21 | | -main() | | 21 | +main() |
Collapse file Copy file name to clipboardExpand all lines: Algorithms/dynamic_programming/fibonacci.js +13-13Lines changed: 13 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | // of the input value n, it is exponential in the size of n as | 9 | 9 | // a function of the number of input bits | 10 | 10 |
| 11 | | -function fib(n) { | 12 | | - var table = []; | 13 | | - table.push(1); | 14 | | - table.push(1); | 15 | | - for (var i = 2; i < n; ++i) { | 16 | | - table.push(table[i - 1] + table[i - 2]); | 17 | | - } | 18 | | - console.log("Fibonacci #%d = %d", n, table[n - 1]); | | 11 | +function fib (n) { | | 12 | + var table = [] | | 13 | + table.push(1) | | 14 | + table.push(1) | | 15 | + for (var i = 2; i < n; ++i) { | | 16 | + table.push(table[i - 1] + table[i - 2]) | | 17 | + } | | 18 | + console.log('Fibonacci #%d = %d', n, table[n - 1]) | 19 | 19 | } | 20 | 20 |
| 21 | | -fib(1); | 22 | | -fib(2); | 23 | | -fib(200); | 24 | | -fib(5); | 25 | | -fib(10); | | 21 | +fib(1) | | 22 | +fib(2) | | 23 | +fib(200) | | 24 | +fib(5) | | 25 | +fib(10) |
Collapse file Copy file name to clipboard +21-21Lines changed: 21 additions & 21 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -function sieveOfEratosthenes(n) { | 2 | | - /* | | 1 | +function sieveOfEratosthenes (n) { | | 2 | + /* | 3 | 3 | * Calculates prime numbers till a number n | 4 | 4 | * :param n: Number upto which to calculate primes | 5 | 5 | * :return: A boolean list contaning only primes | 6 | 6 | */ | 7 | | - let primes = new Array(n + 1); | 8 | | - primes.fill(true); // set all as true initially | 9 | | - primes[0] = primes[1] = false; // Handling case for 0 and 1 | 10 | | - let sqrtn = Math.ceil(Math.sqrt(n)); | 11 | | - for (let i = 2; i <= sqrtn; i++) { | 12 | | - if (primes[i]) { | 13 | | - for (let j = 2 * i; j <= n; j += i) { | 14 | | - primes[j] = false; | 15 | | - } | 16 | | - } | | 7 | + const primes = new Array(n + 1) | | 8 | + primes.fill(true) // set all as true initially | | 9 | + primes[0] = primes[1] = false // Handling case for 0 and 1 | | 10 | + const sqrtn = Math.ceil(Math.sqrt(n)) | | 11 | + for (let i = 2; i <= sqrtn; i++) { | | 12 | + if (primes[i]) { | | 13 | + for (let j = 2 * i; j <= n; j += i) { | | 14 | + primes[j] = false | | 15 | + } | 17 | 16 | } | 18 | | - return primes; | | 17 | + } | | 18 | + return primes | 19 | 19 | } | 20 | 20 |
| 21 | | -function main() { | 22 | | - let n = 69; // number till where we wish to find primes | 23 | | - let primes = sieveOfEratosthenes(n); | 24 | | - for (let i = 2; i <= n; i++) { | 25 | | - if (primes[i]) { | 26 | | - console.log(i); | 27 | | - } | | 21 | +function main () { | | 22 | + const n = 69 // number till where we wish to find primes | | 23 | + const primes = sieveOfEratosthenes(n) | | 24 | + for (let i = 2; i <= n; i++) { | | 25 | + if (primes[i]) { | | 26 | + console.log(i) | 28 | 27 | } | | 28 | + } | 29 | 29 | } | 30 | 30 |
| 31 | | -main(); | | 31 | +main() |
Collapse file Copy file name to clipboardExpand all lines: Ciphers/caesarsCipher.js +16-18Lines changed: 16 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 11 | 11 | * @param {String} str - string to be decrypted | 12 | 12 | * @return {String} decrypted string | 13 | 13 | */ | 14 | | -function rot13(str) { | 15 | | - let response = []; | 16 | | - let strLength = str.length; | | 14 | +function rot13 (str) { | | 15 | + const response = [] | | 16 | + const strLength = str.length | 17 | 17 |
| 18 | | - for (let i = 0; i < strLength; i++) { | 19 | | - const char = str.charCodeAt(i); | 20 | | - | 21 | | - if (char < 65 || (char > 90 && char < 97) || char > 122) { | 22 | | - response.push(str.charAt(i)); | 23 | | - } else if ((char > 77 && char <= 90) || (char > 109 && char <= 122)) { | 24 | | - response.push(String.fromCharCode(str.charCodeAt(i) - 13)); | 25 | | - } else { | 26 | | - response.push(String.fromCharCode(str.charCodeAt(i) + 13)); | 27 | | - } | | 18 | + for (let i = 0; i < strLength; i++) { | | 19 | + const char = str.charCodeAt(i) | 28 | 20 |
| | 21 | + if (char < 65 || (char > 90 && char < 97) || char > 122) { | | 22 | + response.push(str.charAt(i)) | | 23 | + } else if ((char > 77 && char <= 90) || (char > 109 && char <= 122)) { | | 24 | + response.push(String.fromCharCode(str.charCodeAt(i) - 13)) | | 25 | + } else { | | 26 | + response.push(String.fromCharCode(str.charCodeAt(i) + 13)) | 29 | 27 | } | 30 | | - return response.join(""); | | 28 | + } | | 29 | + return response.join('') | 31 | 30 | } | 32 | 31 |
| 33 | | - | 34 | 32 | // Caesars Cipher Example | 35 | | -const encryptedString = "Uryyb Jbeyq"; | 36 | | -const decryptedString = rot13(encryptedString); | | 33 | +const encryptedString = 'Uryyb Jbeyq' | | 34 | +const decryptedString = rot13(encryptedString) | 37 | 35 |
| 38 | | -console.log(decryptedString); // Hello World | | 36 | +console.log(decryptedString) // Hello World |
|