473,480 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C++ Understanding the find_prime(bool prime[ ]) function below

542 Contributor
The code below is part of the title fuction. What has been omitted is setting all elements from 2 to a user entered bound in prime[ ] to true and then setting all elements from j=2 to 2*j< bound to false thus ruling out all even numbers as prime. This last code finds all primes after 2 and before bound and works properly.
Expand|Select|Wrap|Line Numbers
  1. int p=3
  2. int p = 3;
  3.   while (p<= bound/2)
  4.    {
  5.       for(int j=2;p*j<bound;j++)
  6.       prime [p*j] = false;//multiples of p are not prime
  7.       do ++p;
  8.       while(!prime[p]);
  9.    };
  10.  
What i cannot understand is how the last line determines when p is again prime
Mar 30 '08 #1
2 2096
Laharl
849 Recognized Expert Contributor
Prime is an array of bools, yes? Thus, while(!prime[p]) is going to iterate until it reaches one that's false initially, so the ! makes it true.
Mar 30 '08 #2
whodgson
542 Contributor
Prime is an array of bools, yes? Thus, while(!prime[p]) is going to iterate until it reaches one that's false initially, so the ! makes it true.
sorry i`m being so dumb but..............
Yes its an array of bools all of which have been previously set to TRUE after which (and previously) all the even ones have been set to FALSE. So now all the odd ones (which are the only ones we are interested in) are set to TRUE.
But i can`t see how it can evaluate a TRUE one and establish that it is FALSE and is therefore a prime without calling some function.
I really don`t want to waste any more of your time so i`ll keep nagging away at it until some light appears. Its a matter of understanding what you have said above.
thanks
Mar 31 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1568
by: Jason | last post by:
Hello everyone. Back with a pretty simple question regarding functions in PHP. Granted, still learning here and just trying to 'understand' what goes on and how it works. Here is a little...
9
2681
by: Greg Brunet | last post by:
In doing some testing of different but simple algorithms for getting a list of prime numbers, I ended up getting some results that seem a bit contradictory. Given the following test program...
11
5919
by: lostinpython | last post by:
I'm having trouble writing a program that figures out a prime number. Does anyone have an idea on how to write it? All I know is that n > 2 is prim if no number between 2 and sqrt of n...
9
1993
by: Joe Hesse | last post by:
/***** When I run the following program (gnu C++) the address of the automatic object 'a' in f() is the same as the address of the automatic object 'b' in main(). I conclude that 'a' in f() is...
32
76368
by: Cmorriskuerten | last post by:
HI, is this is this solution to test if a number is a prime number or not: /* * Is n a prime number? * Return TRUE (1): n is a prime number * Return FALSE (0): n is a *not* a prime number...
5
1212
by: Claire | last post by:
I've explained the problem in the following code snippet. I want to share single functions for use by several data types. In this function I call different functions to fill out the original object...
3
1347
by: Tom Plunket | last post by:
....at least, I think that I'm having a problem understanding the way closures work. I'm trying to define a function for an object which will take certain objects from the parent scope at the...
2
3027
by: apprentice01 | last post by:
Hello everybody, I have just recently started learning C++ and I have come across my first hurdle. I have read, and re-read, researched online, but to no avail: I'm unable to comprehend how the...
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7059
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6758
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7010
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5362
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3011
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3003
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.