CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Threaded View

  1. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: I dont get this specific FOR loop.

    Quote Originally Posted by fsdama View Post
    Why is it that J is initialised to 2?
    If it were initialized to 1, every number would get sorted out as non-prime because every integral number is divisible by 1, i.e. i % 1 == 0 is always true.

    And what is the condition j<=i/2 for?
    As you seem to understand for loops as such, you certainly know that this is the loop's terminal condition. There would be more efficient ways to phrase the terminal condition of this loop, but this is one of the more obvious ones.

    Imagine a non-prime number j that has the only prime factors 2 and some_large_prime, i.e. is defined as 2 * some_large_prime. Imagine the number would not get sorted out because it is divisible by 2 (which is purely imaginary because it always would, as this is tested first), then the loop would need to run up to j / 2 to find the other prime factor. You get the gist?

    The other extreme is a number j that is a square of a prime. In this case the loop would need to run up to at least exactly that prime to find out that j is non-prime. The lowest possible number to which this applies is 4 (== 2 * 2). In this case it is obvious that the loop needs to run up to 2 until it finds out that j is non-prime. In any other case (e.g. 9 == 3 * 3) the loop would need to run up to at least sqrt(j) which is always <= j / 2 for any j >= 4. (If you would look at it the nit-picking way this would mean the code is cheating for any j < 4, but it gets through with that because there is no prime between j / 2 and sqrt(j) in these cases.)

    The other thing I noticed with this statement is that there are no curly braces '{ }' following it.
    The for loop always controls the next statement or block (enclosed in curly braces) that follows the for statement. If there is only a single statement to be controlled, the braces are redundant (IOW optional). This is a bit less obvious here because the statement to be controlled is an if which in turn controls the statement or block that follows it.

    The fact that you ask this question is one of the cases that shows that it's not always recommended to leave out redundant braces, especially if the code is to be understood by beginners.

    As for the rest of the program: It looks like you already have perfectly understood what's going on there.
    Last edited by Eri523; December 29th, 2010 at 09:04 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured