JavaScript: Sort an array of all prime numbers between 1 and a given integer
JavaScript Basic: Exercise-109 with Solution
Write a JavaScript program to sort an array of all prime numbers between 1 and a given integer.
Sample Solution:
JavaScript Code:
// Function to sort prime numbers up to 'num'
function sort_prime(num) {
var prime_num1 = [], // Array to store prime numbers
prime_num2 = []; // Array to track prime status (true/false)
// Initialize prime_num2 array to true for all indices from 0 to num
for (var i = 0; i <= num; i++) {
prime_num2.push(true);
}
// Loop to find prime numbers using the Sieve of Eratosthenes algorithm
for (var i = 2; i <= num; i++) {
if (prime_num2[i]) {
prime_num1.push(i); // Push the current number to prime_num1 if it's a prime
// Mark multiples of the current number as non-prime in prime_num2
for (var j = 1; i * j <= num; j++) {
prime_num2[i * j] = false;
}
}
}
return prime_num1; // Return the array containing sorted prime numbers
}
// Example usage of the sort_prime function with different values
console.log(sort_prime(5)); // Output: [2, 3, 5]
console.log(sort_prime(11)); // Output: [2, 3, 5, 7, 11]
console.log(sort_prime(19)); // Output: [2, 3, 5, 7, 11, 13, 17, 19]
Output:
[2,3,5] [2,3,5,7,11] [2,3,5,7,11,13,17,19]
Live Demo:
See the Pen javascript-basic-exercise-109 by w3resource (@w3resource) on CodePen.
Flowchart:
ES6 Version:
// Function to sort prime numbers up to a given number
const sort_prime = (num) => {
let prime_num1 = []; // Array to store prime numbers
let prime_num2 = []; // Array to hold flags indicating prime numbers
// Initializing flags in prime_num2 array
for (let i = 0; i <= num; i++) {
prime_num2.push(true);
}
// Finding prime numbers using the Sieve of Eratosthenes algorithm
for (let i = 2; i <= num; i++) {
if (prime_num2[i]) {
prime_num1.push(i); // Pushing the prime number to the prime_num1 array
// Marking multiples of the current prime number as non-prime in prime_num2
for (let j = 1; i * j <= num; j++) {
prime_num2[i * j] = false;
}
}
}
return prime_num1; // Returning the sorted array of prime numbers
};
// Example usage of the function
console.log(sort_prime(5)); // Output: [2, 3, 5] (prime numbers up to 5)
console.log(sort_prime(11)); // Output: [2, 3, 5, 7, 11] (prime numbers up to 11)
console.log(sort_prime(19)); // Output: [2, 3, 5, 7, 11, 13, 17, 19] (prime numbers up to 19)
Improve this sample solution and post your code through Disqus.
Previous: JavaScript program to create the dot products of two given 3D vectors.
Next: JavaScript program to find the number of even values in sequence before the first occurrence of a given number.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/javascript-exercises/javascript-basic-exercise-109.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics