Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Bear Bibeault
• Paul Clapham
• Jeanne Boyarsky
• Knute Snortum
Sheriffs:
• Liutauras Vilda
• Tim Cooke
• Junilu Lacar
Saloon Keepers:
• Ron McLeod
• Stephan van Hulst
• Tim Moores
• Tim Holloway
• Carey Brown
Bartenders:
• Joe Ess
• salvin francis
• fred rosenberger

# find prime numbers from 1-100

Greenhorn
Posts: 10
Here is my code ,but don't print 2,3. how can I include them to my list without hard coding?

result:
5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49, 53, 55, 59, 61, 65, 67, 71, 73, 77, 79, 83, 85, 89, 91, 95, 97,

[edited to enable code tags]

author & internet detective
Posts: 39789
797
Before worrying about 2 and 3 being missing, you should work on the algorithm. 25 is not a prime number. Nor is 35, 49, etc. All you've done is omit the numbers divisible by 2 and 3.

Once you have a proper algorithm for finding prime numbers, I bet the "2 and 3 are special" problem goes away. Remember - the definition of a prime # is that it is only divisible by itself and 1. (and is not 1)

Niloo Rad
Greenhorn
Posts: 10
Thank you for your attention

Jeanne Boyarsky wrote:Before worrying about 2 and 3 being missing, you should work on the algorithm. 25 is not a prime number. Nor is 35, 49, etc. All you've done is omit the numbers divisible by 2 and 3.

Once you have a proper algorithm for finding prime numbers, I bet the "2 and 3 are special" problem goes away. Remember - the definition of a prime # is that it is only divisible by itself and 1. (and is not 1)

Niloo Rad
Greenhorn
Posts: 10
What do you think about this one?

<script>

var array = new Array(100);

for ( var index = 0 ; index < =array.length; index++){
array[index]=1;

for ( var i = 2 ; i < =array.length; i++){
if ( array[i])
for ( var j=i+i; j<array.length; j+=i){
array[j]=0;
}
}
for (var index=2; index >< array.length; index++){
if (array[index])

document.window(index);
}

}

</script>

Jeanne Boyarsky wrote:Before worrying about 2 and 3 being missing, you should work on the algorithm. 25 is not a prime number. Nor is 35, 49, etc. All you've done is omit the numbers divisible by 2 and 3.

Once you have a proper algorithm for finding prime numbers, I bet the "2 and 3 are special" problem goes away. Remember - the definition of a prime # is that it is only divisible by itself and 1. (and is not 1)

Jeanne Boyarsky
author & internet detective
Posts: 39789
797
I'm not sure what that does. What does it output?

Niloo Rad
Greenhorn
Posts: 10
Thank you my friend, I figure it out
My code is working now. Thank you

Jeanne Boyarsky wrote:I'm not sure what that does. What does it output?

Greenhorn
Posts: 1
hello:
If you are looking to get the prime numbers you will need to get the square Root of the number and then look at all the numbers LESS THAN the sqrt. e.g.

Say you want to test if 51 is prime then the SQRT(51) ~ 7 .. so all the numbers less than 7 should be tested if they divide 51; which is 2, 3, 4, 5, 6, and 7.. (3 divides this number) so we stop and 51 is NOT a prime ..

let us try 29, whose square root is approximately 5 (we only want the integral portion of the square root) .. then the numbers less than 5 are 2,3, 4 and 5 ..
As can be seen none of these numbers divide 29. SO 29 is PRIME.. ...

Hope that helps.

Marshal
Posts: 24950
61
Yes, that's another way of doing it. But I'm not convinced that it's better than Niloo's implementation of the Sieve of Eratosthenes, which doesn't require calculating any square roots or even doing any divisions.

Author
Posts: 23
6

var array = new Array(100);

This does not do what you think it does in JavaScript. It does not initialize an array with enough memory set aside for a length of 100. It creates an array with 100 "undefined" values. It creates this:

[undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined]

JavaScript has dynamically lengthed arrays, closer to a Java vector, just do this:

var array = [];

and if you want to loop 100 times, just say index < 100, if an array needs a new slot it makes one.

 Yeah, but is it art? What do you think tiny ad? Java file APIs (DOC, XLS, PDF, and many more) https://products.aspose.com/total/java
Similar Threads