sussy-util
Version:
Util package made by me
52 lines (51 loc) • 1.48 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrimeNumbers = void 0;
const _1 = require(".");
class PrimeNumbers {
constructor() {
this.current = 1;
this.primes = [2];
}
/**
* It returns an array of prime numbers up to a given number.
* @param {number} num - number - The number to get the primes till.
* @returns The primes array.
*/
getTill(num) {
if (num <= this.current) {
return this.primes.slice(0, this.primes.findIndex((e) => e >= num));
}
for (let i = this.current + 1; i <= num; i++) {
if (i % 2 !== 0 && _1.IsSomething.isPrime(i)) {
this.primes.push(i);
}
}
this.current = num;
return [...this.primes];
}
/**
* adds n amount of prime numbers to the cache and returns it
* @param {number} num - number - The number of primes to add to the list.
* @returns The array of primes.
*/
addPrimes(num) {
while (num > 0) {
this.current++;
if (_1.IsSomething.isPrime(this.current)) {
this.primes.push(this.current);
num--;
}
}
return this.primes;
}
/**
* Resets the state of the PrimeNumbers instance.
*/
reset() {
this.current = 1;
this.primes = [2];
}
}
exports.PrimeNumbers = PrimeNumbers;
exports.default = PrimeNumbers;