Hello again (multiple posts today as I’m really trying to make sure I am taking everything in…)
I have “solved” the is_prime challenge with the following code but I am curious if this is the ‘best’ way to find out if a number is indeed prime? For the sake of neatness I have stripped out user entries and call’s to the function and have only listed the base code in the is_prime function.
1. def is_prime(x): 2. # if a number is less than two it is not Prime 3. if x < 2: 4. return False 5. # if 2 is selected this is a Prime 6. if x == 2: 7. return True 8. # does any previous integer in the range divide evenly into 'x' 9. for i in range(2,x): 10. if x % i == 0: 11. return False 12. return True
I would welcome more experienced users thoughts on other ways to implement this functions.