Skip to main content
4 of 8
added 8 characters in body
Chau Giang
  • 1.3k
  • 1
  • 11
  • 16

How many 1's we get

Description

Count how many occurrence of number 1 between two numbers \$[a, b]\$, we need to include both a and b

For example from 1 to 100 it should be 21:

1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 31, 41, 51, 61, 71, 81, 91, 100

The number 1 is repeated 20 times.

Rules

  1. Each number in the input list is guaranteed is an integer in the range \$0 \leq a \leq b < 2^{32}\$.
  2. The shortest answer in bytes wins.

Test cases

[1,100] -> 21
[11,200] -> 138
[123,678] -> 182

Other

Here is my code using bash

eval echo {$1..$2}|grep -o 1|wc -l
Chau Giang
  • 1.3k
  • 1
  • 11
  • 16