Prime Number Calculation

Page 1 of 1

12 Replies - 8236 Views - Last Post: 26 March 2007 - 11:51 AM Rate Topic: -----

#1 Phantom_of_the_opera   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 13-November 06

Prime Number Calculation

Posted 22 March 2007 - 12:41 PM

1

This post has been edited by Phantom_of_the_opera: 03 May 2007 - 11:38 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Prime Number Calculation

#2 skyhawk133   User is offline

  • Head DIC Head
  • member icon

Reputation: 1981
  • View blog
  • Posts: 20,434
  • Joined: 17-March 01

Re: Prime Number Calculation

Posted 22 March 2007 - 12:50 PM

Someone didn't read the instructions:

Quote

I have done the following before posting a new topic:
- Searched for my problem using the search box on the right
- Given my thread a descriptive title (Not "Help", "Homework Problem", etc.)
- Put [code]code here[ /code] tags around my code
- Included any errors and a description of my problem

Was This Post Helpful? 0
  • +
  • -

#3 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Prime Number Calculation

Posted 22 March 2007 - 01:27 PM

Well there are a few ways to calculate prime numbers. Most of them a sieve method, meaning that we take out the numbers which are NOT prime. For example: Take an array of numbers from 2 to 100, starting with 2, remove all other multiples of 2 (setting the array elements to zero). then look for the next non-zero number (in this case 3) and take out all multiples of 3.

Now our array looks like {2, 3, 0, 5, 0, 7, 0, 0, 0, 11, 0, 13...} now we find the next nonzero member {5} and take out all multiples of 5.

This method stated as such would not be much help calculating the first 100000 prime numbers as you wuold need an array capable of holding all of the numbers (about 1318699 elements) but it is the idea. This is called the sieve or Eratosthenes and the link there provides some pseudocode examples.

Another popular begining method is to make an "isPrime(int n)" which checks all numbers from n to sqrt n to see if any of those divide the numbers.... a note though, if you are using a table to store calculated prime numbers then you can use this table to vastly reduce the number of checks by using the primes already calculated.

I have seen about 100 different methods think about prime numbers and thier properties and work out your own. That way you understand exatly how it works.

This post has been edited by NickDMax: 22 March 2007 - 01:28 PM

Was This Post Helpful? 0
  • +
  • -

#4 Phantom_of_the_opera   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 13-November 06

Re: Prime Number Calculation

Posted 23 March 2007 - 10:11 PM

.

This post has been edited by Phantom_of_the_opera: 03 May 2007 - 11:38 AM

Was This Post Helpful? 0
  • +
  • -

#5 born2c0de   User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 187
  • View blog
  • Posts: 4,673
  • Joined: 26-November 04

Re: Prime Number Calculation

Posted 23 March 2007 - 10:48 PM

Quote

Well I now realize that you cannot pass an Array as a reference, my mistake

You made no mistake.
Arrays are passed by reference.
The address of the first element is always passed.
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Prime Number Calculation

Posted 24 March 2007 - 12:26 AM

well you can pass arrays to functions

void foo(int *a);
...
int array[10];
foo(array);
cout << a[0] << a[1];

void foo(int *a)
{
	a[0]=1;
	a[1]=2;
	return;
}


so you can pass the same array to Calculate and to Print.
Was This Post Helpful? 0
  • +
  • -

#7 Phantom_of_the_opera   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 13-November 06

Re: Prime Number Calculation

Posted 24 March 2007 - 08:05 PM

.

This post has been edited by Phantom_of_the_opera: 03 May 2007 - 11:39 AM

Was This Post Helpful? 0
  • +
  • -

#8 Phantom_of_the_opera   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 13-November 06

Re: Prime Number Calculation

Posted 24 March 2007 - 08:18 PM

.

This post has been edited by Phantom_of_the_opera: 03 May 2007 - 11:39 AM

Was This Post Helpful? 0
  • +
  • -

#9 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Prime Number Calculation

Posted 24 March 2007 - 10:07 PM

well first off as I read it, the assignment said to calculate the "first n prime numbers" and you program would calculate all prime numbers up to n. There is a big differance. The 1000'th prime is 7919, where as the largest prime less than 1000 is 997 which is the 168'th prime... so your program would calculate far less then n primes.

so how might this work. Well suppose we take a differnent aproch. We make an array of known primes (int arePrime[100000]) and we poplulate it with primes as we find them. In our loop to prove a number prime we only need to check to see is (num % divisor) == 0 as long as divisor is a prime number (and what do you know we are making an array of them, maybe we can use the ones we already know to find the next one...)

So we have an array (arePrime[]) containing all the prime numbers we know... well we will need a variable (numPrimes) to tell us how many we have found so far.

Now we can define a function to find the next prime number, passing it the array of known numbers, and a pointer to the number of known primes in that array.

inside the function we initialize the number we want to check to the last known prime number +1, then we loop though all known primes and see if (number % arePrime[i] == 0) if so, this is not a prime, if we get though all known primes it IS a prime and can be added to the array.

pseudocode:
int findNextPrime(int[] arePrime, int numPrimes)
{
	int curNumber <-- arePrime[numPrimes - 1]
	while (no prime found && curNumber < Maximum int value)
	{
		curNumber++ 
		for(int i=0; i<numPrimes; i++)
		{
			if (curNumber % arePrime[i] == 0) 
			{
				not a prime
				break
			 }
		 }
	}
	if (found prime) return curNumber 
	else return 0;
}


With this algorithm you could calculate as many primes as your array could hold (you would have to initalize the array with 2) as long as the n'th prime is not greater than the int size. There are a few improvements that I can think of to make it run faster for larger values of n, but it is better than checking all odds up to sqrt(curNumber) by far as is.
Was This Post Helpful? 0
  • +
  • -

#10 Phantom_of_the_opera   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 13-November 06

Re: Prime Number Calculation

Posted 26 March 2007 - 10:31 AM

.

This post has been edited by Phantom_of_the_opera: 03 May 2007 - 11:39 AM

Was This Post Helpful? 0
  • +
  • -

#11 Phantom_of_the_opera   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 13-November 06

Re: Prime Number Calculation

Posted 26 March 2007 - 11:28 AM

.

This post has been edited by Phantom_of_the_opera: 03 May 2007 - 11:39 AM

Was This Post Helpful? 0
  • +
  • -

#12 Amadeus   User is offline

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

Reputation: 253
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Prime Number Calculation

Posted 26 March 2007 - 11:46 AM

You do not appear to have a function named print? Is the post above missing some code?
Was This Post Helpful? 0
  • +
  • -

#13 Phantom_of_the_opera   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 13-November 06

Re: Prime Number Calculation

Posted 26 March 2007 - 11:51 AM

the post above that has it in there, that's what I have for it anyways... I haven't made any improvements on it yet, still trying to get it to work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1