mirror of
https://bitbucket.org/Mattrixwv/typescriptclasses.git
synced 2025-12-06 18:33:59 -05:00
Added functions to get a certain number of primes
This commit is contained in:
105
Algorithms.ts
105
Algorithms.ts
@@ -1,10 +1,10 @@
|
||||
//typescriptClasses/Algorithms.ts
|
||||
//Matthew Ellison
|
||||
// Created: 10-19-20
|
||||
//Modified: 10-19-20
|
||||
//Modified: 03-10-21
|
||||
//This class holds many algorithms that I have found it useful to keep around
|
||||
/*
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
Copyright (C) 2021 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
|
||||
@@ -218,6 +218,107 @@ export function getPrimesBig(goalNumber: bigint): bigint[]{
|
||||
return primes;
|
||||
}
|
||||
|
||||
export function getNumPrimes(numberOfPrimes: 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 negative return an empty list
|
||||
if(numberOfPrimes <= 1){
|
||||
return primes;
|
||||
}
|
||||
//Otherwise 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 number, because they cannot be prime
|
||||
for(let possiblePrime: number = 3;primes.length < numberOfPrimes;possiblePrime += 2){
|
||||
//Check all the 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 by default
|
||||
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 bounds
|
||||
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 getNumPrimesBig(numberOfPrimes: 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 negative return an empty list
|
||||
if(numberOfPrimes <= 1){
|
||||
return primes;
|
||||
}
|
||||
//Otherwise 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 number, because theyu cannot be prime
|
||||
for(let possiblePrime: bigint = 3n;primes.length < numberOfPrimes;possiblePrime += 2n){
|
||||
//Check all the current primes, up to sqrt(possiblePrime), to see if there is a divisor
|
||||
let topPossibleFactor: bigint = sqrtBig(possiblePrime);
|
||||
//We can safely assume that ther ewill be at least 1 element in the primes list because of 2 being added by default
|
||||
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 bounds
|
||||
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));
|
||||
|
||||
Reference in New Issue
Block a user