Skip to content

Commit

Permalink
added primes
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJDufour committed Nov 24, 2022
1 parent b008973 commit 25c3789
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions primes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const compare_positive = require("./compare_positive.js");
const divide = require("./divide.js");
const is_integer = require("./is_integer.js");
const long_addition = require("./long_addition.js");

/**
*
* @param {String} start - numerical string
* @param {String} end - numerical end
* @returns {Array.<string>} primes - array of prime numbers as strings
*/
function primes(start = "0", end = "100") {
const prime_single_digits = ["2", "3", "5", "7", "11"];
const results = prime_single_digits.filter(n => compare_positive(n, start) !== "<" && compare_positive(n, end) !== ">");

let num = "13";

while (compare_positive(num, end) !== ">") {
// don't even bother checking if ends with 5 or all one number
if (!(/^\d+5/.test(num) || /^(\d)\1+/.test(num))) {
if (["9", "7", "3"].every(digit => !is_integer(divide(num, digit)))) {
results.push(num);
}
}
num = long_addition(num, "2");
}
return results;
}

module.exports = primes;
module.exports.default = primes;

0 comments on commit 25c3789

Please sign in to comment.