From 613c28d0410534f856159fbaec663209865f8ff9 Mon Sep 17 00:00:00 2001 From: Abaikumar Date: Sat, 7 Oct 2023 09:28:21 +0530 Subject: [PATCH 1/3] Happy Number Find --- C++/Maths/HappyNumber.cpp | 33 +++++++++++++++++++++++++++++ Java/Maths/HappyNumber.java | 41 +++++++++++++++++++++++++++++++++++++ Python/Maths/HappyNumber.py | 21 +++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 C++/Maths/HappyNumber.cpp create mode 100644 Java/Maths/HappyNumber.java create mode 100644 Python/Maths/HappyNumber.py diff --git a/C++/Maths/HappyNumber.cpp b/C++/Maths/HappyNumber.cpp new file mode 100644 index 0000000..cfef643 --- /dev/null +++ b/C++/Maths/HappyNumber.cpp @@ -0,0 +1,33 @@ +#include +#include + +bool isHappyNumber(int n) { + std::unordered_set seen; + + while (n != 1 && seen.find(n) == seen.end()) { + seen.insert(n); + int sum = 0; + while (n > 0) { + int digit = n % 10; + sum += digit * digit; + n /= 10; + } + n = sum; + } + + return n == 1; +} + +int main() { + int n; + std::cout << "Enter a number to find if it is a Happy Number or Not: "; + std::cin >> n; + + if (isHappyNumber(n)) { + std::cout << n << " is a Happy number" << std::endl; + } else { + std::cout << n << " is not a Happy number" << std::endl; + } + + return 0; +} diff --git a/Java/Maths/HappyNumber.java b/Java/Maths/HappyNumber.java new file mode 100644 index 0000000..817c4ea --- /dev/null +++ b/Java/Maths/HappyNumber.java @@ -0,0 +1,41 @@ +/* +A number is called happy if it leads to 1 after a sequence of steps wherein each +step number is replaced by the sum of squares of its digit that is if we start +with Happy Number and keep replacing it with digits square sum, we reach 1. +*/ + +import java.util.Scanner; + +class Happynumber { + static boolean isHappynumber(int n) { + if (n == 1 || n == 7) + return true; + int sum = n, x = n; + while (sum > 9) { + sum = 0; + while (x > 0) { + int d = x % 10; + sum += d * d; + x /= 10; + } + if (sum == 1) + return true; + x = sum; + } + if (sum == 7) + return true; + return false; + } + + public static void main(String[] args) { + int n; + Scanner sc = new Scanner(System.in); + System.out.print("Enter number to find Happy Number or Not: "); + n = sc.nextInt(); + if (isHappynumber(n)) + System.out.println(n + " is a Happy number"); + else + System.out.println(n + " is not a Happy number"); + sc.close(); + } +} \ No newline at end of file diff --git a/Python/Maths/HappyNumber.py b/Python/Maths/HappyNumber.py new file mode 100644 index 0000000..93c81ea --- /dev/null +++ b/Python/Maths/HappyNumber.py @@ -0,0 +1,21 @@ +def is_happy_number(n): + def get_next(num): + total_sum = 0 + while num > 0: + num, digit = divmod(num, 10) + total_sum += digit ** 2 + return total_sum + + seen = set() + while n != 1 and n not in seen: + seen.add(n) + n = get_next(n) + + return n == 1 + +if __name__ == "__main__": + n = int(input("Enter number to find Happy Number or Not: ")) + if is_happy_number(n): + print(f"{n} is a Happy number") + else: + print(f"{n} is not a Happy number") From 0e95f6a61f2ccc62cfd52b8725a99730fd152924 Mon Sep 17 00:00:00 2001 From: Abaikumar Date: Sat, 7 Oct 2023 09:33:27 +0530 Subject: [PATCH 2/3] happy number created --- C++/Maths/HappyNumber.cpp | 24 ++++++++++++++++++++++++ Java/Maths/HappyNumber.java | 25 ++++++++++++++++++++++--- Python/Maths/HappyNumber.py | 23 +++++++++++++++++++++++ 3 files changed, 69 insertions(+), 3 deletions(-) diff --git a/C++/Maths/HappyNumber.cpp b/C++/Maths/HappyNumber.cpp index cfef643..a76ebf5 100644 --- a/C++/Maths/HappyNumber.cpp +++ b/C++/Maths/HappyNumber.cpp @@ -1,3 +1,27 @@ +/* +A happy number is defined as a number that leads to 1 after a sequence of steps, where each step involves replacing the number with the sum of the squares of its digits. To determine if a number is a happy number, we can follow these steps: + +Approach: +Initialize a set called seen to keep track of numbers encountered during the process to detect cycles. +Repeat the following steps until either n becomes 1 (which makes it a happy number) or n is encountered again (indicating a cycle, and the number is not happy): + a. Add the current value of n to the seen set. + b. Calculate the sum of the squares of its digits by repeatedly taking the last digit of n, squaring it, and adding it to sum. Update n by dividing it by 10. + c. Update n with the calculated sum. +If n eventually becomes 1, it's a happy number. If it enters a cycle and is encountered again, it's not a happy number. + +Time Complexity: + The time complexity of this algorithm depends on the number of iterations required to determine whether the number is happy or not. In the worst case, the algorithm will run until 'n' becomes 1 or until it enters a cycle. The number of iterations is not fixed and can vary from input to input, so the time complexity is not easily expressed in Big O notation. + +Space Complexity: + The space complexity is determined by the space used to store the seen set, which can contain at most all distinct numbers encountered during the process. In the worst case, this set could contain up to 81 distinct numbers (as the sum of the squares of digits of a number is at most 81 for a 3-digit number). Therefore, the space complexity can be considered O(1) for practical purposes, as the number of distinct numbers stored is limited. + +Sample Input and Output: +Enter a number to find if it is a Happy Number or Not: 19 +19 is a Happy number + +Enter a number to find if it is a Happy Number or Not: 4 +4 is not a Happy number +*/ #include #include diff --git a/Java/Maths/HappyNumber.java b/Java/Maths/HappyNumber.java index 817c4ea..3bf2f4e 100644 --- a/Java/Maths/HappyNumber.java +++ b/Java/Maths/HappyNumber.java @@ -1,7 +1,26 @@ /* -A number is called happy if it leads to 1 after a sequence of steps wherein each -step number is replaced by the sum of squares of its digit that is if we start -with Happy Number and keep replacing it with digits square sum, we reach 1. +A happy number is defined as a number that leads to 1 after a sequence of steps, where each step involves replacing the number with the sum of the squares of its digits. To determine if a number is a happy number, we can follow these steps: + +Approach: +Initialize a set called seen to keep track of numbers encountered during the process to detect cycles. +Repeat the following steps until either n becomes 1 (which makes it a happy number) or n is encountered again (indicating a cycle, and the number is not happy): + a. Add the current value of n to the seen set. + b. Calculate the sum of the squares of its digits by repeatedly taking the last digit of n, squaring it, and adding it to sum. Update n by dividing it by 10. + c. Update n with the calculated sum. +If n eventually becomes 1, it's a happy number. If it enters a cycle and is encountered again, it's not a happy number. + +Time Complexity: + The time complexity of this algorithm depends on the number of iterations required to determine whether the number is happy or not. In the worst case, the algorithm will run until 'n' becomes 1 or until it enters a cycle. The number of iterations is not fixed and can vary from input to input, so the time complexity is not easily expressed in Big O notation. + +Space Complexity: + The space complexity is determined by the space used to store the seen set, which can contain at most all distinct numbers encountered during the process. In the worst case, this set could contain up to 81 distinct numbers (as the sum of the squares of digits of a number is at most 81 for a 3-digit number). Therefore, the space complexity can be considered O(1) for practical purposes, as the number of distinct numbers stored is limited. + +Sample Input and Output: +Enter a number to find if it is a Happy Number or Not: 19 +19 is a Happy number + +Enter a number to find if it is a Happy Number or Not: 4 +4 is not a Happy number */ import java.util.Scanner; diff --git a/Python/Maths/HappyNumber.py b/Python/Maths/HappyNumber.py index 93c81ea..9c4266b 100644 --- a/Python/Maths/HappyNumber.py +++ b/Python/Maths/HappyNumber.py @@ -1,3 +1,26 @@ +# A happy number is defined as a number that leads to 1 after a sequence of steps, where each step involves replacing the number with the sum of the squares of its digits. To determine if a number is a happy number, we can follow these steps: + +# Approach: +# Initialize a set called seen to keep track of numbers encountered during the process to detect cycles. +# Repeat the following steps until either n becomes 1 (which makes it a happy number) or n is encountered again (indicating a cycle, and the number is not happy): +# a. Add the current value of n to the seen set. +# b. Calculate the sum of the squares of its digits by repeatedly taking the last digit of n, squaring it, and adding it to sum. Update n by dividing it by 10. +# c. Update n with the calculated sum. +# If n eventually becomes 1, it's a happy number. If it enters a cycle and is encountered again, it's not a happy number. + +# Time Complexity: +# The time complexity of this algorithm depends on the number of iterations required to determine whether the number is happy or not. In the worst case, the algorithm will run until 'n' becomes 1 or until it enters a cycle. The number of iterations is not fixed and can vary from input to input, so the time complexity is not easily expressed in Big O notation. + +# Space Complexity: +# The space complexity is determined by the space used to store the seen set, which can contain at most all distinct numbers encountered during the process. In the worst case, this set could contain up to 81 distinct numbers (as the sum of the squares of digits of a number is at most 81 for a 3-digit number). Therefore, the space complexity can be considered O(1) for practical purposes, as the number of distinct numbers stored is limited. + +# Sample Input and Output: +# Enter a number to find if it is a Happy Number or Not: 19 +# 19 is a Happy number + +# Enter a number to find if it is a Happy Number or Not: 4 +# 4 is not a Happy number + def is_happy_number(n): def get_next(num): total_sum = 0 From 7b8acef51023d6f94f74174cb32f38fc334fc072 Mon Sep 17 00:00:00 2001 From: Abaikumar Date: Sat, 7 Oct 2023 09:35:27 +0530 Subject: [PATCH 3/3] happy number created --- C++/Maths/HappyNumber.cpp | 4 ++-- Java/Maths/HappyNumber.java | 4 ++-- Python/Maths/HappyNumber.py | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/C++/Maths/HappyNumber.cpp b/C++/Maths/HappyNumber.cpp index a76ebf5..8ea70fa 100644 --- a/C++/Maths/HappyNumber.cpp +++ b/C++/Maths/HappyNumber.cpp @@ -9,10 +9,10 @@ Repeat the following steps until either n becomes 1 (which makes it a happy numb c. Update n with the calculated sum. If n eventually becomes 1, it's a happy number. If it enters a cycle and is encountered again, it's not a happy number. -Time Complexity: +Time Complexity: O(n) The time complexity of this algorithm depends on the number of iterations required to determine whether the number is happy or not. In the worst case, the algorithm will run until 'n' becomes 1 or until it enters a cycle. The number of iterations is not fixed and can vary from input to input, so the time complexity is not easily expressed in Big O notation. -Space Complexity: +Space Complexity: O(1) The space complexity is determined by the space used to store the seen set, which can contain at most all distinct numbers encountered during the process. In the worst case, this set could contain up to 81 distinct numbers (as the sum of the squares of digits of a number is at most 81 for a 3-digit number). Therefore, the space complexity can be considered O(1) for practical purposes, as the number of distinct numbers stored is limited. Sample Input and Output: diff --git a/Java/Maths/HappyNumber.java b/Java/Maths/HappyNumber.java index 3bf2f4e..d9109ab 100644 --- a/Java/Maths/HappyNumber.java +++ b/Java/Maths/HappyNumber.java @@ -9,10 +9,10 @@ c. Update n with the calculated sum. If n eventually becomes 1, it's a happy number. If it enters a cycle and is encountered again, it's not a happy number. -Time Complexity: +Time Complexity: O(n) The time complexity of this algorithm depends on the number of iterations required to determine whether the number is happy or not. In the worst case, the algorithm will run until 'n' becomes 1 or until it enters a cycle. The number of iterations is not fixed and can vary from input to input, so the time complexity is not easily expressed in Big O notation. -Space Complexity: +Space Complexity: O(1) The space complexity is determined by the space used to store the seen set, which can contain at most all distinct numbers encountered during the process. In the worst case, this set could contain up to 81 distinct numbers (as the sum of the squares of digits of a number is at most 81 for a 3-digit number). Therefore, the space complexity can be considered O(1) for practical purposes, as the number of distinct numbers stored is limited. Sample Input and Output: diff --git a/Python/Maths/HappyNumber.py b/Python/Maths/HappyNumber.py index 9c4266b..acf8260 100644 --- a/Python/Maths/HappyNumber.py +++ b/Python/Maths/HappyNumber.py @@ -8,15 +8,15 @@ # c. Update n with the calculated sum. # If n eventually becomes 1, it's a happy number. If it enters a cycle and is encountered again, it's not a happy number. -# Time Complexity: +# Time Complexity: O(n) # The time complexity of this algorithm depends on the number of iterations required to determine whether the number is happy or not. In the worst case, the algorithm will run until 'n' becomes 1 or until it enters a cycle. The number of iterations is not fixed and can vary from input to input, so the time complexity is not easily expressed in Big O notation. -# Space Complexity: +# Space Complexity: O(1) # The space complexity is determined by the space used to store the seen set, which can contain at most all distinct numbers encountered during the process. In the worst case, this set could contain up to 81 distinct numbers (as the sum of the squares of digits of a number is at most 81 for a 3-digit number). Therefore, the space complexity can be considered O(1) for practical purposes, as the number of distinct numbers stored is limited. # Sample Input and Output: -# Enter a number to find if it is a Happy Number or Not: 19 -# 19 is a Happy number +# Enter a number to find if it is a Happy Number or Not: 19 +# 19 is a Happy number # Enter a number to find if it is a Happy Number or Not: 4 # 4 is not a Happy number