Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Experts

I had seen for loop in the below manner so many time but I am not understand the logic behind this to use in this way.

C#
for(int i=2;i<=num/2;i++)


Why we divide num/2 as loop break?

In below program both giving same output but why in first approach we will use num/2?

C#
//First Approach 
public static void IsPrimeNumber1(int num)
        {
            int Count=0;
            for(int i=2;i<=num/2;i++)
            {
                if(num%2==0)
                {
                   Count++;
                    break;
                }
            }
            if(Count==0 && num!=1)
            {
                Console.WriteLine("Prime Number");
            }
            else
            {
                 Console.WriteLine("Not Prime Number");
            }
        }
//Second Approach
 public static  void IsPrimeNumber2(int num)
        {
            int i=2;
            for( i=2;i<=num-1;i++)
            {
                if(i%num==0)
                {
                    Console.WriteLine("Not Prime Number");
                    break;
                }
            }
            if (num == i)
            {
                Console.WriteLine("Prime Number");
            }
            else
            {
                Console.WriteLine("Not Prime Number");
            }
        }




Thanks
Dinesh Sharma

What I have tried:

I tried to find the logic behind divide by 2 as loop for break.
Posted
Updated 18-Jul-16 11:09am
Comments
PIEBALDconsult 12-Jul-16 1:46am    
Those two functions do very different things.
The first does not do what its name suggests it does and the for loop does nothing but waste time.
The second one does, but in one of the least efficient ways.
Patrice T 12-Jul-16 6:29am    
No, the second also waste time because the test is done backward
the test must be done with num%i instead of i%num
BillWoodruff 12-Jul-16 6:06am    
Your code is confused, buggy; it suggests to me you are not learning the fundamentals of C# programming properly. Don't feel "bad" about that ... everyone here was once a beginner, and we learn from our mistakes !

I strongly suggest you get a good .NET book, like the (free, on-line pdf file you can download) book '.NET Book Zero' by Charles Petzold:

http://www.charlespetzold.com/dotnet/

And, do a thorough study of the chapters.
Patrice T 12-Jul-16 6:32am    
The question suggest that the OP is not the author of the code.
BillWoodruff 12-Jul-16 7:55am    
All the more reason for the OP to get a good book and get to work ! :)

Because the developer doesn't know that he should start at 3, step by 2, and stop at sqrt(num) ; typical rookie mistake.
 
Share this answer
 
v2
Comments
BillWoodruff 12-Jul-16 7:56am    
+5
Quote:

C++
for(int i=2;i<=num/2;i++)

Why we divide num/2 as loop break?
We don't.
This code is a loop that breaks when i get over num/2.

Think about how you check if a number is prime, what do you do, when do you stop ?
Take a sheet of paper, a pencil and write down what you do to check 31.

Your program is very buggy, use the debugger to see what the code is doing.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

[Update]
By the way, rather than trying a code that you have been given and don't work,
try to write your own, it can't be worse.
A great lecture to learn C
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]
 
Share this answer
 
v2
Comments
BillWoodruff 12-Jul-16 6:03am    
My vote of #2: if you look closely at the OP's code (very confused, buggy), you will see they use 'num%2 == 0 as a break condition.
Patrice T 12-Jul-16 6:17am    
:(The OP is trying to understand a buggy code he found somewhere, it is a waste of time.
I know, but continuously testing if num is even, have never been enough to teel if a number is prime or not.
Did you see the other break condition i%num==0 ?
it is even better.
BillWoodruff 12-Jul-16 7:56am    
Adding confused advice to a buggy code does not improve the OP, or their question.
I believe you meant to write:
C++
if(num%i==0)

Instead of
C++
if(num%2==0)

for the first approach.
The for loop does break at (num/2) because, there is no prime number that can be divided by any number that is greater it's half.
That is surely great for the complexity as Solution #3 stated. the more the loop ends early with out extra checking the less complex this piece of code is and the more efficient it becomes when dealing with large numbers.
 
Share this answer
 
Because num/2 will take half numbers. Ex. we have 10 numbers we execute that loop till 5 for checking number is prime or not there is no need to check further for 6,7..9.
When we took num - 1 for loop will execute more than (num/2) so for complexity terms num /2 is good.
 
Share this answer
 
Comments
PIEBALDconsult 12-Jul-16 1:39am    
7 isn't prime anymore?
Rais Shaikh 12-Jul-16 1:48am    
For 7 it is showing correct result. For any prime number we need to check num /2 further we don't need to check. We get the result in (num/2).
Philippe Mori 12-Jul-16 9:23am    
Stopping at num / 2 is not good. Maybe you were sleeping in your computer course! As indicate in solution 2, there is no point in test numbers above square root of num.

For ex. with 10, you only need to test if the number is divisible by 2 or 3. 10 is not a prime number since 10 / 2 is equal to 5. On the other hand 11 would be a prime number as 11 % 2 equals 1 and 11 % 3 equals 2.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900