Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions Algorithms/EucledianGCD.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
function euclideanGCDRecursive(first, second) {
/*
function euclideanGCDRecursive (first, second) {
/*
Calculates GCD of two numbers using Euclidean Recursive Algorithm
:param first: First number
:param second: Second number
:return: GCD of the numbers
*/
if (second === 0) {
return first;
} else {
return euclideanGCDRecursive(second, (first % second));
}
if (second === 0) {
return first
} else {
return euclideanGCDRecursive(second, (first % second))
}
}

function euclideanGCDIterative(first, second) {
/*
function euclideanGCDIterative (first, second) {
/*
Calculates GCD of two numbers using Euclidean Iterative Algorithm
:param first: First number
:param second: Second number
:return: GCD of the numbers
*/
while (second !== 0) {
let temp = second;
second = first % second;
first = temp;
}
return first;
while (second !== 0) {
const temp = second
second = first % second
first = temp
}
return first
}

function main() {
let first = 20;
let second = 30;
console.log('Recursive GCD for %d and %d is %d', first, second, euclideanGCDRecursive(first, second));
console.log('Iterative GCD for %d and %d is %d', first, second, euclideanGCDIterative(first, second));
function main () {
const first = 20
const second = 30
console.log('Recursive GCD for %d and %d is %d', first, second, euclideanGCDRecursive(first, second))
console.log('Iterative GCD for %d and %d is %d', first, second, euclideanGCDIterative(first, second))
}

main();
main()
34 changes: 17 additions & 17 deletions Algorithms/KadaneAlgo.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
function KadaneAlgo (array) {
let cummulativeSum = 0
let maxSum = 0
for (var i = 0; i < array.length; i++) {
cummulativeSum = cummulativeSum + array[i]
if(cummulativeSum < 0 ) {
cummulativeSum = 0
}
if (maxSum < cummulativeSum) {
maxSum = cummulativeSum
}
let cummulativeSum = 0
let maxSum = 0
for (var i = 0; i < array.length; i++) {
cummulativeSum = cummulativeSum + array[i]
if (cummulativeSum < 0) {
cummulativeSum = 0
}
return maxSum
// This function returns largest sum contigous sum in a array
if (maxSum < cummulativeSum) {
maxSum = cummulativeSum
}
}
return maxSum
// This function returns largest sum contigous sum in a array
}
function main() {
var myArray = [1,2,3,4,-6]
var result = KadaneAlgo(myArray)
console.log(result)
function main () {
var myArray = [1, 2, 3, 4, -6]
var result = KadaneAlgo(myArray)
console.log(result)
}
main()
main()
26 changes: 13 additions & 13 deletions Algorithms/dynamic_programming/fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
// of the input value n, it is exponential in the size of n as
// a function of the number of input bits

function fib(n) {
var table = [];
table.push(1);
table.push(1);
for (var i = 2; i < n; ++i) {
table.push(table[i - 1] + table[i - 2]);
}
console.log("Fibonacci #%d = %d", n, table[n - 1]);
function fib (n) {
var table = []
table.push(1)
table.push(1)
for (var i = 2; i < n; ++i) {
table.push(table[i - 1] + table[i - 2])
}
console.log('Fibonacci #%d = %d', n, table[n - 1])
}

fib(1);
fib(2);
fib(200);
fib(5);
fib(10);
fib(1)
fib(2)
fib(200)
fib(5)
fib(10)
42 changes: 21 additions & 21 deletions Algorithms/sieveOfEratosthenes.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
function sieveOfEratosthenes(n) {
/*
function sieveOfEratosthenes (n) {
/*
* Calculates prime numbers till a number n
* :param n: Number upto which to calculate primes
* :return: A boolean list contaning only primes
*/
let primes = new Array(n + 1);
primes.fill(true); // set all as true initially
primes[0] = primes[1] = false; // Handling case for 0 and 1
let sqrtn = Math.ceil(Math.sqrt(n));
for (let i = 2; i <= sqrtn; i++) {
if (primes[i]) {
for (let j = 2 * i; j <= n; j += i) {
primes[j] = false;
}
}
const primes = new Array(n + 1)
primes.fill(true) // set all as true initially
primes[0] = primes[1] = false // Handling case for 0 and 1
const sqrtn = Math.ceil(Math.sqrt(n))
for (let i = 2; i <= sqrtn; i++) {
if (primes[i]) {
for (let j = 2 * i; j <= n; j += i) {
primes[j] = false
}
}
return primes;
}
return primes
}

function main() {
let n = 69; // number till where we wish to find primes
let primes = sieveOfEratosthenes(n);
for (let i = 2; i <= n; i++) {
if (primes[i]) {
console.log(i);
}
function main () {
const n = 69 // number till where we wish to find primes
const primes = sieveOfEratosthenes(n)
for (let i = 2; i <= n; i++) {
if (primes[i]) {
console.log(i)
}
}
}

main();
main()
34 changes: 16 additions & 18 deletions Ciphers/caesarsCipher.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,26 @@
* @param {String} str - string to be decrypted
* @return {String} decrypted string
*/
function rot13(str) {
let response = [];
let strLength = str.length;
function rot13 (str) {
const response = []
const strLength = str.length

for (let i = 0; i < strLength; i++) {
const char = str.charCodeAt(i);

if (char < 65 || (char > 90 && char < 97) || char > 122) {
response.push(str.charAt(i));
} else if ((char > 77 && char <= 90) || (char > 109 && char <= 122)) {
response.push(String.fromCharCode(str.charCodeAt(i) - 13));
} else {
response.push(String.fromCharCode(str.charCodeAt(i) + 13));
}
for (let i = 0; i < strLength; i++) {
const char = str.charCodeAt(i)

if (char < 65 || (char > 90 && char < 97) || char > 122) {
response.push(str.charAt(i))
} else if ((char > 77 && char <= 90) || (char > 109 && char <= 122)) {
response.push(String.fromCharCode(str.charCodeAt(i) - 13))
} else {
response.push(String.fromCharCode(str.charCodeAt(i) + 13))
}
return response.join("");
}
return response.join('')
}


// Caesars Cipher Example
const encryptedString = "Uryyb Jbeyq";
const decryptedString = rot13(encryptedString);
const encryptedString = 'Uryyb Jbeyq'
const decryptedString = rot13(encryptedString)

console.log(decryptedString); // Hello World
console.log(decryptedString) // Hello World
Loading