Ask your own question, for FREE!
Computer Science 10 Online
OpenStudy (anonymous):

From another website: Printing all the prime numbers between 0 and 100 (this is a tricky one!) How can I do this?

OpenStudy (anonymous):

I'm assuming you want a specific example, although I can also do theory. First, you create an empty list of prime numbers. Then, you run through numbers 2 to 100, and each time you get to a number that doesn't have any factors in the list of primes, you add it to the list of primes. Then you print out the list. In python: primes = [] def isPrime(num): for primeNum in primes: if(num % primeNum == 0): return False return True for i in range(2, 100): if(isPrime(i)): primes.append(i) print(primes)

OpenStudy (anonymous):

It helps but, I need in javascript. My idea it's to check every number if is prime. Another solution?

OpenStudy (anonymous):

Here you go // Check if a number is prime. function isPrime(N) { for (var i = 2; i < N; i++) { if (N % i == 0 && i != N) // Checks if N is prime or not. return false; } return true; } // Gets all primes from inclusive 'startN' to inclusive 'endN'. function getAllPrimesBetween(startN, endN) { var primes = []; // Array holding primes. for (var N = startN; N <= endN; N++) // For loop. { if(isPrime(N)) // Checking if N is prime and eventually adding it to array. primes.push(N); // Adds N to array. } return primes; } // Usage sample. // Gets an array of primes from 2 to 100. getAllPrimesBetween(2,100); // [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!