Skip to content

Added Happy Number #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions LeetcodeProblems/Algorithms/Happy_Number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
Happy Number
https://leetcode.com/problems/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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you update this using a ^? The digits together are a bit confusing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure


Example 2:
Input: n = 2
Output: false


*/

/**
* @param {number} n
* @return {boolean}
*/
var isHappy = function(n) {
return checkHappyNumber(n);
};

function checkHappyNumber(n){
strNumber = n.toString();
splitNumber = strNumber.split("");
Comment on lines +40 to +41
Copy link
Owner

@ignacio-chiazzo ignacio-chiazzo Oct 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another solution that doesn't require translating the integer to a String would be to
get the number, apply mod 10 and divide it by 10, get the digit and repeat the process.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, i don't get it. could you give an example?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

last_digit = n % 10 
n = (n - last_digit) / 10

if(splitNumber.length <= 1){
return (n <= 1)? true:false;
}
const digit = splitNumber.reduce((a,b)=> parseInt(a) + Math.pow(parseInt(b),2),0);
return checkHappyNumber(digit)
}

module.exports.isHappy = isHappy;
9 changes: 9 additions & 0 deletions LeetcodeProblemsTests/Algorithms/Happy_Number_Test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const assert = require('assert');
const isHappy = require('../../LeetcodeProblems/Algorithms/Happy_Number').isHappy;

var test = function () {
assert.equal(isHappy(19),true);
assert.equal(isHappy(2),false);
}

module.exports.test = test;
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ To run a specific problem in your console run `node <problem_file_path>` (e.g.
| [Permutations With Duplicates ](/LeetcodeProblems/Algorithms/Permutations_With_Duplicates.js) | | |
| [Deletion Distance](/LeetcodeProblems/Algorithms/Deletion_Distance.js) | | |
| [Award Budget Cuts](/LeetcodeProblems/Algorithms/Award_Budget_Cuts.js) | | |
| [Happy Number](https://leetcode.com/problems/happy-number/) | Easy | https://leetcode.com/problems/happy-number/ |

### Sorting Algorithms
| Algoritmhs |
Expand Down