Lecture Notes on 5 October 2012 def sumDigits (n): sum = 0 while (n > 0): sum = sum + (n % 10) n = n // 10 return sum def revNum (n): rev_num = 0 while (n > 0): rev_num = rev_num * 10 + (n % 10) n = n // 10 return rev_num def sumDivisors (n): sum = 0 for divisor in range (1, n // 2 + 1): if (n % divisor == 0): sum = sum + divisor return sum def isPalindromic (num): return (num == revNum(num)) def isPrime (num): limit = int (num ** 0.5) divisor = 2 while (divisor <= limit): if (num % divisor == 0): return False divisor = divisor + 1 return True def main (): # Test sumDivisors() function num = eval (input ("Enter a number: ")) sum_divisors = sumDivisors (num) print ("Sum of divisors is ", sum_divisors) # print all perfect numbers less than 10000 for num in range (2, 10001): if (num == sumDivisors (num)): print (num) # print all prime numbers in a user defined range lo, hi = eval (input ("Enter a range: ")) for n in range (lo, hi + 1): if (isPrime (n)): print (n) # find the non palindromic number whose cube is palindromic for n in range (1000, 10000): if (not isPalindromic (n) and isPalindromic (n * n * n)): print (n) break # find the prime that remains prime when added to it reversal for n in range (100, 1000): if (isPrime(n) and isPrime (n + revNum(n))): print (n) break main()