Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e6c138b

Browse files
committed
Prob 188 happy number
1 parent c0ace54 commit e6c138b

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 187 |
9+
| Total Problems | 188 |
1010

1111
</center>
1212

@@ -167,7 +167,7 @@ Include contains single header implementation of data structures and some algori
167167
| Calculate factorial of large number, say 100 (it will have 158 digits) |[factorial_of_large_num.cpp](math_problems/factorial_of_large_num.cpp)|
168168
| Generate all possible words from a number entered on a traditional mobile keypad | [phone_digits.cpp](math_problems/phone_digits.cpp)|
169169
| 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)|
170-
170+
| 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)|
171171
### Stack Problems
172172
| Problem | Solution |
173173
| :------------ | :----------: |

‎math_problems/happy_number.cpp‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Detect if a number is a happy number. A number is happy number if
3+
* sequence of operations where number is replaced by sum of square
4+
* of its digits leads eventually to 1. A number is not a happy number
5+
* if we are in an infinite loop when above operations are performed.
6+
*
7+
* Input: 49
8+
* 4^2 + 9^2 = 16 + 81 = 97
9+
* 9^2 + 7^2 = 81 +たす 49 = 130
10+
* 1^2 + 3^2 + 0^2 = 1 +たす 9 +たす 0 = 10
11+
* 1^2 + 0^2 = 1
12+
* Thus 49 is a happy number.
13+
* Approach:
14+
*
15+
* We can treat this problem same as finding a cycle in a linked list.
16+
* Imagine intermediate numbers as node and operation of square and sum
17+
* as link.
18+
* If we find the cycle, and the cycle is at 1, number is a happy number.
19+
* Else, the number is not a happy number.
20+
*/
21+
22+
#include <iostream>
23+
24+
int square_sum(int num)
25+
{
26+
int squared_sum = 0;
27+
while (num > 0) {
28+
squared_sum += (num % 10) * (num % 10);
29+
num /= 10;
30+
}
31+
return squared_sum;
32+
}
33+
34+
bool is_happy(int num)
35+
{
36+
int slow = num;
37+
int fast = num;
38+
39+
do {
40+
slow = square_sum(slow);
41+
fast = square_sum(square_sum(fast));
42+
} while (slow != fast);
43+
return slow == 1;
44+
}
45+
46+
int main()
47+
{
48+
int num = 45;
49+
if (is_happy(num)) {
50+
std::cout << num << " is a happy number." << std::endl;
51+
} else {
52+
std::cout << num << " is not a happy number." << std::endl;
53+
}
54+
num = 49;
55+
if (is_happy(num)) {
56+
std::cout << num << " is a happy number." << std::endl;
57+
} else {
58+
std::cout << num << " is not a happy number." << std::endl;
59+
}
60+
return 0;
61+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /