# Loopy exercises: Answers # -------------------------- 1. fred(q) ---------------- def fred(q): return q*q-3 # -------------------------- 2. sumFredWhile(n) ---------- def sumFredWhile(n): the_sum=0 i=0 while isofar: # if it's larger than the one we already have, keep it sofar=factor if factor==n: # if the factor is the number itself, then we're done return sofar n /= factor # now reduce the number by that factor, and start again # --------------------------------- 9. Prime or not? ---------------- ## 98767 False ## 987127 True ## 135797533 True ## 12345678911 False ## 12345677654381 True