-
Notifications
You must be signed in to change notification settings - Fork 5
/
happy_number.dart
193 lines (162 loc) · 4.19 KB
/
happy_number.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
-* Happy Number *-
Write an algorithm to determine if a number n is happy.
A happy number is a number defined by the following process:
Starting with any positive integer, replace the number by the sum of the squares of its digits.
Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
Those numbers for which this process ends in 1 are happy.
Return true if n is a happy number, and false if not.
Example 1:
Input: n = 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
Example 2:
Input: n = 2
Output: false
Constraints:
1 <= n <= 231 - 1
*/
import 'dart:collection';
import 'dart:math';
class A {
/*
Complexity Analysis:
Time Complexity: O(n*log(n)).
Auxiliary Space: O(1).
*/
// Runtime: 559 ms, faster than 36.59% of Dart online submissions for Happy Number.
// Memory Usage: 140.6 MB, less than 95.12% of Dart online submissions for Happy Number.
bool isHappy(int n) {
int slow, fast;
// initialize slow and fast by n
slow = fast = n;
do {
// move slow number
// by one iteration
slow = numSquareSum(slow);
// move fast number
// by two iteration
fast = numSquareSum(numSquareSum(fast));
} while (slow != fast);
return (slow == 1);
}
// Utility method to return sum of square of
// digit of n
int numSquareSum(int n) {
int sumSquare = 0;
while (n != 0) {
sumSquare += (n % 10) * (n % 10);
n ~/= 10;
}
return sumSquare;
}
}
class B {
/*
Time Complexity: O(n*log(n)).
Auxiliary Space: O(n) since using extra set for storage
*/
// Runtime: 536 ms, faster than 53.66% of Dart online submissions for Happy Number.
// Memory Usage: 143.7 MB, less than 29.27% of Dart online submissions for Happy Number.
bool isHappy(int n) {
HashSet hs = HashSet();
while (true) {
n = numSquareSum(n);
if (n == 1) return true;
if (hs.contains(n)) return false;
hs.add(n);
}
}
int numSquareSum(int n) {
int sumSquare = 0;
while (n != 0) {
sumSquare += (n % 10) * (n % 10);
n ~/= 10;
}
return sumSquare;
}
}
class C {
// Runtime: 484 ms, faster than 78.05% of Dart online submissions for Happy Number.
// Memory Usage: 140.6 MB, less than 95.12% of Dart online submissions for Happy Number.
bool isHappy(int n) {
if (n == 1 || n == 7) return true;
int sum = n, x = n;
// this loop executes till the sum of square of
// digits obtained is not a single digit number
while (sum > 9) {
sum = 0;
// this loop finds the sum of square of digits
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;
}
}
class D {
// Runtime: 261 ms, faster than 100.00% of Dart online submissions for Happy Number.
// Memory Usage: 139.3 MB, less than 100.00% of Dart online submissions for Happy Number.
/// > It takes a number, adds the squares of its digits, and returns the result
///
/// Args:
/// n (int): The number to be checked.
///
/// Returns:
/// The sum of the squares of the digits of the number.
int solve(int n) {
int sum = 0;
for (int i = 0; n != 0; i++) {
int r = n % 10;
sum = sum + r * r;
n = n ~/ 10;
}
return sum;
}
bool isHappy(int n) {
int i = 10;
/// A loop that checks if the number is happy.
while (i > 0) {
int happy = solve(n);
if (happy == 1) {
return true;
}
n = happy;
i--;
}
return false;
}
}
class E {
// Runtime: 309 ms, faster than 100.00% of Dart online submissions for Happy Number.
// Memory Usage: 158 MB, less than 7.32% of Dart online submissions for Happy Number.
int sums(int n) {
int ans = 0;
while (n != 0) {
//ans += pow(n % 10, 2);
ans += pow(n % 10, 2).floor();
n = n ~/ 10;
}
return ans;
}
bool isHappy(int n) {
int s = sums(n);
int f = sums(sums(n));
if (f == 1) return true; //for case n==1
while (s != f) {
if (f == 1) return true;
s = sums(s);
f = sums(sums(f));
}
return false;
}
}