add: 快乐数
This commit is contained in:
14
src/array/happy-number.js
Normal file
14
src/array/happy-number.js
Normal file
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* @param {number} n
|
||||
* @return {boolean}
|
||||
*/
|
||||
export const isHappy = function (n) {
|
||||
const set = new Set()
|
||||
|
||||
while (n !== 1 && !set.has(n)) {
|
||||
set.add(n)
|
||||
n = n.toString().split('').reduce((a, b) => a + b * b, 0)
|
||||
}
|
||||
|
||||
return n === 1
|
||||
}
|
Reference in New Issue
Block a user