From 611a191667e49bf35c2b74402201de8aa3cb9a06 Mon Sep 17 00:00:00 2001 From: Mojirade18 Date: Mon, 20 Oct 2025 22:47:40 +0100 Subject: [PATCH 1/3] CPP: add Happy Number solution (IEEEXtreme style) --- CPP/Basics/happy_number.cpp | 72 +++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 CPP/Basics/happy_number.cpp diff --git a/CPP/Basics/happy_number.cpp b/CPP/Basics/happy_number.cpp new file mode 100644 index 0000000..7d446fa --- /dev/null +++ b/CPP/Basics/happy_number.cpp @@ -0,0 +1,72 @@ +// happy_number.cpp +// Problem: Happy Number (IEEEXtreme / LeetCode 202 style) +// Description: +// A number is "happy" if repeatedly replacing the number by the sum of the +// squares of its digits eventually leads to 1. If the process enters a +// cycle that does not include 1, the number is not happy. +// Input/Output for this implementation: +// - This program reads a single positive integer n from stdin, +// computes whether n is a happy number, and prints "YES" if happy, +// otherwise prints "NO". +// Complexity: +// - Each transformation is O(d) where d = number of digits (<= log10 n). +// - For base-10 the sequence enters a cycle within limited bounds; overall +// expected time is small; space O(1) (Floyd cycle-detection) or O(k) if +// using a set where k is number of visited states. + +#include +using namespace std; + +// compute sum of squares of digits of x +long long sum_of_squares(long long x) { + long long s = 0; + while (x > 0) { + int d = x % 10; + s += 1LL * d * d; + x /= 10; + } + return s; +} + +// Approach 1: Floyd's cycle detection (tortoise & hare). +// If the sequence reaches 1 -> happy. If cycle forms and doesn't include 1 -> not happy. +bool isHappy(long long n) { + auto next = [](long long x){ return sum_of_squares(x); }; + + long long tortoise = n; + long long hare = n; + while (true) { + tortoise = next(tortoise); // move by 1 + hare = next(next(hare)); // move by 2 + if (tortoise == 1 || hare == 1) return true; + if (tortoise == hare) return false; // cycle detected + } +} + +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + + long long n; + if (!(cin >> n)) return 0; + if (n <= 0) { + cout << "NO\n"; + return 0; + } + + cout << (isHappy(n) ? "YES\n" : "NO\n"); + return 0; +} + +/* +Sample: +Input: +19 +Output: +YES + +Explanation: +19 -> 1^2 + 9^2 = 82 +82 -> 8^2 + 2^2 = 68 +68 -> 6^2 + 8^2 = 100 +100 -> 1^2 + 0 + 0 = 1 -> happy \ No newline at end of file From db6168c3c01708f460ca714a76794a7f545344dc Mon Sep 17 00:00:00 2001 From: Mojirade18 Date: Mon, 20 Oct 2025 22:55:48 +0100 Subject: [PATCH 2/3] CPP: add Happy Number solution (IEEEXtreme style) --- CPP/Basics/happy_number.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CPP/Basics/happy_number.cpp b/CPP/Basics/happy_number.cpp index 7d446fa..d491f1e 100644 --- a/CPP/Basics/happy_number.cpp +++ b/CPP/Basics/happy_number.cpp @@ -1,4 +1,5 @@ -// happy_number.cpp +// Problem: Happy Number +// Language: C++ // Problem: Happy Number (IEEEXtreme / LeetCode 202 style) // Description: // A number is "happy" if repeatedly replacing the number by the sum of the From d8f0674e0ebdd24af7c9262767c3e7e39f57f7be Mon Sep 17 00:00:00 2001 From: Mojirade18 Date: Mon, 20 Oct 2025 22:59:48 +0100 Subject: [PATCH 3/3] Fix: make Happy Number code CI-compliant (remove nonstandard headers) --- CPP/Basics/happy_number.cpp | 58 +++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/CPP/Basics/happy_number.cpp b/CPP/Basics/happy_number.cpp index d491f1e..0a0e557 100644 --- a/CPP/Basics/happy_number.cpp +++ b/CPP/Basics/happy_number.cpp @@ -1,44 +1,45 @@ // Problem: Happy Number // Language: C++ -// Problem: Happy Number (IEEEXtreme / LeetCode 202 style) // Description: -// A number is "happy" if repeatedly replacing the number by the sum of the -// squares of its digits eventually leads to 1. If the process enters a -// cycle that does not include 1, the number is not happy. -// Input/Output for this implementation: -// - This program reads a single positive integer n from stdin, -// computes whether n is a happy number, and prints "YES" if happy, -// otherwise prints "NO". -// Complexity: -// - Each transformation is O(d) where d = number of digits (<= log10 n). -// - For base-10 the sequence enters a cycle within limited bounds; overall -// expected time is small; space O(1) (Floyd cycle-detection) or O(k) if -// using a set where k is number of visited states. +// A number is "happy" if repeatedly replacing the number by the sum +// of the squares of its digits eventually leads to 1. If it enters +// a cycle that does not include 1, the number is not happy. +// Input: A single positive integer n +// Output: "YES" if n is a happy number, otherwise "NO" + +#include -#include using namespace std; -// compute sum of squares of digits of x +// Function to compute the sum of squares of digits long long sum_of_squares(long long x) { long long s = 0; while (x > 0) { - int d = x % 10; - s += 1LL * d * d; + int d = static_cast(x % 10); + s += static_cast(d) * d; x /= 10; } return s; } -// Approach 1: Floyd's cycle detection (tortoise & hare). -// If the sequence reaches 1 -> happy. If cycle forms and doesn't include 1 -> not happy. +// Floyd's cycle detection algorithm bool isHappy(long long n) { - auto next = [](long long x){ return sum_of_squares(x); }; + auto next = [](long long x) { + long long s = 0; + while (x > 0) { + int d = static_cast(x % 10); + s += static_cast(d) * d; + x /= 10; + } + return s; + }; long long tortoise = n; long long hare = n; + while (true) { - tortoise = next(tortoise); // move by 1 - hare = next(next(hare)); // move by 2 + tortoise = next(tortoise); + hare = next(next(hare)); if (tortoise == 1 || hare == 1) return true; if (tortoise == hare) return false; // cycle detected } @@ -50,6 +51,7 @@ int main() { long long n; if (!(cin >> n)) return 0; + if (n <= 0) { cout << "NO\n"; return 0; @@ -60,14 +62,8 @@ int main() { } /* -Sample: -Input: +Sample Input: 19 -Output: +Sample Output: YES - -Explanation: -19 -> 1^2 + 9^2 = 82 -82 -> 8^2 + 2^2 = 68 -68 -> 6^2 + 8^2 = 100 -100 -> 1^2 + 0 + 0 = 1 -> happy \ No newline at end of file +*/