Skip to content

Instantly share code, notes, and snippets.

@jescalan
Forked from tkraak/index.js
Last active Dec 11, 2016
Embed
What would you like to do?
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