![]() |
|
#1
|
||||
|
||||
Help w/ prime # determinationWhat 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:
CPP / C++ / C Code:
Thanks for any new ideas on this. |
|
#2
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
Quote:
Correct. Another possibility is to simply output 2 as prime then start your loop and skip even numbers: Code:
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
|
||||
|
||||
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
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
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
|
||||
|
||||
|
Quote:
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
|
|||
|
|||
|
Quote:
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
|
||||
|
||||
|
Quote:
Quote:
__________________
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 GIDBlog
Observations of Iraq by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The