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

    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    Help w/ Writing Function Definitions


    Hi, I am taking a class in order to learn Python. One of the exercises I need to do is write function definitions. I cannot figure out how to do one of them. To show you an example here is a similar problem:

    If m is an integer, then isPrime(m) iff m is prime.
    The code:

    # Prompts the user for an integer m and then computes and prints whether
    # m is prime or not.

    # isPrime(m): I -> Bool
    # If m is an integer, then isPrime(m) if and only if m is prime.
    def isPrime(m):
    return False if m <= 1 else isPrimeItr(1,0,m)

    # isPrimeItr(i,a,m): I x I x I -> Bool
    def isPrimeItr(i,a,m):
    return False if a> 2 else True if a == 2 and i == m +1 else isPrimeItr(i+1,a+1,m) if m % i == 0 else isPrimeItr(i+1,a,m)

    # print a brief description of the program followed by an empty line
    print("Computing Prime Numbers")
    print("Prompts the user an integer value for m and then computes and")
    print("prints if m is prime or not.")
    print()

    # prompt the user for a value for m
    m = eval(input("Enter an integer value for m: "))

    # print if m is prime
    print("The value that", m, "is a prime integer is", isPrime(m))

    ------------------------------------------------------------------------------------------------------------------------

    These are the problems I am having problem with:

    If m and n are integers, then anyPrimes(m,n) iff there are any prime numbers in the interval [m,n).

    If m and n are integers, then countPrimes(m,n) is the number of prime numbers in the interval [m,n).

    If anyone could help that would be great. Thank you.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,710
    Rep Power
    480
    maybe you could construct the other functions using this primality test.
    Code:
    from math import sqrt # **0.5
    
    def isPrime(A):
        '''tested for the first 62 primes'''
        if A < 2: # my choice.
            return False
        if A in (2,3,):
            return True
        if (A-1)%6 and (A+1)%6:
            return False
        for potential_divisor in range(5, 1+int(sqrt(A)), 2): # could be a list of primes found so far, could continue to use the 6 \pm 1 information
            if not (A % potential_divisor):
                return False
        return True
    
    for i in range(-3, 300):
        if isPrime(i):
            print(i, end=' ')
    
    # Let A be the vector printed by this program.
    # Then the j sentence  (-:p:@i.@#)A  confirms the primality of the values,
    # and that all primes through the first 62 are included in the vector.
    # www.jsoftware.com  J no longer looks like line noise to me.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo