1

I write a program that find the prime numbers . My teacher told me to do that in three steps . Firstly i had to generate all odds numbers . Then i need to check if it is devigable or not . To do that he tell me to use % operator . Here is my code :

for num in range(3,10):
    if num%2 != 0: #generating odds
        for i in range(3,num):
            if num%i != 0:
                print num
            else:
                None
    else:
        None

Now my question is , the above code is right to generate prime ?

The above code gives me wrong ans . But where's the bugs here ? I need the explenation .

Sample output of the above code is: 3 5 5 5 7 7 7 7 7 9 9 9 9 9 9

Tanzir Uddin
  • 51
  • 1
  • 9

3 Answers3

1

The second for loop should be terminated if num is not a prime number.

for num in range(3, 100):
    if num % 2 != 0:
        for i in range(3, num):
            if num % i == 0:
                break
        else:
            print(num)

I found the official document for you:

Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement.

Luckily, it provides with a sample about prime number.

tanglong
  • 278
  • 2
  • 11
  • The last else statement remains at the level of second for statement . I can't undestand that . Do it should at the level of second if statement ? If so , then the result is wrong . Can you please explain in brief about the use of else statement here – Tanzir Uddin May 20 '16 at 07:59
  • @user6359012 if the second `for` loop never trigger off `break`, then it goto execute statement in `else` – tanglong May 20 '16 at 08:05
  • Thank you . I understand now . – Tanzir Uddin May 20 '16 at 08:18
  • For completeness sake, '2' should be included in the output. Namely, the script should look like so: print(2),;for num in range(3, 100):; if num % 2 != 0:; for i in range(3, num):; if num % i == 0:; break; else:; print(num),; – boardrider May 21 '16 at 01:06
1

You have some correct ideas, but also some bugs. Since you mentioned a teacher I'll not give you a code example, just some hints.

You iterate (count upwards) for all numbers from 3 to 10. That's missing the number 2 already, which is also prime. You should just print that before everything else, to be complete.

Then you check if a number is odd by %2. That is a good idea, but it is also possible in python to iterate over only odd numbers by using range(3,10,2) instead. The last number is the step size, so it would count 3, 5, 7, 9. (This is just an enhancement, nothing wrong with your version)

Next you loop over all numbers smaller than the current number. First, you can already stop at num/2, because a divisor cannot be larger than that. Your bug comes now: you print num every time one of the i does not divide. This is not a prime number. It's only a prime number if NONE of the numbers i divide num. I suggest you reverse your logic: print in the else statement and use the python statement break to escape the loop if one of the is divide.

Additionally: else: None doesn't do anything, you can just leave it out. In Python not every if has to have an else.

StefanS
  • 1,740
  • 14
  • 20
-2

i provide pseudo code in C language you understand it and try

int num=40; //your variable
for(int i=3;i<num;i++)
{
 if(i%2!=0)  //odd
 {
    int j=3;
    for( ;j<i;J++)
    {
     if(i%j==0)
      {
       printf("not Prime");
       break;
      }
    }
   if(i==j)
    {
     printf("num is prime=%d",&i);
    }

}
else
{
 printf("none")
}
}   
Vinod Kumawat
  • 741
  • 5
  • 8