Skip to content

Commit

Permalink
Adding new challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
hevertoncastro committed Apr 7, 2020
1 parent 99715d6 commit 5d3ad0d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

| Exercise | Code | PDF |
|----------|------|-----|
| [Hash Tables: Ransom Note](https://www.hackerrank.com/challenges/ctci-ransom-note/problem) | [Code](challenges/ctci-ransom-note/code.js) | [PDF](ctci-ransom-note/text.pdf) |
| [Hash Tables: Ransom Note](https://www.hackerrank.com/challenges/ctci-ransom-note/problem) | [Code](challenges/ctci-ransom-note/code.js) | [PDF](challenges/ctci-ransom-note/text.pdf) |
| [Lucky Numbers](https://www.hackerrank.com/challenges/lucky-numbers/problem) | [Code](challenges/lucky-numbers/code.js) | [PDF](challenges/lucky-numbers/text.pdf) |
44 changes: 44 additions & 0 deletions challenges/lucky-numbers/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function luckyNumbers(a, b) {
let totalLuckyNumbers = 0;
for (let i = a; i <= b; i++) {
const list = numberDigitsToArray(i);
const digitsSum = list.length > 1 ? getDigitsSum(list) : list;

if (!isPrime(digitsSum)) continue;

const squaresSum = list.length > 1 ? getSquaresSum(list) : list;

if (!isPrime(squaresSum)) continue;

totalLuckyNumbers += 1;
}
return totalLuckyNumbers;
}

function numberDigitsToArray(num) {
return Array.from(num.toString()).map(Number);
}

function getDigitsSum(digits) {
const totalDigits = digits.reduce((accumulator, digit) => accumulator + digit);
return totalDigits;
}

function getSquaresSum(squares) {
const totalSquares = squares.reduce((accumulator, digit) => {
return accumulator + digit ** 2
}, 0);
return totalSquares;
}

function isPrime(num) {
const sqrtnum = Math.floor(Math.sqrt(num));
let prime = num != 1;
for (let i = 2; i < sqrtnum + 1; i++) {
if (num % i == 0) {
prime = false;
break;
}
}
return prime;
}
Binary file added challenges/lucky-numbers/text.pdf
Binary file not shown.

0 comments on commit 5d3ad0d

Please sign in to comment.