mirror of
https://bitbucket.org/Mattrixwv/typescriptclasses.git
synced 2025-12-06 18:33:59 -05:00
Added getPrimes and getFactors
This commit is contained in:
200
Algorithms.ts
200
Algorithms.ts
@@ -21,6 +21,10 @@ Copyright (C) 2020 Matthew Ellison
|
||||
*/
|
||||
|
||||
|
||||
import { kMaxLength } from "buffer";
|
||||
import { InvalidResult } from "./InvalidResult";
|
||||
|
||||
|
||||
export function arrayEquals(array1: any[], array2: any[]): boolean{
|
||||
//If they aren't the same type they aren't equal
|
||||
if((typeof array1) != (typeof array2)){
|
||||
@@ -42,6 +46,25 @@ export function arrayEquals(array1: any[], array2: any[]): boolean{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
export function sqrtBig(value: bigint): bigint{
|
||||
if(value < 0n){
|
||||
throw "Negative numbers are not supported";
|
||||
}
|
||||
|
||||
let k = 2n;
|
||||
let o = 0n;
|
||||
let x = value;
|
||||
let limit = 100;
|
||||
|
||||
while(x ** k !== k && x !== o && --limit){
|
||||
o = x;
|
||||
x = ((k - 1n) * x + value / x ** (k - 1n)) / k;
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
export function getAllFib(goalNumber: number): number[]{
|
||||
//Setup the variables
|
||||
let fibNums: number[] = [];
|
||||
@@ -76,14 +99,14 @@ export function getAllFibBig(goalNumber: bigint): bigint[]{
|
||||
if(goalNumber <= 0){
|
||||
return fibNums;
|
||||
}
|
||||
else if(goalNumber == BigInt(1)){
|
||||
fibNums.push(BigInt(1));
|
||||
else if(goalNumber == 1n){
|
||||
fibNums.push(1n);
|
||||
return fibNums;
|
||||
}
|
||||
|
||||
//This means that at least 2 1's are elements
|
||||
fibNums.push(BigInt(1));
|
||||
fibNums.push(BigInt(1));
|
||||
fibNums.push(1n);
|
||||
fibNums.push(1n);
|
||||
|
||||
//Loop to generate the rest of the Fibonacci numbers
|
||||
while(fibNums[fibNums.length - 1] <= goalNumber){
|
||||
@@ -95,3 +118,172 @@ export function getAllFibBig(goalNumber: bigint): bigint[]{
|
||||
return fibNums;
|
||||
}
|
||||
|
||||
export function getPrimes(goalNumber: number): number[]{
|
||||
let primes: number[] = []; //Holds the prime numbers
|
||||
let foundFactor: boolean = false; //A flag for whether a factor of the current number has been found
|
||||
|
||||
//If the number is 0 or a negative return an empty list
|
||||
if(goalNumber <= 1){
|
||||
return primes;
|
||||
}
|
||||
//Optherwise the number is at least 2, so 2 should be added to the list
|
||||
else{
|
||||
primes.push(2);
|
||||
}
|
||||
|
||||
//We can now start at 3 and skip all even numbers, because they cannot be prime
|
||||
for(let possiblePrime: number = 3;possiblePrime <= goalNumber;possiblePrime += 2){
|
||||
//Check all current primes, up to sqrt(possiblePrime), to see if there is a divisor
|
||||
let topPossibleFactor: number = Math.ceil(Math.sqrt(possiblePrime));
|
||||
//We can safely assume that there will be at least 1 element in the primes list because of 2 being added before this
|
||||
for(let primesCnt: number = 0;primes[primesCnt] <= topPossibleFactor;){
|
||||
if((possiblePrime % primes[primesCnt]) == 0){
|
||||
foundFactor = true;
|
||||
break;
|
||||
}
|
||||
else{
|
||||
++primesCnt;
|
||||
}
|
||||
//Check if the index has gone out of range
|
||||
if(primesCnt >= primes.length){
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//If you didn't find a factor then the current number must be prime
|
||||
if(!foundFactor){
|
||||
primes.push(possiblePrime);
|
||||
}
|
||||
else{
|
||||
foundFactor = false;
|
||||
}
|
||||
}
|
||||
|
||||
//Sort the list before returning it
|
||||
primes = primes.sort((n1, n2) => n1 - n2);
|
||||
return primes;
|
||||
}
|
||||
export function getPrimesBig(goalNumber: bigint): bigint[]{
|
||||
let primes: bigint[] = []; //Holds the prime numbers
|
||||
let foundFactor: boolean = false; //A flag for whether a factor of the current number has been found
|
||||
|
||||
//If the number is 0 or a negative return an empty list
|
||||
if(goalNumber <= 1){
|
||||
return primes;
|
||||
}
|
||||
//Optherwise the number is at least 2, so 2 should be added to the list
|
||||
else{
|
||||
primes.push(2n);
|
||||
}
|
||||
|
||||
//We can now start at 3 and skip all even numbers, because they cannot be prime
|
||||
for(let possiblePrime: bigint = 3n;possiblePrime <= goalNumber;possiblePrime += 2n){
|
||||
//Check all current primes, up to sqrt(possiblePrime), to see if there is a divisor
|
||||
let topPossibleFactor: bigint = sqrtBig(possiblePrime);
|
||||
//We can safely assume that there will be at least 1 element in the primes list because of 2 being added before this
|
||||
for(let primesCnt: number = 0;primes[primesCnt] <= topPossibleFactor;){
|
||||
if((possiblePrime % primes[primesCnt]) == 0n){
|
||||
foundFactor = true;
|
||||
break;
|
||||
}
|
||||
else{
|
||||
++primesCnt;
|
||||
}
|
||||
//Check if the index has gone out of range
|
||||
if(primesCnt >= primes.length){
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//If you didn't find a factor then the current number must be prime
|
||||
if(!foundFactor){
|
||||
primes.push(possiblePrime);
|
||||
}
|
||||
else{
|
||||
foundFactor = false;
|
||||
}
|
||||
}
|
||||
|
||||
//Sort the list before returning it
|
||||
primes = primes.sort(function(n1, n2){
|
||||
if(n1 > n2){
|
||||
return 1;
|
||||
}
|
||||
else if(n1 < n2){
|
||||
return -1;
|
||||
}
|
||||
else{
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
return primes;
|
||||
}
|
||||
|
||||
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));
|
||||
let primes: number[] = getPrimes(topPossiblePrime);
|
||||
let factors: number[] = [];
|
||||
|
||||
//You need to step through each prime and see if it is a factor in the number
|
||||
for(let cnt: number = 0;cnt < primes.length;){
|
||||
//If the prime is a factor you need to add it to the factor list
|
||||
if((goalNumber % primes[cnt]) == 0){
|
||||
factors.push(primes[cnt]);
|
||||
goalNumber /= primes[cnt];
|
||||
}
|
||||
//Otherwise advance the location in primes you are looking at
|
||||
//By not advancing f the prime is a factor you allow for multiple of the same prime number as a factor
|
||||
else{
|
||||
++cnt;
|
||||
}
|
||||
}
|
||||
|
||||
//If you didn't get any factors the number itself must be a prime
|
||||
if(factors.length == 0){
|
||||
factors.push(goalNumber);
|
||||
goalNumber /= goalNumber;
|
||||
}
|
||||
|
||||
//If for some reason the goalNumber is not 1 throw an exception
|
||||
if(goalNumber != 1){
|
||||
throw new InvalidResult("The factor was not 1: " + goalNumber);
|
||||
}
|
||||
|
||||
//Return the list of factors
|
||||
return factors;
|
||||
}
|
||||
export function getFactorsBig(goalNumber: bigint): bigint[]{
|
||||
//You need to get all the primes that could be factors of this number so you can test them
|
||||
let topPossiblePrime: bigint = sqrtBig(goalNumber);
|
||||
let primes: bigint[] = getPrimesBig(topPossiblePrime);
|
||||
let factors: bigint[] = [];
|
||||
|
||||
//You need to step through each prime and see if it is a factor in the number
|
||||
for(let cnt: number = 0;cnt < primes.length;){
|
||||
//If the prime is a factor you need to add it to the factor list
|
||||
if((goalNumber % primes[cnt]) == 0n){
|
||||
factors.push(primes[cnt]);
|
||||
goalNumber /= primes[cnt];
|
||||
}
|
||||
//Otherwise advance the location in primes you are looking at
|
||||
//By not advancing f the prime is a factor you allow for multiple of the same prime number as a factor
|
||||
else{
|
||||
++cnt;
|
||||
}
|
||||
}
|
||||
|
||||
//If you didn't get any factors the number itself must be a prime
|
||||
if(factors.length == 0){
|
||||
factors.push(goalNumber);
|
||||
goalNumber /= goalNumber;
|
||||
}
|
||||
|
||||
//If for some reason the goalNumber is not 1 throw an exception
|
||||
if(goalNumber != 1n){
|
||||
throw new InvalidResult("The factor was not 1: " + goalNumber);
|
||||
}
|
||||
|
||||
//Return the list of factors
|
||||
return factors;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user