[Question]
  [
## Description
Count how many occurrences there are of the digit **1** between two given numbers \$[a, b]\$, inclusive.
For example, from 1 to 100 it should be 21:
**1**, **1**0, **11**, **1**2, **1**3, **1**4, **1**5, **1**6, **1**7, **1**8, **1**9, 2**1**, 3**1**, 4**1**, 5**1**, 6**1**, 7**1**, 8**1**, 9**1**, **1**00
The number **1** is repeated 21 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
```
## Example
Here is my code using bash
```
eval echo {$1..$2}|grep -o 1|wc -l
```
  
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), ~~5~~ 4 bytes
Thanks a lot to OP! Now I can save a byte
```
≈∏SŒòO
```
[Try it online!](https://tio.run/##yy9OTMpM/f//6I7gczP8//@PNtQxNDCIBQA "05AB1E ‚Äì Try It Online")
## Explanation
```
≈∏    Inclusive range
 S   Split the string into individual chars
  Œò  (Vectorizes) Does this character == "1"?
   O Sum the resulting list
```
[Answer]
# [Python 2](https://docs.python.org/2/), ~~39~~ 36 bytes
-3 bytes thanks to @SurculoseSputum
```
lambda a,b:`range(a,b+1)`.count('1')
```
[Try it online!](https://tio.run/##NY7RqsIwDIbv@xRBGGs4cdgKOoSdJzkXduq0sLWlrRc@/Uw7Ti5C/vD9fxI@@eWdXicY4G@dzTLeDRgaL9do3PMhefxReO1u/u2ybFWLq12CjxnSJwkx@QizdQ@wriy6lO/WXQRwWRcI/DtzcCG6mHK0QWKXwmw5Cva/0GJFDcHI2GKCtC5Tsf5T1CKKCj19iZpkgTdbbSGyRe6aBMMA3GWTcAcNSObrfdqcAxSONSKuitThUB7QSihFehPq2Aulj3Q691X2@gs "Python 2 ‚Äì Try It Online")
# [Python 3](https://docs.python.org/3/), ~~42~~ 40 bytes
-2 bytes thanks to @JoKing
```
lambda a,b:f"{*range(a,b),b}".count('1')
```
[Try it online!](https://tio.run/##NY7BisMwDETv/goRCLGKGuoEdkMh/ZK9ONuma0hsY7uHUvrtqeywOgyMeDOSf6Y/Z/tthhF@tkWv01WDpuk8V69D0PZ@k@yQpnfV/rqHTbJRDW5m9S4kiM8oxOwCLMbewNi8aGO6GnsWwGOsJ3CPxOWZaENMwXiJbfSL4So4XqDBgmqCibFVe2lsohz9p6hBFAW6u1w1ywzvsSI@cERWdYRxBFZZR6ygBsl8uU97coTMsUfETZE6nfIDnRJKUbcb1Q9CdT19fQ/FDt0H "Python 3 ‚Äì Try It Online")
[Answer]
# [Rust](https://www.rust-lang.org/)+[itertools](https://crates.io/crates/itertools), 42 bytes
```
|a,b|(a..=b).join("").matches('1').count()
```
[Try it in the Rust Playground!](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=ddd5ae6f71aee1a0d4392a066410fe24)
[Answer]
# [APL (Dyalog Extended)](https://github.com/abrudz/dyalog-apl-extended), 9 bytes
```
+/'1'=‚çï‚ç§‚Ä¶
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn/X1tf3VDd9lHv1Ee9Sx41LPuf9qhtwqPevkddzY961zzq3XJovfGjtomP@qYGBzkDyRAPz@D/hgppCoYGBlyGIIYRiGFkDGSZmVsAAA "APL (Dyalog Extended) ‚Äì Try It Online")
`+/`‚ÄÉsum
`'1'=`‚ÄÉwhere the character is equal to
`‚çï`‚ÄÉthe string representation
`‚ç§`‚ÄÉof
`‚Ä¶`‚ÄÉthe range
[Answer]
# [Octave](https://www.gnu.org/software/octave/), ~~29~~ ~~28~~ 27 bytes
```
@(x,y)sum(mat2str(x:y)==49)
```
[Try it online!](https://tio.run/##y08uSSxL/Z9mq6en999Bo0KnUrO4NFcjN7HEqLikSKPCqlLT1tbEUvN/moahjqGBgSYXkGGoYwRlGRnrmJlbaP4HAA "Octave ‚Äì Try It Online")
[Answer]
# Java 8, ~~71~~ ~~67~~ 66 bytes
```
a->b->{var s="";for(;b>=a;)s+=b--;return~-s.split("1",-1).length;}
```
-4 bytes thanks to *@OlivierGr√©goire*.
[Try it online.](https://tio.run/##jY8xb4MwEIX3/IqTJyxsK06ltooLY6UOnTIiBkOAmjoG4SMlQvSvUyeNOnf77t7du3etPmveHj/X0mrv4V0bN28APGo0JbRBFSMaK@rRlWg6J17v8PLmsGqqgf1v6A5pCjUkq@ZpwdP5rAfwCSGq7oZIFWmiFfVxUnCuhgrHwX1zL3xvDUZEEsYlFbZyDX6oZVWbELMfCxti3tOeO3OEU/ggOuBgXJPloOn1G4DrAeMwdCbYg6u@4FZl@TxLJrfbhc1Sst0v7B7Y49PzstDbKsDh4rE6iW5E0QdftC6asm0eE0biKZMB9kDiWui@t5ebRP9Y5pSq4LNslvUH)
**Explanation:**
```
a->b->{                // Method with two integer inputs and integer return-type
  var s="";            //  String `s`, starting empty
  for(;b>=a;)          //  Loop `b` downwards in the range [`b`, `a`]:
    s+=b--;            //   And append `b` to to String `s`
  return~-s.split("1", //  Split String `s` on "1",
                  -1)  //  and keep empty trailing items
           .length;    //  Then get the amount of parts of this array
                       //  And decrease it by 1 with `~-`, before turning it as result
```
[Answer]
# [R](https://www.r-project.org/), 43 bytes
```
function(x,y)sum(unlist(gregexpr(1,x:y))>0)
```
[Try it online!](https://tio.run/##K/qfnF@aVxKfn5dabPs/rTQvuSQzP0@jQqdSs7g0V6M0LyezuEQjvSg1PbWioEjDUKfCqlJT085AE0kfUNTQwECTC1nEUMcIXcjIWMfM3ELzPwA "R ‚Äì Try It Online")
[Answer]
# [Ruby](https://www.ruby-lang.org/), 62 46 28 bytes
```
->c,d{[*c..d].join.count ?1}
```
-18 bytes, courtesy of [Dingus.](https://codegolf.stackexchange.com/users/92901/dingus)
[Try it online!](https://tio.run/##KypNqvyfZvtf1y5ZJ6U6WitZTy8lVi8rPzNPLzm/NK9Ewd6w9n@BQlq0oY6hgUEsF5hpqGMEZxsZ65iZW8T@BwA "Ruby ‚Äì Try It Online")
[Answer]
# JavaScript (ES6), 44 bytes
Expects `(a)(b)`.
```
a=>g=b=>b<a?0:(b+g).split(1).length-3+g(b-1)
```
[Try it online!](https://tio.run/##XclBDsIgEADAu6/guJsGykKipJH6FqgUawg0Qvw@etBL5zpP93Z1eW1747ncQ19td3aO1tvZX91NTuCHiKLuaWtAKFLIsT24HiJ4TtiXkmtJQaQSYQXGCIGkRGTjyBSdDk3fVv8mbQ5PSiOcL@b3RvUP "JavaScript (Node.js) ‚Äì Try It Online")
### How?
We use a recursive function `g` to count how many `1`'s we have in `b` and decrement `b` until it's lower than `a`.
In order to count the `1`'s, we have to coerce `b` to a string. We could do `b+''` but it's shorter to use `b+g`. Because the source code of `g` itself contains two `1`'s, we subtract `3` instead of just `1` from the result of `(b+g).split(1).length`.
[Answer]
# [Retina 0.8.2](https://github.com/m-ender/retina/wiki/The-Language/a950ad7d925ec9316e3e2fb2cf5d49fd15d23e3d), ~~36~~ 32 bytes
```
\d+
$*_
(?<=(_+) _*)(?=\1)
$.'
1
```
[Try it online!](https://tio.run/##K0otycxL/K@q4Z7wPyZFm0tFK55Lw97GViNeW1MhXktTw942xlCTS0VPncvw/39DBUMDAy5DQwUjEGVkrGBmbgEA "Retina 0.8.2 ‚Äì Try It Online") Link includes test cases. Explanation:
```
\d+
$*_
```
Convert `a` and `b` to unary, but use `_` instead of `1` to avoid confusion. (In Retina 1 this would just be `*`, saving 2 bytes.)
```
(?<=(_+) _*)(?=\1)
$.'
```
At each boundary in `b` up to and including `a` from the end, insert the distance to the end in decimal, thus generating the range from `b` down to `a`.
```
1
```
Count the resulting number of `1`s.
[Answer]
# Google Sheets, 68
```
=SUM(ArrayFormula(LEN(REGEXREPLACE(""&SEQUENCE(A2-A1+1,1,A1),"[^1]",
```
Sheets auto-closes parens.
This is super slow at large ranges, but as this is Code Golf, we're here to optimize character count. My first attempt was to use `JOIN(SEQUENCE(...))` to make one long string then count the 1's, but as it turns out, Sheets has a limit of 50000 characters, so that didn't work.
[Answer]
# [Kotlin](https://kotlinlang.org), 49 bytes
```
{x:Int,y:Int->(x..y).sumBy{"$it".count{it=='1'}}}
```
[Try it online!](https://tio.run/##TcnBCoMgGADgu0/xE4MUytJgi4EddttjxECQ2V9sOhLx2V2jHbp8l@85O2swa48wjQYpg0g@owUNKsf1ekdXhZ/1QFfOA@NvP91CLE7GFfwxe3TROKVKUaaU8vIy6CxSTUUFom0ZA4CmgXoAKchht5Z77yu6/tiyq@B86bf/dy9JyvkL "Kotlin ‚Äì Try It Online")
[Answer]
# [Haskell](https://www.haskell.org/), 26 bytes
```
a!b=sum[1|'1'<-show[a..b]]
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/P1Exyba4NDfasEbdUN1Gtzgjvzw6UU8vKTb2f25iZp6CrUJKvkJBUWZeiYahoqGBgaY1jGeoaITMNTJWNDO30PwPAA "Haskell ‚Äì Try It Online")
[Answer]
# [MathGolf](https://github.com/maxbergmark/mathgolf/blob/master/math_golf.txt), 8 [bytes](https://github.com/maxbergmark/mathgolf/blob/master/code_page.py)
```
‚Ü®√¶‚ñím‚î¥Œ£]Œ£
```
[Try it online.](https://tio.run/##y00syUjPz0n7//9R24rDyx5Nm5T7aMqWc4tjzy3@/99Qx9DAgMvQUMcIRBkZ65iZWwAA)
**Explanation:**
```
‚Ü®         # Loop in the range [a,b] using the two implicit inputs a,b,
 √¶        # and execute the following four commands:
  ‚ñí       #  Convert the integer to a list of digits
   m      #  Map over each digit:
    ‚î¥     #   And check which are equal to 1 (1 if 1; 0 otherwise)
     Œ£    #  Get the sum of those checks
      ]   # After the loop, wrap all values on the stack into a list
       Œ£  # And sum this list
          # (after which the entire stack joined together is output implicitly as result)
```
[Answer]
# [Perl 5](https://www.perl.org/) `-p`, 21 bytes
```
map$\+=y/1//,$_..<>}{
```
[Try it online!](https://tio.run/##K0gtyjH9/z83sUAlRtu2Ut9QX19HJV5Pz8autvr/f0NDLiMDg3/5BSWZ@XnF/3ULAA "Perl 5 ‚Äì Try It Online")
[Answer]
# [PowerShell](https://github.com/TryItOnline/TioSetup/wiki/Powershell), 40 bytes
```
$a,$b=$args
($a..$b|sls 1 -a|% m*).Count
```
[Try it online!](https://tio.run/##K8gvTy0qzkjNyfn/XyVRRyXJViWxKL2YS0MlUU9PJammOKdYwVBBN7FGVSFXS1PPOb80r@T///@G/w0NDAA "PowerShell ‚Äì Try It Online")
---
# [PowerShell](https://github.com/TryItOnline/TioSetup/wiki/Powershell), 40 bytes
```
($args-join'..'|iex|sls 1 -a|% m*).Count
```
[Try it online!](https://tio.run/##K8gvTy0qzkjNyfn/X0MlsSi9WDcrPzNPXU9PvSYztaKmOKdYwVBBN7FGVSFXS1PPOb80r@T///@G/w0NDAA "PowerShell ‚Äì Try It Online")
[Answer]
# [PHP](https://php.net/), ~~46~~ 45 bytes
```
fn($a,$b)=>substr_count(join(range($a,$b)),1)
```
[Try it online!](https://tio.run/##TY1PC4IwGMbv@xQvsoODiW5CCaadgg5B3lViiss6OHHzFH329RJC3R5@z795nP3hOI8zIVQXXk8hVZx2rCjt2lm33HqzTi58mscULmq6D5vPuGA@x5IbrLNQQE1qwUEkScshjiEqQQpEyOQfE2mGUKYcdvvsRzNJWhzTZhlUP4awrSqLChi8CECNv0C7Fq@oyxEM/WggoCqiXeOisnEB@hq73xwwDtW5up2ul5y8/Qc "PHP ‚Äì Try It Online")
[Answer]
# [C (gcc)](https://gcc.gnu.org/), 58 bytes
```
i;c;f(a,b){for(c=0;b/a;)for(i=a++;i;i/=10)c+=i%10==1;a=c;}
```
[Try it online!](https://tio.run/##bVHBboMwDL33KywkpKSkIoC6MWXZZdpXFDSFELYcRivggIb4dmYgdK3USHZiv@fnONGHL62nyQotKqJYQYfq3BAtuShCJegcWKmCQFhhQxlxqgNp/YhLGQkltRgnW3fwo2xN6G7YAa450Zm2@1SnHCQMEYNotjgZxT2hcATOGcSze3pObzimv5i@M@XCgiGeRZIUXRo7lv5WzR6aVcbL@o8461/e0Y4eg9s48bBiKcGJgMzqti5Nj3VcuOMrtPbXnCuy9aWhS@yvGQFBsLAprMNud1Wo5IZe8FzcwcUGFw9hg/B12keEFgnr//znLw0iFfFOfsnAL3M4vOEGxC8p@G1W4xMoBgWDloFh@Eoogm1yJzHuxukP "C (gcc) ‚Äì Try It Online")
[Answer]
# [Factor](https://factorcode.org/), 65 bytes
```
: f ( a b -- n ) [a,b] [ number>string [ 49 = ] count ] map sum ;
```
[Try it online!](https://tio.run/##Nc09D4IwGATgnV9xoyaWUDR@QHRVFhbjRBgKeUGjLdiPEH59rTFuzw1314nWDtrfrkV5zvAkregFKew9HoU2pH/WQvVkYOjtSLVBoyZr51E/lEUeFWWGyzAxKdTMuGETsZ6sz9BhAYEGjEFhiUqsmhoVlJMN6ZOxod@HvDngiBrt4MJcHS5HGCeRew6eJGEljjhH@me6xna3/9p/AA "Factor ‚Äì Try It Online")
[Answer]
# [K (oK)](https://github.com/JohnEarnest/ok), ~~19~~ 17 bytes
**Solution:**
```
{+/"1"=,/$x_!1+y}
```
[Try it online!](https://tio.run/##y9bNz/6fZlWtra9kqGSro69SEa9oqF1Z@z9NT13DUMHQwMDa0FDBCEQZGSuYmVto/v8PAA "K (oK) ‚Äì Try It Online")
**Explanation:**
Range generation is inefficient (i.e. generate range 0..Y and then drop from front, rather than generating range X..Y) but saves 2 bytes.
```
{+/"1"=,/$x_!1+y} / the solution
{               } / lambda taking implicit x & y args
             1+y  / add 1 to y
            !     / range 0..N
          x_      / drop (_) x items from front
         $        / convert to string
       ,/         / flatten 
   "1"=           / is string equal to "1"?
 +/               / sum
```
[Answer]
# JavaScript (ES6), ~~63~~ 62 bytes
```
a=>b=>([...Array(b-a+1)].map((_,i)=>i+a)+'').split(1).length-1
```
[Try it online](https://tio.run/##XcmxDoIwEADQ3a/oxl2QgyuJsrSJ32GMORCwplJSiIlfXx104a3vIS9ZuujmtZjCrU@DSWJsayyciegUo7yhLSRnvNBTZoDr3qGxLhfMswxpmb1bgZF8P43rveDUhWkJvicfRhhAKUbgqkJUZak07zbN39b/5rrZPOsa4XBsft/o9AE)
```
[...Array(b-a+1)] // an array of length b-a+1
map((_,i)=>i+a)   // fill it with numbers from a to b
+''               // convert it to a string with each number separated by a comma
.split(1)         // split at each 1
.length-1         // count the chunks and subtract 1
```
-1 byte thanks to @Jo King
[Answer]
# [J](http://jsoftware.com/), ~~19~~ 16 bytes
-3 bytes thanks to Jonah!
```
1#.1=/&":[,-.&i.
```
[Try it online!](https://tio.run/##y/qvpKeepmBrpaCuoKNgoGAFxLp6Cs5BPm7/DZX1DG311ZSsonV09dQy9f5rcilwpSZn5CsoKBgaGChoWFunaSoYQoWMECJQITNzC5iIkTFXakVmibo616MpPY@mNGBDa2CMCUBFTTB1a5BUTACKGxkCCYiCJhAT6BAIR0EBLjEFrHoLks4ZSAqm4LAfrn4GHkeiuwjZqVhca2hsgeRckHuNEO5FdzBWNxNwNLoe/E4n4HpsHrAwQvKAkTGQBEYrHh@Q6Ql0bf8B "J ‚Äì Try It Online")
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 5 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
The spec contradicts itself as to whether the range should be inclusive or not. If it shouldn't then replace `√µ` with `o`.
```
√µ ¬¨√®1
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=9SCs6DE&input=MTAw)
[Answer]
# [Rust](https://www.rust-lang.org/), 63 bytes
```
|a,b|(a..=b).map(|x|format!("{}",x).matches('1').count()).sum()
```
[Try it online!](https://tio.run/##XY29DoIwFIVneYrCwr1JbagmavxhZPMhCoIytBh6mxApz16BwcHlnOTLyXd6Zyk0hmnVGkA2RhuqLUHwipcelBC3EoVWb/CDb7peK4ohGaeEDwum6lVbSGWKouqcIUAU1mnAgJdoimbvYrsWZ1aYuyNwtv3UfE3c5mvnoB2xZl6s74/yGUMDksssw1nyA5Lv/shuzw/H04Km8AU "Rust ‚Äì Try It Online")
[Answer]
# [MATL](https://github.com/lmendo/MATL), 7 bytes
```
&:V1V=s
```
[Try it online!](https://tio.run/##y00syfn/X80qzDDMtvj/f0MjYy4zcwsA "MATL ‚Äì Try It Online")
## Explanation
```
&:V1V=s
     =s  % Count occurrences
   1V    % of '1' in
  V      % string of
&:       % inclusive range of input
```
[Answer]
# [Java (OpenJDK 8)](http://openjdk.java.net/), 80 bytes
```
a->b->IntStream.range(a,b+1).flatMap(i->(""+i).chars()).filter(x->x==49).count()
```
[Try it online!](https://tio.run/##bY8xb8IwEIX3/IoTk12wRaUuFY3HSkgwZaw6XILjOnVsy74gUMVvTw1lK9Od3nf37t2ARxQhaj8cvmc7xpAIhqLJiayTT5vqn9ZPviMb/EOYKWkcr6jqHOYMe7QefiqAOLXOdpAJqZRjsAcYC2MNJevNxydgMpnfRgHe7zfetp600Wn1QNkFb5SCHmq47cwoVCtU4c1fiITeaIardvnMZe@Q9hiZFYotFkvLZfeFKTNekHWkEzsJdarrl9dCwuSJ8bk8cTVuzpn0KMNEMpas5DzrJcbozqwY35v1mvNNGb9Ul/kX "Java (OpenJDK 8) ‚Äì Try It Online")
Doesn't need much of an explanation, but here's one anyways:
```
Function<Integer, Function<Integer, Long>> f = 
    a -> b-> 
      IntStream
        .range(a,b+1) //Create an IntStream going from a to b
        .flatMap(i -> //Map every int i in that stream
          (""+i)      //Make it a string
          .chars()    //Turn that string to an IntStream
        )    //Flatten that
        .filter(x -> x == 49)  //Keep all the '1's
        .count();              //Find out how many '1's there are
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 5 bytes
```
rDFƒã1
```
[Try it online!](https://tio.run/##y0rNyan8/7/Ixe1It@H///8NDf8bGRgAAA "Jelly ‚Äì Try It Online")
**Explanation:**
```
rDFƒã1
r      range of the inputs
 DF    make decimal and flatten; list of all the digits in the range
   ƒã1  count occurences of 1
```
[Answer]
# [Pyth](https://github.com/isaacg1/pyth), ~~8~~ 7 bytes
-1 byte thanks to @FryAmTheEggman
```
/`}QE"1
```
[Try it online!](https://tio.run/##K6gsyfj/Xz@hNtBVyfD/f0MjYy4zc4t/@QUlmfl5xf91UwA "Pyth ‚Äì Try It Online")
## Explanation
```
/`}QE"1
  }QE     # Inclusive range on input
 `        # string of the range ([1, 2, 3] -> "[1, 2, 3]")
/    "1   # count ones
```
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 29 bytes
```
Tr@DigitCount[Range@##,10,1]&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n277P6TIwSUzPbPEOb80ryQ6KDEvPdVBWVnH0EDHMFbtf0BRZl6JgkN6tCFQxCCWC8E31DFCFTAy1jEzt4j9/x8A "Wolfram Language (Mathematica) ‚Äì Try It Online")
[Answer]
# [Dart](https://www.dartlang.org/), 56 bytes
```
f(a,b)=>'1'.allMatches([for(;a<=b;a++)a].join()).length;
```
[Try it online!](https://tio.run/##S0ksKvn/P00jUSdJ09ZO3VBdLzEnxzexJDkjtVgjOi2/SMM60cY2yTpRW1szMVYvKz8zT0NTUy8nNS@9JMP6f24iiK9QzaWgUFCUmVeikaZhqGNoYKCpaY0sZKhjhCFmZKxjZm4BEqz9DwA "Dart ‚Äì Try It Online")
]