From 64afc01a8a56a4fb8c99bb704a8ba240f7ceb68e Mon Sep 17 00:00:00 2001 From: jaydev Date: Mon, 24 Nov 2025 12:33:07 +0530 Subject: [PATCH] Enhance comments in happy number function Added comments to clarify the logic of the happy number algorithm. --- 202_Happy_Number.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/202_Happy_Number.py b/202_Happy_Number.py index 8436d372c..5a0c5856b 100644 --- a/202_Happy_Number.py +++ b/202_Happy_Number.py @@ -4,9 +4,13 @@ def isHappy(self, n): :type n: int :rtype: bool """ - # https://en.wikipedia.org/wiki/Happy_number + # set to store numbers already seen to detect loops seen_numbers = set() + # loop continues until n becomes 1 or a loop is found while n > 1 and n not in seen_numbers: + # store current number to check if it repeats later seen_numbers.add(n) + # replaces n with the sum of squares of its digits n = sum(map(lambda x: int(x) * int(x), list(str(n)))) - return n == 1 \ No newline at end of file + # if n becomes 1, it's a happy number + return n == 1