-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhappyNumber.js
67 lines (57 loc) · 1.49 KB
/
happyNumber.js
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
// https://leetcode.com/problems/happy-number/
// const helper = (num) => String(num)
// .split('')
// .map(numStr => parseInt(numStr)*parseInt(numStr))
// .reduce((total, curr) => total+curr,0)
// const helper = (num) => {
// if(!num) return 0;
// let sum = 0;
// let strNum = String(num).split('');
// for(let i=0;i<strNum.length;i++) {
// sum = sum + parseInt(strNum[i])*parseInt(strNum[i])
// }
// return sum;
// }
const helper = (num) => {
if(!num) return 0;
let sum = 0;
let remainingNum=num;
// let strNum = String(num).split('');
while(remainingNum) {
const current = remainingNum % 10;
remainingNum = Math.floor(remainingNum / 10);
sum = sum+current*current;
}
return sum;
}
const happyNumber =(num) => {
const seen = {};
let sum = num;
while(sum !==1 && !seen.hasOwnProperty(sum)) {
seen[sum] = 1;
sum = helper(sum);
}
return sum === 1
}
console.log(happyNumber(19))
// 2nd version
const sumSquare1 = (num) => {
let total = 0;
while(num !== 0) {
let lastDigit = num%10;
num = Math.floor(num/10);
total += Math.pow(lastDigit, 2);
}
return total;
}
const happyNumber1 = (n) => {
let fast = n;
let slow = n;
while(true) {
fast = sumSquare1(sumSquare1(fast));
slow = sumSquare1(slow);
if(fast === slow) {
return fast === 1
}
}
}