# sum the digits of a number def sum_digits (n): sum_num = 0 while (n > 0): sum_num += (n % 10) n = n // 10 return sum_num # reverse a number def rev_num (n): rev_n = 0 while (n > 0): rev_n = rev_n * 10 + (n % 10) n = n // 10 return rev_n # determine if a number is palindromic def is_palindromic (n): return (n == rev_num(n)) # sum the proper divisors of a number def sum_divisors (n): sum_div = 0 limit = n // 2 div = 1 while (div <= limit): if (n % div == 0): sum_div += div div += 1 return sum_div # determine if a number is prime def is_prime (n): if (n == 1): return False limit = int (n ** 0.5) + 1 div = 2 while (div < limit): if (n % div == 0): return False div += 1 return True def main(): # prompt the user to enter a number num = int (input ("Enter a number: ")) # print the sum of the digits print (sum_digits (num)) print () # print the reverse of a number print (rev_num (num)) print () # print all perfect numbers less than 10000 n = 1 while (n <= 10000): if (n == sum_divisors (n)): print (n) n += 1 print () # find 4 digit non-palindromic number whose cube is palindromic for n in range (1000, 10000): if (not is_palindromic (n) and is_palindromic (n * n * n)): print (n) break print () # print all prime numbers less than equal to 50 for i in range (1, 51): if (is_prime(i)): print (i) print () # find all 3-digit emirps for n in range (100, 1000): cond1 = not (is_palindromic (n)) and is_prime (n) cond2 = is_prime (rev_num(n)) if (cond1 and cond2): print (n) print() # find all twin primes less than 100 for n in range (2, 100): if (is_prime (n) and is_prime (n + 2)): n2 = n + 2 print (n, n2) print () main()