← Back to challenges

Happy Numbers & More

PythonHardnumbersmath

Instructions

A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.

A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers.

A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself.

A triangular number counts objects arranged in an equilateral triangle. The nth triangular number is the number of dots in the triangular arrangement with n dots on a side, and is equal to the sum of the n natural numbers from 1 to n.

Objective

Create a function that takes a number as an argument and returns a print showing if the number is happy or not, if the number is prime or not, if the number is perfect or not and if the number is triangular or not.

Examples

happy(2) ➞
"2 is an unhappy number.
2 is a prime number.
2 is not a perfect number.
2 is not a triangular number."

happy(7) ➞
"7 is a happy number.
7 is a prime number.
7 is not a perfect number.
7 is not a triangular number."

happy(8128) ➞
"8128 is a happy number.
8128 is not a prime number.
8128 is a perfect number.
8128 is a triangular number."

Notes

N/A

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.