Added functions to get a certain number of primes

This commit is contained in:
2021-03-10 12:23:03 -05:00
parent 1c47477213
commit 81fa373255
2 changed files with 124 additions and 5 deletions

View File

@@ -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));

View File

@@ -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
@@ -20,8 +20,9 @@ Copyright (C) 2020 Matthew Ellison
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import assert = require("assert");
import { arrayEquals, getAllFib, getAllFibBig, getFactors, getFactorsBig, getPrimes, getPrimesBig } from "./Algorithms";
import { arrayEquals, getAllFib, getAllFibBig, getFactors, getFactorsBig, getPrimes, getPrimesBig, getNumPrimes, getNumPrimesBig } from "./Algorithms";
function testGetAllFib(){
@@ -61,6 +62,22 @@ function testGetPrimes(){
console.log("getPrimes passed");
}
function testGetNumPrimes(){
//Test1
let correctAnswer = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];
let numPrimes = 25;
let answer = getNumPrimes(numPrimes);
assert.ok(arrayEquals(answer, correctAnswer), "getNumPrimes number failed");
//Test2
let bigCorrectAnswer = [2n, 3n, 5n, 7n, 11n, 13n, 17n, 19n, 23n, 29n, 31n, 37n, 41n, 43n, 47n, 53n, 59n, 61n, 67n, 71n, 73n, 79n, 83n, 89n, 97n];
let bigNumPrimes = 25n;
let bigAnswer = getNumPrimesBig(bigNumPrimes);
assert.ok(arrayEquals(bigAnswer, bigCorrectAnswer), "getNumPrimesBig failed");
console.log("getNumPrimes passed");
}
function testGetFactors(){
//Test 1
let correctAnswer = [2, 2, 5, 5];
@@ -86,4 +103,5 @@ function testGetFactors(){
//Run all of the tests
testGetAllFib();
testGetPrimes();
testGetNumPrimes();
testGetFactors();