mirror of
https://bitbucket.org/Mattrixwv/typescriptclasses.git
synced 2025-12-06 18:33:59 -05:00
289 lines
8.2 KiB
TypeScript
289 lines
8.2 KiB
TypeScript
//typescriptClasses/Algorithms.ts
|
|
//Matthew Ellison
|
|
// Created: 10-19-20
|
|
//Modified: 10-19-20
|
|
//This class holds many algorithms that I have found it useful to keep around
|
|
/*
|
|
Copyright (C) 2020 Matthew Ellison
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
|
|
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)){
|
|
return false;
|
|
}
|
|
//If they aren't the same length they aren't equal
|
|
else if(array1.length != array2.length){
|
|
return false;
|
|
}
|
|
else{
|
|
//Loop through every element to see if each one is equal
|
|
for(let cnt = 0;cnt < array1.length;++cnt){
|
|
//If any element in the same location is different return false
|
|
if(array1[cnt] != array2[cnt]){
|
|
return false;
|
|
}
|
|
}
|
|
//If every element was the same they are equal
|
|
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[] = [];
|
|
|
|
//If the number is <= 0 return an empty list
|
|
if(goalNumber <= 0){
|
|
return fibNums;
|
|
}
|
|
else if(goalNumber == 1){
|
|
fibNums.push(1);
|
|
return fibNums;
|
|
}
|
|
|
|
//This means that at least 2 1's are elements
|
|
fibNums.push(1);
|
|
fibNums.push(1);
|
|
|
|
//Loop to generate the rest of the Fibonacci numbers
|
|
while(fibNums[fibNums.length - 1] <= goalNumber){
|
|
fibNums.push((fibNums[fibNums.length - 1]) + (fibNums[fibNums.length - 2]));
|
|
}
|
|
|
|
//At this point the most recent number is > goalNumber, so remove it and return the rest of the list
|
|
fibNums.pop();
|
|
return fibNums;
|
|
}
|
|
export function getAllFibBig(goalNumber: bigint): bigint[]{
|
|
//Setup the variables
|
|
let fibNums:bigint[] = [];
|
|
|
|
//If the number is <= 0 return an empty list
|
|
if(goalNumber <= 0){
|
|
return fibNums;
|
|
}
|
|
else if(goalNumber == 1n){
|
|
fibNums.push(1n);
|
|
return fibNums;
|
|
}
|
|
|
|
//This means that at least 2 1's are elements
|
|
fibNums.push(1n);
|
|
fibNums.push(1n);
|
|
|
|
//Loop to generate the rest of the Fibonacci numbers
|
|
while(fibNums[fibNums.length - 1] <= goalNumber){
|
|
fibNums.push((fibNums[fibNums.length - 1]) + (fibNums[fibNums.length - 2]));
|
|
}
|
|
|
|
//At this point the most recent number is > goalNumber, so remove it and return the rest of the list
|
|
fibNums.pop();
|
|
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;
|
|
}
|