mirror of
https://bitbucket.org/Mattrixwv/typescriptclasses.git
synced 2025-12-06 18:33:59 -05:00
Added isPrime function
This commit is contained in:
@@ -319,6 +319,35 @@ export function getNumPrimesBig(numberOfPrimes: bigint): bigint[]{
|
||||
return primes;
|
||||
}
|
||||
|
||||
export function isPrime(possiblePrime: number): boolean{
|
||||
if(possiblePrime <= 3){
|
||||
return possiblePrime > 1;
|
||||
}
|
||||
else if(((possiblePrime % 2) == 0) || ((possiblePrime % 3) == 0)){
|
||||
return false;
|
||||
}
|
||||
for(let cnt: number = 5;(cnt * cnt) <= possiblePrime; cnt += 6){
|
||||
if(((possiblePrime % cnt) == 0) || ((possiblePrime % (cnt + 2)) == 0)){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
export function isPrimeBig(possiblePrime: bigint): boolean{
|
||||
if(possiblePrime <= 3n){
|
||||
return possiblePrime > 1n;
|
||||
}
|
||||
else if(((possiblePrime % 2n) == 0n) || ((possiblePrime % 3n) == 0n)){
|
||||
return false;
|
||||
}
|
||||
for(let cnt : bigint = 5n;(cnt * cnt) <= possiblePrime;cnt += 6n){
|
||||
if(((possiblePrime % cnt) == 0n) || ((possiblePrime % (cnt + 2n)) == 0n)){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
export function getFactors(goalNumber: number): number[]{
|
||||
//You need to get all the primes that could be factors of this number so you can test them
|
||||
let topPossiblePrime: number = Math.ceil(Math.sqrt(goalNumber));
|
||||
|
||||
Reference in New Issue
Block a user