GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Apr-2004, 18:53
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,543
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
Question

Help w/ prime # determination


What started out as a simple project is giving me headaches. The task is to write a C program the outputs x prime numbers, where x is a value input by the user. My problem is that rather than stopping after the program loops x times, it stops when the prime number = x. For example, if you want to see 3000 prime numbers, the program stops after 431 iterations when the prime number is 2999. I've been told that instead of incrementing my input variable w/ every loop cycle, it should only increment when a prime number is found, but I can't figure out how to do that.

Below is the main file and the function.
CPP / C++ / C Code:
#include "primes.h"

int main()
{
	int see, input, i=0;
	
	printf("%s\n%s",
		"PRIMES WILL BE PRINTED.",
		"How many do you want to see?  ");
	scanf("%d", &see);
	for (input = 0; input <= see; ++input)
		if (is_prime(input)==1){     /*if a prime is found, increment a counter then print the counter and the prime number*/
			++i;
			printf("%d:\t%d\n", i, input);
		}
	return 0;
}

CPP / C++ / C Code:
#include "primes.h"

int is_prime(int n) /*'input' is passed to function as 'n'*/
{
	int k, limit;
	
	if (n==2) /*if 'input' = 2, then it's prime and 'true' is returned*/
		return 1;
	if (n%2==0) /*if 'input' is evenly divisible by 2, then it's not prime and 'false' is returned*/
		return 0;
	limit = n/2;
	for (k=3; k <= limit; k += 2) /*checks to see if 'input' is evenly divided by an odd #*/
		if (n%k==0)
			return 0;
	return 1;
}

Thanks for any new ideas on this.
  #2  
Old 12-Apr-2004, 19:56
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
change this loop:

for (input = 0; i < see; ++input)

the condition i<see is what u should do because you want to loop until the number of see is reached that is the number of primes "i".
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #3  
Old 12-Apr-2004, 19:59
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
And here is a better way to determine wether a number is prime or not:

The number is not prime when:
(n-1)%6!=0 and (n+1)%6!=0

hope this helps. ;-)
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #4  
Old 13-Apr-2004, 01:43
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by Max Payne
change this loop:

for (input = 0; i < see; ++input)

the condition i<see is what u should do because you want to loop until the number of see is reached that is the number of primes "i".

Correct.

Another possibility is to simply output 2 as prime then start your loop and skip even numbers:

Code:
for (input=3, i=1; i < see; input+=2)
This will start input at 3, i at 1 (for the 2), and skip all even numbers.

Also, the max number you need to check for a prime is sqrt(value) instead of value/2. Play around with some tests and you'll see why.
__________________

Age is unimportant -- except in cheese
  #5  
Old 13-Apr-2004, 18:33
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,543
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
Talking

Thanks for the help!


I finally got it working. I knew it was a simple fix but just couldn't see it. I really appreciate the quick responses. I hope that I will soon be able to offer my own advice to others, as soon as I learn a little more.

Thanks again!
  #6  
Old 14-Apr-2004, 13:02
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
yes skipping evens would be much more efficient! good idea walt! i was wondering if one could use bitshifting in some way instead of modular division to make it even more efficient!
  #7  
Old 14-Apr-2004, 17:55
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by machinated
yes skipping evens would be much more efficient! good idea walt! i was wondering if one could use bitshifting in some way instead of modular division to make it even more efficient!
Personally, I would consider that level of "efficiency" a waste of time unless you are dealing with extremely huge numbers. But then you'd also be dealing with non-standard integers (16- or 32-byte -- yes byte -- integers.) Then these tricks might be useful, but not for standard types as defined in current systems.

It would also be hard to understand the code because of the obfuscation. Therefore, not recommended on that level either.
__________________

Age is unimportant -- except in cheese
  #8  
Old 14-Apr-2004, 19:34
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
Quote:
Originally Posted by machinated
yes skipping evens would be much more efficient! good idea walt! i was wondering if one could use bitshifting in some way instead of modular division to make it even more efficient!

Hi machinates,

Can you explain to me what is bitshifting is. I am constantly searching for a good algo to generate prime number. Maybe this could help me. Thanks.
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #9  
Old 14-Apr-2004, 20:01
sdeming sdeming is offline
VIP
 
Join Date: May 2003
Location: Michigan, US
Posts: 8
sdeming will become famous soon enough
Quote:
Originally Posted by Max Payne
Hi machinates,

Can you explain to me what is bitshifting is. I am constantly searching for a good algo to generate prime number. Maybe this could help me. Thanks.
If you are looking to generate a single prime number then a combination of efforts can be used. If you want to generate a range, starting from 1 you should look at the Sieve of Erastothenes. It's rather nifty.

Consider this. You have a huge bitset, all turned off. You initialize your bitset by turning on 1 and 2 (also store these or print these out as primes) and this is where the fun begins. Not iterate through the entire bitset in multiples of 2 (4, 6, 8, 10, ...) and turn the bits on. Now from this point forward, you increment by 1 (we're now at three) and if the bit is off then the number is prime, store it or print it. Now iterate through all multiples of this prime (6, 9, 12, 15 ...) and turn the bits off. Increment again and we're at 4, which was turned off when iterating through multiples of 2. Skip it and move on to 5 which is off. Remember, any time you encounter an off during the main loop, you are on a prime. Display or store 5 and iterate through all multiple of 5, turning the bits off. Increment to 6, it's a multiple of 2 so it's already off. Next is 7, it's off and prime, display or store and iterate through it's multiples and turn them all off. Continue until you fill your field.

This sieve is pretty nifty but it does require a good deal of memory to build a sizable list of primes. But you may notice that there are no divides or mods here at all. It's all increments and muls.
  #10  
Old 15-Apr-2004, 01:09
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
Quote:
Originally Posted by sdeming
If you are looking to generate a single prime number then a combination of efforts can be used. If you want to generate a range, starting from 1 you should look at the Sieve of Erastothenes. It's rather nifty.

Consider this. You have a huge bitset, all turned off. You initialize your bitset by turning on 1 and 2 (also store these or print these out as primes) and this is where the fun begins. Not iterate through the entire bitset in multiples of 2 (4, 6, 8, 10, ...) and turn the bits on. Now from this point forward, you increment by 1 (we're now at three) and if the bit is off then the number is prime, store it or print it. Now iterate through all multiples of this prime (6, 9, 12, 15 ...) and turn the bits off. Increment again and we're at 4, which was turned off when iterating through multiples of 2. Skip it and move on to 5 which is off. Remember, any time you encounter an off during the main loop, you are on a prime. Display or store 5 and iterate through all multiple of 5, turning the bits off. Increment to 6, it's a multiple of 2 so it's already off. Next is 7, it's off and prime, display or store and iterate through it's multiples and turn them all off. Continue until you fill your field.
It look like a good algo, but I it increases the number of loops and if the number gets bigger, the number of loops also increase dramatically..is it ok??
Quote:
Originally Posted by sdeming
This sieve is pretty nifty but it does require a good deal of memory to build a sizable list of primes. But you may notice that there are no divides or mods here at all. It's all increments and muls.
does division or mod consumes more calculation time than increments and muls??
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:36.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.