Skip to content

Commit

Permalink
Prob 188 happy number
Browse files Browse the repository at this point in the history
  • Loading branch information
mandliya committed Feb 9, 2018
1 parent c0ace54 commit e6c138b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

| Current Status| Stats |
| :------------: | :----------: |
| Total Problems | 187 |
| Total Problems | 188 |

</center>

Expand Down Expand Up @@ -167,7 +167,7 @@ Include contains single header implementation of data structures and some algori
| Calculate factorial of large number, say 100 (it will have 158 digits) |[factorial_of_large_num.cpp](math_problems/factorial_of_large_num.cpp)|
| Generate all possible words from a number entered on a traditional mobile keypad | [phone_digits.cpp](math_problems/phone_digits.cpp)|
| Given a string representation of a number, remove n characters from the string such that number representation is lowest possible.| [lowest_possible_number.cpp](math_problems/lowest_possible_number.cpp)|

| Detect if a number is a happy number. A number is happy number if sequence of operations where number is replaced by sum of square of its digits leads eventually to 1. A number is not a happy number if we are in an infinite loop when above operations are performed.| [happy_number.cpp](math_problems/happy_number.cpp)|
### Stack Problems
| Problem | Solution |
| :------------ | :----------: |
Expand Down
61 changes: 61 additions & 0 deletions math_problems/happy_number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Detect if a number is a happy number. A number is happy number if
* sequence of operations where number is replaced by sum of square
* of its digits leads eventually to 1. A number is not a happy number
* if we are in an infinite loop when above operations are performed.
*
* Input: 49
* 4^2 + 9^2 = 16 + 81 = 97
* 9^2 + 7^2 = 81 + 49 = 130
* 1^2 + 3^2 + 0^2 = 1 + 9 + 0 = 10
* 1^2 + 0^2 = 1
* Thus 49 is a happy number.
* Approach:
*
* We can treat this problem same as finding a cycle in a linked list.
* Imagine intermediate numbers as node and operation of square and sum
* as link.
* If we find the cycle, and the cycle is at 1, number is a happy number.
* Else, the number is not a happy number.
*/

#include <iostream>

int square_sum(int num)
{
int squared_sum = 0;
while (num > 0) {
squared_sum += (num % 10) * (num % 10);
num /= 10;
}
return squared_sum;
}

bool is_happy(int num)
{
int slow = num;
int fast = num;

do {
slow = square_sum(slow);
fast = square_sum(square_sum(fast));
} while (slow != fast);
return slow == 1;
}

int main()
{
int num = 45;
if (is_happy(num)) {
std::cout << num << " is a happy number." << std::endl;
} else {
std::cout << num << " is not a happy number." << std::endl;
}
num = 49;
if (is_happy(num)) {
std::cout << num << " is a happy number." << std::endl;
} else {
std::cout << num << " is not a happy number." << std::endl;
}
return 0;
}

0 comments on commit e6c138b

Please sign in to comment.