Prime number program

Write a program that outputs the prime numbers between 1 and 100

Page 1 of 1

13 Replies - 4845 Views - Last Post: 29 October 2007 - 10:02 PM Rate Topic: -----

#1 Apollos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-October 07

Prime number program

Post icon  Posted 26 October 2007 - 10:19 AM

Okay I have followed the code that Martyr2 has posted and I understand the basic ideas in it. However when I compile and run the code it still will not run and comes up with a laundry list of errors.


 #include <iostream>
using namespace std;

int main () 

{
	int prime_number , number;
		
		int[] numbers = new int[99 + 1];
		 
		 int count = 2;
		 for ( int i = 2; i <= 100; i++)
		 {
			 number[i] = count;
			 count++;
			 }
			  for (int i = 2; (i * i); <= 100; i++) {
				
				for (int j = (i * i); j <= 100; j = j + i) {
					numbers[j] = 0;
				   
					for (int i = 2; i <= 100; i++) {
						if (numbers[i] != 0)
						 { lstPrimes.Items.Add(numbers[i]); }
			
								   
			}
		}
	} 
	  
	  system("pause");

	return 0;




I am sooooooo lost

Apollos

Is This A Good Question/Topic? 0
  • +

Replies To: Prime number program

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 247
  • View blog
  • Posts: 13,505
  • Joined: 12-July 02

Re: Prime number program

Posted 26 October 2007 - 10:20 AM

Can you please post the errors? At the very least, it should be noted that you have not closed your main function. you are also referencing a class object
lstPrimes.Items.Add(numbers[i]);


that has never been declared or instantiated.

Can you post where you got the code from? Martyr2 is an accomplished programmer, so I must assume this is code out of context.
Was This Post Helpful? 0
  • +
  • -

#3 Apollos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-October 07

Re: Prime number program

Posted 26 October 2007 - 11:42 AM

View PostAmadeus, on 26 Oct, 2007 - 10:20 AM, said:

Can you please post the errors? At the very least, it should be noted that you have not closed your main function. you are also referencing a class object
lstPrimes.Items.Add(numbers[i]);


that has never been declared or instantiated.

Can you post where you got the code from? Martyr2 is an accomplished programmer, so I must assume this is code out of context.



[quote name='Apollos' date='26 Oct, 2007 - 11:34
AM']

Okay here is the error list


 C:\Documents and Settings\Owner\My Documents\Chapter 3_14.cpp In function `int main()': 

9 C:\Documents and Settings\Owner\My Documents\Chapter 3_14.cpp expected primary-expression before "int" 

9 C:\Documents and Settings\Owner\My Documents\Chapter 3_14.cpp expected `;' before "int" 

14 C:\Documents and Settings\Owner\My Documents\Chapter 3_14.cpp invalid types `int[int]' for array subscript 

17 C:\Documents and Settings\Owner\My Documents\Chapter 3_14.cpp expected primary-expression before '<=' token 

17 C:\Documents and Settings\Owner\My Documents\Chapter 3_14.cpp expected `)' before ';' token 

17 C:\Documents and Settings\Owner\My Documents\Chapter 3_14.cpp name lookup of `i' changed for new ISO `for' scoping 

17 C:\Documents and Settings\Owner\My Documents\Chapter 3_14.cpp   using obsolete binding at `i' 

17 C:\Documents and Settings\Owner\My Documents\Chapter 3_14.cpp expected `;' before ')' token 




I got this from a link that Martry2 had posted on one of the snippits (I think) Here is the original code that he posted:


namespace sieve 
{
   public partial class Form1 : Form {
		public Form1()
		{
			InitializeComponent();
		}

		private void btnPrimes_Click(object sender, EventArgs e)
		{
			int result;
			Int32.TryParse(txtMaxNumber.Text, out result);

			if (result > 0) {
				findPrimes(result);
			}
		}

		private void findPrimes(int size)
		{
			// Setup an array of the size specified
			int[] numbers = new int[size + 1];

			// Starting at 2, initialize the array up to the number specified.
			int count = 2;
			for (int i = 2; i <= size; i++) {
				numbers[i] = count;
				count++;
			}

			// Now starting at two and going until the square of the counter is less 
			// than the number specified, start crossing out multiples with a zero.

			for (int i = 2; (i * i) <= size; i++) {
				for (int j = (i * i); j <= size; j = j + i) {
					numbers[j] = 0;
				}
			}

			// Now go back through the array and look for the values not crossed out.
			// Those will be your prime numbers.
			lstPrimes.Items.Clear();

			for (int i = 2; i <= size; i++) {
						if (numbers[i] != 0) { lstPrimes.Items.Add(numbers[i]); }
			}
		}
	}
}




I hope that this helps you answer my Q!!!! :-)

Apollos
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 415
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Prime number program

Posted 26 October 2007 - 12:57 PM

That snippet of code that you are trying to use is written in C#. However, you are trying create a program in C++.

So that snippet of code is not going to work for you, as it was written. You will need to use the C++ equivalent of the logic that is found in the C# snippet. In other words you are going to need to translate it over to C++.
Was This Post Helpful? 0
  • +
  • -

#5 manzoor  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 468
  • Joined: 07-August 07

Re: Prime number program

Posted 27 October 2007 - 10:58 AM

Here's the code. I didn't write this.

Hope you find it helpful


#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
	int i, j;
	for(i=2; i<=100; i++) {
	for(j=2; j <= (i/j); j++)
	if(!(i%j)) break; // if factor found, not prime
	if(j > (i/j)) cout << i << " is prime number\n";
	}
	system ("pause");
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#6 brawnyman713  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 139
  • Joined: 21-October 07

Re: Prime number program

Posted 27 October 2007 - 11:13 AM

View Postk00ldude47, on 27 Oct, 2007 - 10:58 AM, said:

Here's the code. I didn't write this.

Hope you find it helpful


#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
	int i, j;
	for(i=2; i<=100; i++) {
	for(j=2; j <= (i/j); j++)
	if(!(i%j)) break; // if factor found, not prime
	if(j > (i/j)) cout << i << " is prime number\n";
	}
	system ("pause");
	return 0;
}


All of your programming is very inefficient, because as the numbers get larger, it has to go through more numbers to determine a factor. I only do ruby and java, but I will write for you some simple code that never slows down, and runs so fast that even in ruby it can find all of the primes up to 20000 in a fraction of a second, and it will maintain that rate of speed indefinitely:

max_number = whatever // Number you want to find all of the prime numbers to
current_number = 7
if number >= 1 // You will see why this is necessary later
print "1\n";
end // if
if number >= 2
print "2\n";
end // if
if number >= 3
print "3\n";
end // if
if number >= 5
print "5\n";
end // if

while current_number <= max_number
if current_number % 2 != 0 and current_number % 3 != 0 and current_number % 5 != 0
print current_number + "\n";
end // if
current_number++;
end // while

The reason this program works is because all prime numbers starting at the number 7 cannot divide evenly into 2, 3, or 5. Any number that is evenly divisible by any of these numbers is not a prime. 1, 2, 3, and 5 are exceptions to this rule and so are handled by the if statements. Hope this programs helps.
Was This Post Helpful? 0
  • +
  • -

#7 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 41
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Prime number program

Posted 27 October 2007 - 01:10 PM

Inefficient, perhaps, but at least it should work. Your code does not (and yes, I've translated it into the proper language for this forum).

For instance, it outputs the following numbers:
7
49
77
13
91
143
The first three are evenly divisible by seven, the second three are evenly divisible by 13. None of them are prime.

Indivisibility by 2,3, or 5 is not sufficient for proving a prime. Required, yes, but not sufficient.

And please post code using code tags like this :code:.

-jjh

This post has been edited by jjhaag: 27 October 2007 - 01:10 PM

Was This Post Helpful? 0
  • +
  • -

#8 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: Prime number program

Posted 27 October 2007 - 04:23 PM

so how do you find prime numbers?

The basic method is known as the Sieve of Eratosthenes. The basic idea here is to fill an array with numbers form 2-whatever (some people actually start at 0 so that the index==number it holds). Then grab the first number (2) and put it into the list of primes, then set all multiples of 2 to zero. Next find the next non-zero number (3) and put it in the list of primes, then zero all multiples of 3, grab the next non-zero number (5) and then zero all multiples of that number, grab the next non-zero number (7) and then zero all multiples of that number... how do you know when to stop... when the square of number is larger than the size of your sieve array then you just need to scroll though the sieve array and pick out the non-zero numbers. In the case of primes in 1-100 then once you have done 11 then you can just read off the primes.

This method is very fast. But, it takes a large amount of memory to calculate many primes. (There are lots of ways to limit the length of the array without effecting performance too much).

Another method is to initialize your prime list with 2, next find the first number that is not divisible by 2 (i.e. num % 2 =! 0) (should be 3), add that number to your list of primes. Next starting with the next number (4) count up until you find a number that is not divisible by 2 or 3, (5), then add it to the list of primes...

This method is a little less officiant than the sieve, but you can add in logic that will allow your to add in blocks of primes to your list very fast. Then also allows you to make functions like isPrime(int) and nextPrime(int).

There are many other methods (often combinations of the above).
Was This Post Helpful? 0
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: Prime number program

Posted 27 October 2007 - 05:13 PM

PS... 7 IS prime... just thought I would add that... so is 13... technically. :) But that program is NOT correct. If only the definition of prime was "{2, 3, 5} + 'number not divisible by {2, 3, 5}'".

The program was probably the case II I stated above, but lost something some logic along the way.
Was This Post Helpful? 0
  • +
  • -

#10 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 41
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Prime number program

Posted 27 October 2007 - 05:48 PM

hah...oops :rolleyes: . That was stupid of me. Just a downright terrible day for checking for logical errors and typos - more edits than you could shake a stick at. Well, just add the next non-prime multiples of 7 and 13 to that list instead of 7 and 13 themselves...91 and 169, I believe.
Was This Post Helpful? 0
  • +
  • -

#11 manzoor  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 468
  • Joined: 07-August 07

Re: Prime number program

Posted 29 October 2007 - 07:56 AM

The code I have provided is it working fine ??

As i will also be using a prime number prog in my project (just for practice) so i want to know the best and fastest one

Thanks
Was This Post Helpful? 0
  • +
  • -

#12 makaveli93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-May 07

Re: Prime number program

Posted 29 October 2007 - 11:37 AM

#include <iostream>
using namespace std;
bool prime(int num);

int main()
{
 
 for (int i=2; i<=100; i++)
 {
 if (prime(i) ) 
  cout<<i <<endl;
 }
system ("PAUSE");
}

bool prime(int num)
{
int flag=1;
for(int i=2; i<= num/2; i++)
{
if (num%i==0)
flag=0;
}
if (flag==0)
return false;
else 
return true;


}




This should work.
Was This Post Helpful? 0
  • +
  • -

#13 tikotiko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 14-October 07

Re: Prime number program

Posted 29 October 2007 - 09:06 PM

#include<iostream>
using namespace std;

int main()
{
int N,i,nb=0,d;
bool is_prime;

cout<<"Typer the value of N : ";cin>>N;

for(i=2;i<=N;i++)
	{
	/* test if i is prime*/
	is_prime=true;
	d=2;
	while(is_prime && d*d<=i)
		if(i%d==0)is_prime=false; else d++;

	if(is_prime==true)nb++;
	}

cout<<"The number of prime number lesser or equal to "
	<<N<<" is "<<nb<<endl;

return 0;
}


N in your question is 100

#include<iostream>
using namespace std;

int main()
{
int N,i=1,nb=0,d;
bool is_prime;

cout<<"Type the value of N : ";cin>>N;

while(nb<N)
	{
	i++;
	is_prime=true;
	d=2;
	while(is_prime && d*d<=i)
		if(i%d==0)is_prime=false; else d++;

	if(is_prime==true)nb++;
	}

cout<<"Le N-st prime number is "<<i<<endl;

return 0;
} 

This is compute the N-st prime number
Was This Post Helpful? 0
  • +
  • -

#14 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 41
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Prime number program

Posted 29 October 2007 - 10:02 PM

tikotiko, please post source code using the code tags like this - :code:

If you do, you'll find that you don't need to use such an irritatingly large font to differentiate the rest of your post from the code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1