Skip to content

Instantly share code, notes, and snippets.

@jescalan
Forked from tkraak/index.js
Last active December 11, 2016 21:02
Show Gist options
  • Save jescalan/f449899caf442ad233eba2df80009bea to your computer and use it in GitHub Desktop.
Save jescalan/f449899caf442ad233eba2df80009bea to your computer and use it in GitHub Desktop.
Gist from mistakes.io
/**
* @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