Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python: This program will determine if the number entered by the user is happy or not happy. This process involves computing the sum of the

Python: This program will determine if the number entered by the user is happy or not happy. This process involves computing the sum of the squares of the digits of a number. If the number (n) is not initially 1 or 42, n is updated to be the sum of the squares of its digits. This process is then repeated until n either n reaches 1(happy) or 42(not happy). This process is explained further below. Your program should include the following functions:
sum_of_squares - This function has one parameter of type int and returns the sum of the squares (an integer). This function can assume the argument is a non-negative integer. To calculate the sum of squares, square each digit individually then sum all the squares. For example, sum_of_squares(123) returns 14 since 1*1+2*2+3*3=14. To implement the sum of squares, use the % and // operators. Remember that for any integer n greater than zero, n %10 will result in the digit in the one's place, and that n //10 will result in all the digits except for the digit in the one's place. For example, 123%10 is 3 and 123//10 is 12. An alternative approach is to convert the parameter to a string, then iterate through the characters (digits) of the string, converting each digits back to a number and squaring it.
happy - This function has one parameter of type int and returns a boolean value representing if the parameter number is happy or not happy. For example, happy(5) returns False. To determine if a number is happy or not happy, repeatedly find the sum of the squares until either 1 or 42 is reached. If 1 is reached, the number is happy, and if 42 is reached, the number is not happy. Each time you call the sum_of_squares function, be sure to print the result to the screen. For example, happy(13) returns True since 1*1+3*3=10, and then 1*1+0*0=1, and the function call happy(13) would print the following (also see the sample run above):
10
1
Note that if 42 is reached, then the number is stuck in a circle: 42,4*4+2*2=20,2*2+0*0=4,4*4=16,1*1+6*6=37,3*3+7*7=58,5*5+8*8=89,8*8+9*9=145,1*1+4*4+5*5=42(perhaps we should call these numbers dizzy numbers!).
main - This function has no parameters and does not return a value. This function is the main driver of the program. It should first print the welcome message. Then it should ask the user to input a number, call the happy function, and based on the returned value, print either:
# is NOT a happy number. :(
or
# is a happy number. :)
where # is the number input by the user.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Provide examples of Dimensional Tables.

Answered: 1 week ago