Skip to content

Instantly share code, notes, and snippets.

@tkraak
Last active December 11, 2016 21:00
Show Gist options
  • Save tkraak/94be30647627f7dc31e4be43118d753b to your computer and use it in GitHub Desktop.
Save tkraak/94be30647627f7dc31e4be43118d753b 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) {
var squared = []
for (var k = 0; k <= n; k++) {
squared.push(k*k)
}
var count = squared.filter(containsOne)
function containsOne(n) {
return n.toString().includes(d)
}
return count.length
}
numberDig(10, 1) // 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment