Reminder from class demonstrations... There are two main techniques for repetition in Python: "while" and "for". Here is an example of each... Suppose we wanted to add up all the numbers from 1 to 10:
def usingWhile():
# Note: this is a more verbose solution than actually necessary
start=1
end=10
subtotal=0
while start <= end:
subtotal=subtotal+start
start+=1
return subtotal
def usingFor():
subtotal=0
for i in range(1,11): # Note: this will assign to the variable i the numbers from 1 to 10 (but not 11)
subtotal+=i # Note: a+=b means a = a + b
return subtotal
>>> factorPairs(24) 1 24 2 12 3 8 4 6 >>> factorPairs(11) 1 11
7. Mini-Challenge Problem: Create the function isPrime(n) that will return True if n is a prime number (n will be an integer greater than 1), and False if n has factors other than 1 and itself. Examples:
>>> isPrime(23) True >>> isPrime(50) False
largestPrimeFactor(24) # returns 3 largestPrimeFactor(56) # returns 7
9. Another Challenge problem: Here are a few numbers (below). Can you use your version of isPrime() above to tell which are prime? Put the answers into the Comments-to-Teacher. If your version of isPrime() takes too long to answer the questions, perhaps you might try to change your function to make it run faster (do less work to come up with the answer).
Which of
the following numbers are prime?
98767
987127
135797533
12345678911
12345677654381