-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
99715d6
commit 5d3ad0d
Showing
3 changed files
with
46 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.