update,
This commit is contained in:
43
max015/CDS2003_assignment02/src/P07.py
Normal file
43
max015/CDS2003_assignment02/src/P07.py
Normal file
@@ -0,0 +1,43 @@
|
||||
'''
|
||||
Draft a python script to solve the following questions. thanks
|
||||
|
||||
# Requirement
|
||||
Please use recursion to define and test a function to check whether a number is Prime or not.
|
||||
|
||||
# procedure
|
||||
- let's think it step by step
|
||||
- leave me comment with your code
|
||||
- solve with recursion
|
||||
- write your code in `def isPrime_Recur(a,b):` , with comment `# implementation here`
|
||||
- write a sample function to use it, with comment `# test here`
|
||||
'''
|
||||
|
||||
import sys
|
||||
sys.setrecursionlimit(999999)
|
||||
|
||||
|
||||
def isPrime_Recur(a,b):
|
||||
# implementation here
|
||||
|
||||
# https://t5k.org/notes/faq/negative_primes.html
|
||||
if a < 0:
|
||||
return "tested number should be greater than 1"
|
||||
|
||||
# https://corp.tutorocean.com/is-1-a-prime-number/
|
||||
if a == 0 or a == 1:
|
||||
return "Conservative result: tested number is neither prime nor composite"
|
||||
|
||||
if b == 1:
|
||||
return True
|
||||
|
||||
if a % b == 0:
|
||||
return False
|
||||
else:
|
||||
return isPrime_Recur(a,b-1)
|
||||
|
||||
# test here
|
||||
if __name__ == '__main__':
|
||||
num_list = [-99, -3, -1, 0, 1, 3, 7, 9, 9999, 10007]
|
||||
for num in num_list:
|
||||
print('is ', num, ' Prime?', isPrime_Recur(num, num-1))
|
||||
|
Reference in New Issue
Block a user