Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Primes to N
- const input = ['24'];
- const print = this.print || console.log;
- const gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
- const N = +gets();
- const arrN = [];
- const arrPrimes = [];
- function isPrime(n) {
- if (n === 1) {
- return true;
- } else if (n === 2) {
- return true;
- } else {
- for (var i = 2; i <= Math.sqrt(n); i++) {
- if (n % i === 0) {
- return false;
- }
- }
- return true;
- }
- }
- if (N >= 1 && N <= 1024) {
- for (let i = 1; i <= N; i++) {
- arrN.push(i);
- }
- for (let i = 0; i < arrN.length; i++) {
- if (isPrime(arrN[i]) === true) {
- arrPrimes.push(arrN[i]);
- }
- }
- }
- print(arrPrimes.toString().replace(/,/g, ' '));
Advertisement
Add Comment
Please, Sign In to add comment