Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
April 14th, 2013, 11:11 PM
 lmp725
Registered User

Join Date: Apr 2013
Posts: 3
Time spent in forums: 8 m
Reputation Power: 0
Help with code!?!?

def isPrime(number):
divisor = 3
while divisor <= number / 2:
if number % divisor == 0:

return False # number is not a prime
divisor += 3

return True # number is prime

NUMBER_OF_PRIMES = 50
count = 0 # count the number of prime numbers
number = 2 # number to be tested for primeness

for i in range(3, 1001):
if isPrime(number):
value = number + 1
print("(", number, ",", value, ")", end = '')

number += 1

def main():
print("The Twin Prime Numbers are: ")

main()

I keep getting an incorrect answer for this assignment in my class. It is displaying: ( 2 , 3 )( 3 , 4 )( 4 , 5 )( 5 , 6 )( 7 , 8 )( 8 , 9 )( 10 , 11 )( 11 , 12 )( 13 , 14 ).......

Any suggestions!!!

#2
April 15th, 2013, 10:12 AM
 rrashkin
Contributing User

Join Date: May 2012
Location: 39N 104.28W
Posts: 139
Time spent in forums: 2 Days 20 m 52 sec
Reputation Power: 2
Re-post with code tags. The way you posted, it's too difficult to parse.

#3
April 15th, 2013, 01:33 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,156
Time spent in forums: 1 Month 3 Weeks 2 Days 8 h 36 m 31 sec
Reputation Power: 455
In addition to rrashkin's comment you must also explain what output you expect. What are some test cases? What is the problem?

Let's for the moment assume that isPrime returns True if its input is a prime number, otherwise it returns False. Next let's insert a doctest to see if your prime testing algorithm is plausible. You'll see that the algorithm gives false positives, reporting that 10 is prime. Invoke the doctest, close inspection shows isPrime tests these values as true primes:
# 4 True
# 8 True
# 10 True
# 14 True
# 16 True
# 20 True
# 22 True
# 25 True
They look like even numbers, but....wait? Why does 25 pass this prime test? I'm not so worried about 1 testing as prime. You can fix that easily.
Code:
```'''
satisfy doctest search for module level test
>>> None
'''
def isPrime(number):
'''
doctest for isPrime.  Command line use:
python3 -m doctest -v thisFile.py

>>> for n in range(1,26):
...     print('%-2d %s'%(n,isPrime(n)))
...
1  False
2  True
3  True
4  False
5  True
6  False
7  True
8  False
9  False
10 False
11 True
12 False
13 True
14 False
15 False
16 False
17 True
18 False
19 True
20 False
21 False
22 False
23 True
24 False
25 False
'''
divisor = 3
while divisor <= number / 2:
if number % divisor == 0:
return False # number is not a prime
divisor += 3
return True # number is prime

# result of doctest
#\$ ( cd /tmp ; python3 -m doctest q.py )  # bash command
#**********************************************************************
#File "q.py", line 10, in q.isPrime
#Failed example:
#    for n in range(1,26):
#        print('%-2d %s'%(n,isPrime(n)))
#Expected:
#    1  False
#    2  True
#    3  True
#    4  False
#    5  True
#    6  False
#    7  True
#    8  False
#    9  False
#    10 False
#    11 True
#    12 False
#    13 True
#    14 False
#    15 False
#    16 False
#    17 True
#    18 False
#    19 True
#    20 False
#    21 False
#    22 False
#    23 True
#    24 False
#    25 False
#Got:
#    1  True
#    2  True
#    3  True
#    4  True
#    5  True
#    6  False
#    7  True
#    8  True
#    9  False
#    10 True
#    11 True
#    12 False
#    13 True
#    14 True
#    15 False
#    16 True
#    17 True
#    18 False
#    19 True
#    20 True
#    21 False
#    22 True
#    23 True
#    24 False
#    25 True
#**********************************************************************
#   1 of   1 in q.isPrime
#***Test Failed*** 1 failures.```
__________________
[code]Code tags[/code] are essential for python code!

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Help with code!?!?