Skip to content

Commit

Permalink
Created 202-Happy-Number
Browse files Browse the repository at this point in the history
  • Loading branch information
loczek committed Jul 8, 2022
1 parent 6aaade3 commit bc700fc
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions typescript/202-Happy-Number.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function isHappy(n: number): boolean {
const visit = new Set();

while (!visit.has(n)) {
visit.add(n);
n = sumOfSquares(n);

if (n == 1) return true;
}

return false;
}

function sumOfSquares(n: number): number {
let output = 0;

while (n) {
let digit = n % 10;
digit = digit ** 2;
output += digit;
n = Math.floor(n / 10);
}

return output;
}

0 comments on commit bc700fc

Please sign in to comment.