mirror of
https://bitbucket.org/Mattrixwv/typescriptclasses.git
synced 2025-12-06 18:33:59 -05:00
Added Sieve of Eratosthenes
This commit is contained in:
@@ -22,8 +22,9 @@ Copyright (C) 2021 Matthew Ellison
|
||||
|
||||
|
||||
import assert = require("assert");
|
||||
import { nextTick } from "process";
|
||||
import { arrayEquals, getAllFib, getAllFibBig, getFactors, getFactorsBig, getPrimes, getPrimesBig, getNumPrimes, getNumPrimesBig,
|
||||
sqrtBig, getSum, getSumBig, getProd, getProdBig, getDivisors, getDivisorsBig, factorial, factorialBig, isPalindrome, toBin, toBinBig, isPrime, isPrimeBig } from "./Algorithms";
|
||||
sqrtBig, getSum, getSumBig, getProd, getProdBig, getDivisors, getDivisorsBig, factorial, factorialBig, isPalindrome, toBin, toBinBig, isPrime, isPrimeBig, sieveOfEratosthenes, sieveOfEratosthenesBig } from "./Algorithms";
|
||||
|
||||
|
||||
function testArrayEquals(){
|
||||
@@ -95,6 +96,34 @@ function testSqrtBig(){
|
||||
console.log("testSqrtBig passed");
|
||||
}
|
||||
|
||||
function testSieveOfEratosthenes(){
|
||||
//Test 1
|
||||
let sieve = sieveOfEratosthenes();
|
||||
let correctAnswer: number[] = [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 answer: number[] = [];
|
||||
for(let cnt = 0;cnt < 25;++cnt){
|
||||
let prime = sieve.next().value;
|
||||
if(typeof prime == "number"){
|
||||
answer.push(prime);
|
||||
}
|
||||
}
|
||||
assert.ok(arrayEquals(answer, correctAnswer), "sieveOfEratosthenes failed");
|
||||
|
||||
//Test 2
|
||||
let bigSieve = sieveOfEratosthenesBig();
|
||||
let bigCorrectAnswer: bigint[] = [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 bigAnswer: bigint[] = [];
|
||||
for(let cnt = 0;cnt < 25;++cnt){
|
||||
let prime = bigSieve.next().value;
|
||||
if(typeof prime == "bigint"){
|
||||
bigAnswer.push(prime);
|
||||
}
|
||||
}
|
||||
assert.ok(arrayEquals(bigAnswer, bigCorrectAnswer), "sieveOfEratosthenes big failed");
|
||||
|
||||
console.log("sieveOfEratosthenes passed");
|
||||
}
|
||||
|
||||
function testGetAllFib(){
|
||||
//Test 1
|
||||
let correctAnswer = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89];
|
||||
@@ -359,6 +388,7 @@ function testToBin(){
|
||||
//Run all of the tests
|
||||
testArrayEquals();
|
||||
testSqrtBig();
|
||||
testSieveOfEratosthenes();
|
||||
testGetAllFib();
|
||||
testGetPrimes();
|
||||
testGetNumPrimes();
|
||||
|
||||
Reference in New Issue
Block a user