Fixed O(n) runtime of "Find Largest Prime Factor" for high primes to 0(sqrt(n)).

This commit is contained in:
Sotisi 2019-05-13 12:58:18 +02:00 committed by danielyxie
parent a2551f98c2
commit 086fc67ecc

@ -65,16 +65,15 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
solver: (data: number, ans: string) => {
let fac: number = 2;
let n: number = data;
while (n > fac) {
if (n % fac === 0) {
while (Math.sqrt(n) > fac-1) {
while (n % fac === 0) {
n = Math.round(n / fac);
fac = 2;
} else {
++fac;
}
}
return fac === parseInt(ans, 10);
return (n===1?(fac-1):n) === parseInt(ans, 10);
},
},
{