Created
April 16, 2013 16:50
-
-
Save yurydelendik/5397529 to your computer and use it in GitHub Desktop.
Calculate number of appearance of the specific digit if the int numbers from 1 to max
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script> | |
function CalcDigits(stdlib) { | |
"use asm"; | |
function howMany(n, digit) { | |
n = n|0; | |
digit = digit|0; | |
var amount = 0, m = 0; | |
while (n|0 > 0|0) { | |
m = (n|0) % (10|0); | |
if ((m - digit)|0 == 0|0) { | |
amount = (amount + 1)|0; | |
} | |
n = ((n|0) / (10|0))|0; | |
} | |
return amount|0; | |
} | |
function calc(max, digit) { | |
max = max|0; | |
digit = digit|0; | |
var i = 0, n = 0, m = 0; | |
var sum = 0; | |
for (i = 1; ((i - max)|0) <= 0|0; i = (i + 1)|0) { | |
sum = (sum + howMany(i, digit))|0; | |
} | |
return sum|0; | |
} | |
return { calc: calc }; | |
} | |
</script> | |
</head> | |
<body> | |
<script> | |
var module = CalcDigits(window); | |
console.time('calc'); | |
var result = module.calc(1000000, 3) | |
console.timeEnd('calc'); | |
alert(result); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment