26 Replies  1524 Views  Last Post: 13 April 2011  09:39 AM
#16
Re: Anyone can explain how this program works?
Posted 13 April 2011  05:22 AM
Well I already understood how the FOR works pretty much on my second post. I did go through it the same way i did a few posts ago today.
#17
Re: Anyone can explain how this program works?
Posted 13 April 2011  05:27 AM
Reread how the for() loop conditional works.
#18
Re: Anyone can explain how this program works?
Posted 13 April 2011  05:35 AM
#19
Re: Anyone can explain how this program works?
Posted 13 April 2011  05:38 AM
I am not sure what you are saying.
What, exactly, is your question?
#20
Re: Anyone can explain how this program works?
Posted 13 April 2011  05:42 AM
If (3%2==0)
prime=false
break;
Since 3%2 is 1 instead of 0 then prime=false doesnt apply. The break; in the next line will cause a jump to the
if(prime)
cout<<"Prime: "<<a<<endl;
the prime in that brackets means prime=true?
#21
Re: Anyone can explain how this program works?
Posted 13 April 2011  05:46 AM
If the if() test resolves to false then everything inside the braces "{" and "}" defining the scope of the if() test is skipped.
So the break will never be actioned in the example you give.
#22
Re: Anyone can explain how this program works?
Posted 13 April 2011  05:49 AM
I think he was wandering if
if (prime) { //... }
is the same as
if (prime == true) { //... }
which is true. And since he initialises prime before the "for" to true, for his test case "prime" will be printed since the break statement never gets executed.
This post has been edited by muballitmitte: 13 April 2011  05:49 AM
#23
Re: Anyone can explain how this program works?
Posted 13 April 2011  05:53 AM
If (3%2==0)resolves to false, it will go to
if(prime) next right?
Actually never got to saying thank you for your help so far. Thanks
muballitmitte, yes I was trying to know if if(prime) is the same as if(prime==true)!
#24
Re: Anyone can explain how this program works?
Posted 13 April 2011  06:01 AM
#25
Re: Anyone can explain how this program works?
Posted 13 April 2011  07:09 AM
And if you can't do that, then as janotte suggested earlier, you should be going back to the fundamentals of how to write a simple program  for example, how to write a program loop that sums the numbers from 1 to 5 and prints the result.
#26
Re: Anyone can explain how this program works?
Posted 13 April 2011  07:12 AM
#27
Re: Anyone can explain how this program works?
Posted 13 April 2011  09:39 AM
You have chosen to work from what I like to call the "naive algorithm" for finding primes. This is a very inefficient bruit force method based upon the direct definition of a prime number "A prime number's only divisors are 1 and itself" so we test each number between 2 and N1 to see if it is a divisor.
To do this we need to loop between 2 and our test number  1:
for(int n = 2; n < testNumber; n++) { if (testNumber % n == 0) { // If we get here then the number IS NOT prime break; // no need to check any more because we know the number is not prime! } }
Since we will want to use the knowledge outside of the loop we can fall back on the old standby of using a flag variable:
bool isPrime = true; //assume it is true since we are testing for false hood for(int n = 2; n < testNumber; n++) { if (testNumber % n == 0) { // If we get here then the number IS NOT prime break; // no need to check any more because we know the number is not prime! } } //now we can use our flag to do something with the knowledge if (isPrime) { cout << testNumber << " is a prime number" << endl; } else { cout << testNumber << " is NOT prime" << endl; }
So now we can test all numbers up to the test number by adding an outer loop and you get your original function.
to add up all the primes all you need is an accumulator variable (some integer initialized to zero) and accumulator += testNumber when it is shown to be a prime.
So there are some many improvements that can be made here. for example we know that with the exception of 2 only odd number are prime so there is no need to check all of the even numbers past 2. Next we know that divisors come it pairs: given the number N if it is divisible by x then there exits a number N/x such that x * N/x == N
So all divisors are broken up into two sets, those above sqrt(N) and those below sqrt(N) and for every divisor below sqrt(N) there exists one above, and visa versa. SO! there is no need to check all numbers less than our testNumber, we only need to check up to the square root of our test number.
Finally if a number if going to be divisible by a number that number is either prime or composite, if it is composite then it is divisible by some prime number  SO, we really only need to check divisibility by other prime numbers! So if we check for divisors that are prime numbers below sqrt(testNumber) then we can REALLY cut the amount of time needed to test a number.
These idea end up culminating in the classic "Sieve of Eratosthenes".
Take things slow and make sure you understand the intent as well as the stepbystep logic. Using the naive is fine so ensure you think about what it is trying to accomplish.
