Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Happy Number Program added #88

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions C++/Maths/HappyNumber.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
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: 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: 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: 4
4 is not a Happy number
*/
#include <iostream>
#include <unordered_set>

bool isHappyNumber(int n) {
std::unordered_set<int> 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;
}
60 changes: 60 additions & 0 deletions Java/Maths/HappyNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
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: 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: 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: 4
4 is not a Happy number
*/

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();
}
}
44 changes: 44 additions & 0 deletions Python/Maths/HappyNumber.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 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: 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: 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: 4
# 4 is not a Happy number

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")