Click here to Skip to main content
15,891,938 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I am trying to generate a list of prime numbers within a upper limit , eg. 100: so it should generate all the prime numbers below 100 from 0.
I was following Sieve of Eratosthenes algorithm , but cant implement it in code. Tried many times.
What i am trying to do is:

Quote:

Assume list of all numbers from 2 to N.
Let p initially equal to 2. The first prime.
Strike all multiples of p from the list less than or equal to N.
Find the next number after p, that’s not yet crossed out on the list. This is the next prime.
Repeat step 3 and 4 for the new p, unless p2 is greater than N.
Numbers not crossed out on the list are primes below the limit N.


Can i do this without using an array, or do i have to use an array for sure.
Also is there any other method to generate Prime numbers( even to detect them , i would work on later myself). Thanks
Posted
Updated 13-Nov-13 6:49am
v2

Algorithm:
Quote:
Create list of all numbers from 2 to N.
[...]

Question:
Quote:
Can i do this without using an array, or do i have to use an array for sure.

Answer: "No, you cannot".

[update]
Finding the prime numbers between 0 and 100 wihtout using an array is possible, of course. For each candidate n just test if there is a number between 2 and n (integer) square root, dividing it
[/update]
 
Share this answer
 
v2
Comments
Abhinav Gauniyal 13-Nov-13 12:48pm    
Sorry for being unclear , but what i meant was like if we want to generate prime number b/w 2 to 100 , 1 and 0 integers are already excluded, so if i want a loop , so i would be doing i=2 , i<=100 , and by using term list , i was not trying really to mean an array.
Thanks :)
We don't do homework here. You say you have tried many times but have not posted your code here for us to help with a specific problem.

Having said that look at this link for some suggestions

http://stackoverflow.com/questions/14211628/listing-prime-numbers-up-to-2-billion-using-sieves-method-in-c[^]

And there are many many more suggestions with these results from google. I've left the full link in view so you can see what the search criteria were

https://www.google.co.uk/#q=sieve+of+eratosthenes+prime+numbers+up+to+100+C&safe=active[^]
 
Share this answer
 
Comments
Abhinav Gauniyal 13-Nov-13 12:44pm    
I have achieved it using an array , but i want to detect a number whether it is prime or not , and that too without using an array.
Theres one method to check that number till its half value ( modulus==0 or not), but it wouldn't be optimized in terms of repetition. I am not too sure about it.
The fact that i am not preferring an array is that it will be needing a huge array to store all values to get a small prime number like 200th.
And the fact that i havent posted a code here is because i have already done that sieve of erasth.. part with an array , but i want to implement this without using an array.
I am sorry if i wasnt clear with my question :(
Thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900