0

I have tried to solve this algorithm in JavaScrip and I couldn't make it. I have done it in Java but I can't find a way to solve this is JS. If somebody can show me and explain to me the solution I would be grateful to the moon and back :) The algorithm:

Find the frequency of occurrence of a given digit in all whole numbers between 0 and a given positive integer (both inclusive).

Let's say that the number to count (we'll call it K) is 2.

So, between 0 and 35, the K (in this case 2) appears 14 times:

2, 12, 20, 21, 22 (twice), 23, 24, 25, 26, 27, 28, 29 32 = 14 times

Input

Input consists of 2 lines. First line contains the number (K) which the program needs to count. Second line contains the number N, which is the maximum number in the range to check for occurrences of K (for example, between 0 and N). All number are positive whole numbers. There are no decimals or fractions.

Output

Print the number of K’s appearing between 0 and N (both inclusive). Print 0 if not found.

Thank you in advance.

Nemanja15
  • 3
  • 2
  • Well how did you do it in Java? – Pointy Sep 14 '20 at 20:13
  • [link](https://www.geeksforgeeks.org/number-of-occurrences-of-2-as-a-digit-in-numbers-from-0-to-n/?ref=lbp) Here is the solution, I found it here, but when I try to "convert" that into JS I can't get the same output. – Nemanja15 Sep 14 '20 at 20:30

1 Answers1

0

Possible solution, might be more efficient onces.

let k = String(2);
let n = 35;
let s = String("");

// create string from numbers 0 to n
for (let i = 0; i <= n; i++) s += i;

// check each character for digit
let occurs = s.split('').reduce((a, x) => x === k ? a + 1 : a, 0);

// print result
console.log(occurs);
Johan
  • 68
  • 2
  • 1
  • 1
    There's a much faster solution. For any digit, the number of times it appears in written-out base-10 numbers from 0 to 10^p is p * 10^(p - 1). From that it's possible to compute the answer in a small fraction of the iterations necessary for a simple "count the digits" solution. – Pointy Sep 14 '20 at 20:37
  • I see, thank you for the explanation. I'm on my way back into programming and relearning a lot of stuff. :-) – Johan Sep 14 '20 at 20:41
  • That solution isn't what I'd call "easy" but it's not terribly hard, and it's massively faster for large `n`. – Pointy Sep 14 '20 at 20:43
  • @pointy "0 to 10^p - 1" i.e. don't include the 10^p, otherwise this miscounts 0s and 1s. – Dave Sep 14 '20 at 20:43
  • @Dave yes that's the tricky part of the algorithm :) – Pointy Sep 14 '20 at 20:44
  • The "naive" approach can't really handle numbers bigger than 100000 in a reasonable amount of time. The "good" approach, tricky though it may be, can handle values up to `Number.MAX_SAFE_INTEGER` almost instantaneously. – Pointy Sep 14 '20 at 20:45
  • What is p in the question –  Sep 14 '20 at 20:46
  • @StackGeek `p` is the number of digits in `N`. But the formula is only valid when all of the digits in `N` are 9. So `N` has to be 9, 99, 999, 9999, etc. For example, there are 300 5's in the numbers from 0 to 999. – user3386109 Sep 14 '20 at 21:06
  • Great, thanks guys! I really appreciate. Do you have any possible solution on how to not hardcode the variables N and K, and to put them into a function as a parameters so I can assign them a value when I call that function? – Nemanja15 Sep 14 '20 at 21:45