-
-
Save jescalan/f449899caf442ad233eba2df80009bea to your computer and use it in GitHub Desktop.
Gist from mistakes.io
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
/** | |
* @param {number} n - integer (n >= 0) | |
* @param {number} d - digit (0 <= d <= 9) | |
* square all numbers k between 0 and n (0 <= k <= n) | |
* count the number of digits d used in the k*k's | |
* example | |
* | |
* n = 10 | |
* d = 1 | |
* the k*k's are 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 | |
* d of 1 appears in 1, 16, 81, 100 | |
* therefore, count is 4 | |
*/ | |
function numberDig (n, d) { | |
return Array.apply(null, { length: n }).map(function (_, i) { | |
return i * i | |
}).filter(function () { | |
return n.toString().includes(d) | |
}).length | |
} | |
numberDig(10, 1) // 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment