#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep 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

    def TwinPrimeNumbers(twinPrime):
    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: ")
    TwinPrimeNumbers(50)

    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. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    Re-post with code tags. The way you posted, it's too difficult to parse.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,896
    Rep Power
    481
    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 items had failures:
    #   1 of   1 in q.isPrime
    #***Test Failed*** 1 failures.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo