Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
I need to generate prime numbers up to 150.
This is what I have so far. What should be changed from here?

What I have tried:

int main(void)
{
	int p=2, prime[150], i,n;

	for (p = 2; p <= 150; ++p){
		for (i = 2; i <= 150; ++i){
			n = p*i;
			prime[i] = i;
			prime[n] = 1;	
		}
	}
	printf("%i\n", prime[i]);

	return 0;
}
Posted
Updated 17-Sep-16 9:33am
v2
Comments
Richard MacCutchan 17-Sep-16 5:09am    
Search the articles section, there are samples to be found there.
Philippe Mori 17-Sep-16 8:36am    
Use a code block and format your code...

By the way, it would probably not be very hard to find that information by using Google or Bing...
Philippe Mori 17-Sep-16 8:38am    
Whenever possible, declare your variable at their first use. This is a bad habit from limited language to declare variable at the top of a function.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Advice: take a sheet of paper and try to do it by hand, your program should use the same procedure.
Ask your self a few questions:
- How do I know that actual number is a prime ?
- When I got a new prime, What is the value of the first multiple of that prime that is not already checked as non prime ?
 
Share this answer
 
v3
If you want to implement such algorithm then you have to first understand it. In order to understand it, a good reading is its very wikipedia page: Sieve of Eratosthenes - Wikipedia, the free encyclopedia[^].
 
Share this answer
 

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