primeTester
Written on August 25, 2019
문제
A prime number is a whole number that has no other divisors other than itself and 1. Write a function that accepts a number and returns true if it’s a prime number, false if it’s not.
풀이
const isMultiple = (arr, number) => {
for (let i = 0; i < arr.length; i++) {
if (number % arr[i] === 0) {
return true;
}
}
return false;
};
var primeTester = function(n) {
if (typeof n !== "number" || n < 1 || n % 1 !== 0) {
// n isn't a number or n is less than 1 or n is not an integer
return false;
}
let primeNums = primeSieve(2, n);
if (primeNums[primeNums.length - 1] === n) {
return true;
}
return false;
};
var primeSieve = function(start, end) {
let primeNums = [2, 3, 5, 7];
let newStart = start;
if (start > 10) {
newStart = 11;
for (let i = newStart; i <= end; i++) {
if (i % 10 === 1 || i % 10 === 3 || i % 10 === 7 || i % 10 === 9) {
// console.log(primeNums);
if (!isMultiple(primeNums, i)) {
primeNums.push(i);
}
}
}
}
return primeNums.filter(e => start <= e && end >= e);
};
👩🏻💻 배우는 것을 즐기는 프론트엔드 개발자 입니다
부족한 블로그에 방문해 주셔서 감사합니다 🙇🏻♀️
in the process of becoming the best version of myself