Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-271

Count Happy Numbers Below N

Problem

Count how many happy numbers strictly less than n exist, where a happy number reaches 1 by repeatedly replacing it with the sum of the squares of its digits.

Run the digit-square process with a seen set for every value from 1 to n - 1. Return 0 for n <= 1.

Hint

1 is happy, and unhappy numbers fall into a cycle that the seen set detects.

Test cases

case 1
in: [1]
expected: 0
case 2
in: [2]
expected: 1
case 3
in: [10]
expected: 2
case 4
in: [20]
expected: 5
case 5
in: [100]
expected: 19
Starter codePython
def count_happy_below(n):
    # Your code here
    pass

5 test cases · run your code against them in the editor