Created
May 21, 2019 00:41
-
-
Save rahpuser/3adee9c47084dc20e85ddc7b5c1caf22 to your computer and use it in GitHub Desktop.
bla bla
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const N = process.argv[2]; | |
const NS_PER_SEC = 1e9; | |
function mcd(a, b) { | |
let aa = a; | |
let bb = b; | |
while (bb !== BigInt(0)) { | |
const temp = bb; | |
bb = aa % bb; | |
aa = temp; | |
} | |
return aa; | |
} | |
function mcm(a, b) { | |
return (a * b) / mcd(a, b); | |
} | |
function getMinimum(number) { | |
let i = BigInt(2); | |
let multiplo = BigInt(1); | |
while (i <= BigInt(number)) { | |
if (multiplo % i !== 0) multiplo = mcm(multiplo, i); | |
i++; | |
} | |
return multiplo; | |
} | |
const time = process.hrtime(); | |
const total = getMinimum(N); | |
const diffTime2 = process.hrtime(time); | |
// console.log("solution:", total); | |
console.log( | |
`Solution took ${diffTime2[0] * NS_PER_SEC + | |
diffTime2[1]} nanoseconds or ${diffTime2[0] + | |
diffTime2[1] / NS_PER_SEC} seconds ` | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const N = process.argv[2]; | |
const NS_PER_SEC = 1e9; | |
const getSumDiff1 = number => { | |
return ( | |
(number / 12) * | |
(3 * Math.pow(number, 3) + 2 * Math.pow(number, 2) - 3 * number - 2) | |
); | |
}; | |
const time = process.hrtime(); | |
const total = getSumDiff1(N); | |
const diffTime2 = process.hrtime(time); | |
console.log("solution:", total); | |
console.log( | |
`Solution took ${diffTime2[0] * NS_PER_SEC + | |
diffTime2[1]} nanoseconds or ${diffTime2[0] + | |
diffTime2[1] / NS_PER_SEC} seconds ` | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const N = process.argv[2]; | |
const NS_PER_SEC = 1e9; | |
const getNpositivee = n => 6 * n + 1; | |
const getNnegative = n => 6 * n - 1; | |
const primes = [2, 3, 5]; | |
const isPrime = number => { | |
if ([2, 3, 5].includes(number)) return true; | |
if (number % 2 === 0 || number % 5 === 0) return false; | |
let isCompoundNumber = false; | |
const limit = Math.sqrt(number) + 1; | |
for (let i = 0; !!primes[i] && primes[i] < limit; i++) { | |
if (number % primes[i] === 0) { | |
isCompoundNumber = true; | |
break; | |
} | |
} | |
return !isCompoundNumber; | |
}; | |
const getPrime = Index => { | |
if (primes[Index]) return primes[Index]; | |
let n = 1; | |
let primeIteratorPositive = getNpositivee(n); | |
let primeIteratorNegative = getNnegative(n); | |
while (true) { | |
primeIteratorNegative = getNnegative(n); | |
if (isPrime(primeIteratorNegative)) { | |
primes.push(primeIteratorNegative); | |
if (primes.length - 1 === Index) return primeIteratorNegative; | |
} | |
primeIteratorPositive = getNpositivee(n); | |
if (isPrime(primeIteratorPositive)) { | |
primes.push(primeIteratorPositive); | |
if (primes.length - 1 === Index) return primeIteratorPositive; | |
} | |
n++; | |
} | |
}; | |
const time = process.hrtime(); | |
const total = getPrime(Number(N) - 1); | |
const diffTime2 = process.hrtime(time); | |
console.log("solution:", total); | |
console.log( | |
`Solution took ${diffTime2[0] * NS_PER_SEC + | |
diffTime2[1]} nanoseconds or ${diffTime2[0] + | |
diffTime2[1] / NS_PER_SEC} seconds ` | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment