Problem in calculating prime numbers in python [closed]

I'm a noob in programming. I was asked to code a program that can calculate prime numbers. So i coded as provided below. But it prints all the Odd Numbers instead of Prime Numbers. I think the Value start is not being divided by all the integers in the range (2,Startnumber). It just prints all the odd numbers.

startnumber=int(raw_input("Enter a number: "))
start=2
while start<=startnumber:
for divisor in range(2,startnumber):
if start%divisor==0:
break
else:
print start
break
start=start+1

• Although your issue has been solved, would you like a review on your working code? If not, this post will remain closed, preventing any new answers from being posted. – Jamal Aug 16 '13 at 15:34
• ya sure why not.. @Jamal – Chaitanya Aug 17 '13 at 6:27
• Okay. Just edit your question with this request, replacing everything about the non-working code (explanation and the code itself). – Jamal Aug 17 '13 at 7:05

The % is the modulo operator. If you want to check if it's a prime or not you should test if it's only dividable by itself and 1 to get a integer (not a floating point number).

So what you want to do is to loop though a list of integers. In your script you can provide the limit of this list (as I assume). Then for each number in the list you want to check if there's a number other than 1 or itself that can be used to divide the number into integers.

The easiest way to check if a the result of a division is a floating point number or an integer is to convert the result to an integer and check if the number is still the same.

By default if you do math in Python, it only works with integers. Example:

>>> 5 / 2
2


To do a calculation with floating point precision, you need to define at least one of the numbers as a floating point number. You can do this by writing it as a floating point number like this:

>>> 5.0 / 2
2.5


You can also convert integers to floating point numbers using the float method:

>>> float(5)
5.0
>>> float(5) / 2
2.5


You can also convert floats to integers using the int method:

>>> int(2.5)
2
>>> int(float(5))
5


Now if you do a comparison with floats and ints in Python, it doesn't matter if it's a float or int, but what matters is the actual value it represends:

>>> 5 == 5
True
>>> 5.0 == 5
True
>>> 2.5 == 2.5
True
>>> int(2.5) == int(2.5)
True
>>> int(2.5) == 2.5
False


Note the last comparison. We convert the float 2.5 to an int, so that becomes 2, which isn't equal to the float 2.5. We can use this trick to check if the result of a division is a float or an int:

>>> result = 6.0 / 2
>>> result == int(result)
True
>>> result = 5.0 / 2
>>> result == int(result)
False


Great! Let's use that in our loop! We assume the number is a prime until we have proven it's dividable by a number other than 1 or itself.

number = 5.0

# assume the number is a prime
prime = True

# try divisions from 2 through the number - 1
# which are all the numbers between 1 and the number itself
for divisor in range(2, number - 1):

# do the division
result = number / divisor

# check if the result is an integer number
if result == int(result):

# if so, the number is not a prime
prime = False

# since we found it is not a prime,
# we can break out of the for loop
break


The complete script could look something like this:

limit = int(raw_input("Enter a number: "))
number = 0

while number <= limit:

prime = True

for divisor in range(2, number - 1):
result = float(number) / divisor
if result == int(result):
prime = False
break

if prime:
print int(number)

number += 1

• THank YOu sooooo much.... it was really helpful... i highly appreciate. – Chaitanya Aug 16 '13 at 14:21
• Actually when I read Sandeep answer, I realized you can also use modulo which makes sense, because modulo will keep substracting the second number from the first till it's not possible anymore and then returns what's left over. That's also a way to detect if the division will return an integer or a float. – gitaarik Aug 16 '13 at 15:52

There is no need to keep for loop in the while as while is doing the job of for, assuming that your program tests whether the startnumber is prime or not.

using of break in else will terminate your loop once it checks that its not divisible by 2 [ the first divisor] . so break shouldn't be used there in else block as you want to test for numbers above 2.

We can while - else or for-else to solve your problem. the code below shows the representation of your problem in while - else:

startnumber=int(raw_input("Enter a number: "))
start=2
while start<startnumber:

if startnumber%start==0:
print str(startnumber)+" is not a prime."
break
start=start+1
else:
print str(startnumber)+" is a prime."


same code with for -else would be :

startnumber=int(raw_input("Enter a number: "))
start=2
for number in range(2,startnumber):

if startnumber%start==0:
print str(startnumber)+" is not a prime."
break
start=start+1
else:
print str(startnumber)+" is a prime."

• ok, thank you for ur reply... but actually i was trying to print all the prime numbers in the range. i'm able to check if the number is prime but i want to print all the prime numbers – Chaitanya Aug 16 '13 at 14:17
• I presumed based on your prompt for the user. you should change your prompt so that the user understands what you are expecting from him/her. – Sandeep Aug 16 '13 at 14:19