Lecture Notes on 15 Feb 2017 Active Reading: Type code as you read. Read Chapter 6: Sections 6.7 - 6.11 Write the code from the following listings and run them: * Listing 6.5: GCDFunction.py * Listing 6.6: TestGCDFunction.py * Listing 6.7: PrimeNumberFunction.py * Listing 6.8: Decimal2HexConversion.py * Listing 6.9: DefaultArgumentDemo.py * Listing 6.10: MultipleReturnValueDemo.py Challenge Problem A software engineer lived in a neigborhood where all the houses were on one side of the street. The houses were numbered sequentially starting from 1. Every morning she would walk her dog in one direction and in the evening she would walk her dog in the other direction. One one of these walks she noticed something special about her house - the sum of addresses on one side of her house was equal to the sum of the addresses on the other side of her house. [Her house address was not in either sum.] Now her house address was 6 and the last house on the street had an address of 8. The sum of addresses on one side was 15 [= 1 + 2 + 3 + 4 + 5] and on the other side was also 15 [= 7 + 8]. She was convinced that hers was a lucky house since it had that property. When she relocated to another town she went to the real estate agent and asked for a house that had exactly the same property - the sum of addresses on one side had to be equal to the sum of addresses on the other side. Now the lucky house number is dependent on the last house on the street. We know that the first set of numbers (the lucky house and the last house on the street) that satisfies that property is 6 and 8. What is the next set of numbers that have the same property. # 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 # 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 # 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 # determines if a number is palindromic def is_palindromic (n): return n == rev_num(n) # determine if a number is prime def is_prime (n): if (n == 1): return False limit = int (n ** 0.5) + 1 divisor = 2 while (divisor < limit): if (n % divisor == 0): return False divisor += 1 return True def main(): # find a 4 digit non-palindromic number whose cube is palindromic for n in range (1000, 10000): n3 = n * n * n if (not is_palindromic (n) and is_palindromic (n3)): print (n) break # 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) # list all twin primes less than 100 for n in range (2, 100): n2 = n + 2 if (is_prime (n) and is_prime (n2)): print (n, n2) main()