r/pythonhelp • u/flootnooot • Dec 07 '21
HOMEWORK I have an assignment due and no idea what to do.
Greek mathematicians took a special interest in numbers that are equal to the sum of their proper divisors (a proper divisor of n is any divisor less than n itself). They called such numbers perfect numbers. For example, 6 is a perfect number because it is the sum of 1, 2, and 3, which are the integers less than 6 that divide evenly into 6. Similarly, 28 is a perfect number because it is the sum of 1, 2, 4, 7, and 14. Write a function sum divisors that takes an integer n and returns the sum of all the proper divisors of that number (here you will want to use the remainder operator, %). Use this function in writing a program to check for perfect numbers in the range 1 to 9999 by testing each number in turn. When a perfect number is found, your program should display it on the screen. The first two lines of output should be 6 and 28. Your program should find two other perfect numbers in the range as well.
Write a program that asks the user to enter either a binary or octal string of numbers. Then convert this string to the corresponding decimal value. You should include two functions binary to decimal and octal to decimal which perform these conversions. Additionally, this program should include a menu interface similar to that of Area options.py from Homework 5. You should ask the user what type they are converting from (i.e. binary or octal) accounting for input errors on their part, and continue running the program until the user indicates they want to quit. A few things to remember for this: • The computer will be reading in numbers from the user as if they were decimal values by default, so if the user enters (101)2 this will be stored as (101)10 when in actuality this is the decimal number 5. So we have to take this into account when converting. • One trick for isolating place values from right to left is by taking remainder after division by 10 (since the computer is treating these as decimal values). For example, in the binary to decimal function 101%10 would give 1 as a remainder which is the first place value. • We can move to future place values by subtracting the first place value away, then dividing by 10. For example, 101 − 1 = 100 then dividing by 10 we get 100/10 = 10. So we can then repeat the above process again: the remainder from division by 10 will now be 0, 10 − 0 = 10 then 10/10 = 1 and now we have isolated each place value one at a time. • All that is left is to figure out what to do with each place value, and then try implementing a similar process for the octal to decimal function. • You may also want to remember the difference between the / and // operators in python