UNPKG

sussy-util

Version:
45 lines (44 loc) 1.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PrimeNumbers = void 0; const _1 = require("."); class PrimeNumbers { constructor() { this.current = 0; this.primes = []; } /** * 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) { for (let i = this.current; i <= num; i++) { if (i % 2 === 0 || !_1.IsSomething.isPrime(num)) continue; this.primes.push(i); } this.current = num; return this.primes; } return this.primes.filter(e => e <= num); } /** * 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; } } exports.PrimeNumbers = PrimeNumbers; exports.default = PrimeNumbers;