mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-12-19 06:58:15 +08:00
Re-orgainze files and folders in the repository (#172)
* Re-orgainze files and folders in the repository
* updating DIRECTORY.md
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@@ -1,37 +0,0 @@
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
const temp = second
|
||||
second = first % second
|
||||
first = temp
|
||||
}
|
||||
return first
|
||||
}
|
||||
|
||||
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()
|
||||
@@ -1,21 +0,0 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
main()
|
||||
@@ -1,31 +0,0 @@
|
||||
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
|
||||
*/
|
||||
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
|
||||
}
|
||||
|
||||
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()
|
||||
Reference in New Issue
Block a user