mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-12-19 06:58:15 +08:00
npx standard --fix
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user