# Find the number of integers in the range from 1 to N that ends with 2

As input you have:

• a positive integer N

And you should output:

• The number of integers in $$\[1,N]\$$ (an inclusive range) which end with the digit $$\2\$$ in base ten.

# Test cases

1 -> 0
2 -> 1
5 -> 1
10 -> 1
12 -> 2
20 -> 2
30 -> 3
54 -> 6
97 -> 10
100 -> 10


# Rules

It is a code-golf so the lowest score in bytes wins!

# Python 2, 17 bytes

lambda n:(n+8)/10


Try it online! Uses Python 2's integer division. In Python 3 would be a byte longer with lambda n:(n+8)//10.

• How'd you come up with this? – S.S. Anne Mar 2 at 12:26
• @S.S.Anne I noticed the result increases by one every 10 integers, so integer division by 10 was necessary. Then it was just a matter of finding the correct offset so that f(1)=0, f(2)=1 – RGS Mar 2 at 13:13
• My first crack at it was lambda n:n/10+(n%10>1) – Cruncher Mar 2 at 21:13
• @Cruncher still a really nice try :D – RGS Mar 2 at 22:02
• Well done. My usual code golf experience is to formulate my answer and then look what the geniuses of code-golf-mastery have come up with and then stare aghast at my overall ineptitude. But today I got the same answer. It's the small wins in life. – Belly Buster Mar 3 at 13:41

# Jelly, 4 bytes

Ḋm⁵L


A monadic Link accepting a positive integer, N, which yields a non-negative integer.

Try it online!

### How?

Every tenth number starting with $$\2\$$ ends with the digit $$\2\$$...

Ḋm⁵L - Link: integer, N                           e.g. 15
Ḋ    - dequeue (implicit range [1..N]) -> [2..N]       [2,3,4,5,6,7,8,9,10,11,12,13,14,15]
⁵  - literal ten                                     10
m   - modulo slice                                    [2,12]
L - length                                          2


+8:⁵


Add eight, integer divide by ten (as first used in RGS's Python answer I believe).

• Interesting method. Looks like the "add 8 then int-divide by 10" that everyone is doing is the same length: +8:⁵(TIO) – xnor Mar 1 at 17:42
• "as first used in RGS's Python answer I believe" :') also, I should really learn my Jelly atoms... 4 bytes is a really nice length :D – RGS Mar 1 at 19:13

# Perl 5-pl, 13 10 bytes

@Grimmy got it back down to 10 bytes, with correct output.

$_+=1<chop  Try it online! • doesn't return 0 for 1 – Nahuel Fouilleul Mar 2 at 16:10 • Isn't an empty string a form of 0? :) I've fixed it. – Xcali Mar 2 at 16:22 • 10 bytes: $_+=1<chop. Properly outputs 0 for input 1 (relevant meta). – Grimmy Mar 2 at 16:28
• @Grimmy nice fix, i had 11 bytes but using -Minteger – Nahuel Fouilleul Mar 2 at 16:49
• This requires that the input has no trailing newline? Like the one you'd get if you run perl -pe '$_+=1<chop' and input a number..? – ilkkachu Mar 3 at 19:07 # APL (Dyalog Unicode), 8 7 bytes -1 byte thanks to Bubbler Full program ⌊.1×8+⎕  Try it online! • -1 byte using .1×. – Bubbler Mar 2 at 4:45 # Haskell, 14 Bytes f x=div(x+8)10 • you can replace floor and / with div. – ovs Mar 1 at 20:16 • The point-free version (div10).(+8) scores the same. – Jonathan Frech Mar 1 at 23:55 # W, 4 bytes 8+T/  Just another boring formula: Add eight, divide by 10. (W performs integer division if both operands are integers.) # Wd, 5 bytes [ⁿNy|  Uncompressed: Tm2=Wk  ## Explanation  W % For every number in the range [1 .. N]: % Keep all that satisfies: Tm % After modulo by 10, 2= % The result is equal to 2 k% Find the length of that  • 2= is a really frequent operation, I need to make a built-in for that. :D Wk = K too... – Λ̸̸ Mar 2 at 1:10 • Unless that divison in the 4 byter is integer division, then that's an invalid answer, as most n results in a decimal answer. – Lyxal Mar 2 at 1:52 • That's a clever idea. I'm pretty sure that's how other languages implement division like that as well. Disregard my comment then if that is indeed the case. – Lyxal Mar 2 at 1:57 # 05AB1E, 4 bytes 8+T÷  Same approach as everyone else. Some (slightly) more interesting 5-bytes alternatives: LT%2¢ L€θ2¢ L2Å¿O FNθΘO  Try each online. Explanation: 8+ # Add 8 to the (implicit) input-integer T÷ # Integer-divide it by 10 # (after which the result is output implicitly) L # Push a list in the range [1, (implicit) input-integer] T% # Take modulo-10 on each # or €θ # Leave the last digit of each 2¢ # Count the amount of 2s # (after which the result is output implicitly) L # Push a list in the range [1, (implicit) input-integer] 2Å¿ # Check for each whether it ends with a 2 (I'm actually surprised it vectorizes..) O # Sum to get the amount of truthy values F # Loop N in the range [0, (implicit) input-integer): N # Push N θ # Pop and leave only its last digit Θ # 05AB1E trutify: check if it's exactly 1 O # Sum all values on the stack together # (after the loop, the result is output implicitly)  • I like the fact that you are "trutifying" in your last explanation. Almost like "making look like a trout"! – RGS Mar 3 at 23:11 # Retina 0.8.2, 15 13 bytes .+$*
.{2,10}


Try it online! Edit: Saved 2 bytes thanks to @Grimmy. Explanation:

.+
$*  Convert to unary. .{2,10}  Count the number of multiples of 10, each of which contains an integer that ends in 2 in base 10, plus count an additional match for a final 2-9, as that's enough for one last integer that ends in 2 in base 10. # Jelly, 76 5 bytes R%⁵ċ2  Try it online! Thanks to Nick Kennedy for saving me one byte. How it works: R Range from 1 to n, %⁵ modulo 10. ċ2 Then count how many of those are 2.  • R%⁵ċ2 saves 1 – Nick Kennedy Mar 1 at 21:08 # Powershell, 50 20 bytes 1.."$args"-match"2$" $args are arguments to pass as number.

• I think that means you can omit the 1..50 and then your score would be 10 or 11. – S.S. Anne Mar 2 at 12:32
• @S.SAnne how to omit that? Please help I am new to code golfing. – Wasif Hasan Mar 2 at 16:44
• $args is an array of arguments Try it online!. See docs – mazzy Mar 2 at 16:54 • Thanks I knew it but didn't thought I can use it here. – Wasif Hasan Mar 2 at 17:02 • Your objective is to make your byte count lower. Remove 1.."$args"- and then put something in the header to make it work. – S.S. Anne Mar 2 at 17:11

# APL+WIN, 9 bytes

Prompts for integer n:

+/2=10|⍳⎕

Try it online! Courtesy of Dyalog Classic

• This only works if ⎕IO is 1. Does that need to be a part of the program? – mappo Mar 5 at 13:28
• @mappo The default ⎕IO setting of my APL+WIN is 1. Similarly it seems that Dyalog Classic in TIO defaults to ⎕IO = 1. Generally ⎕IO is a session setting and not part of any function. It can be localised within a function if desired to over-ride the session setting. – Graham Mar 5 at 17:35

# dc, 5 4 bytes

8+I/


Try it online!

This is a dc "function": it pops the input from the stack, and then pushes the output onto the stack. (dc is a stack-based language.)

To call it, enter the desired input first (to push it on the stack), follow with the code above, and then enter p to print the output.

dc<<<$1d8+I/p  Try it online! The input is passed as an argument, and the output is printed. (This just uses my dc answer internally.) # C (gcc), 17 bytes f(n){n=(n+8)/10;}  Try it online! # C (gcc), 17 bytes f(n){n+=8;n/=10;}  Try it online! Alternative 17-byter: # C (gcc), 17 bytes f(n){n=n/10.+.8;}  Try it online! Alternative 17-byter # C (gcc), 17 bytes f(n){n=(n+8)/10;}  Try it online! # Clojure, 28 bytes (defn e[n](int(/(+ n 8)10)))  Ungolfed: (defn ends-in-two [n] (int (/ (+ n 8) 10)))  Test harness: (println (e 1)) (println (e 2)) (println (e 5)) (println (e 10)) (println (e 12)) (println (e 20)) (println (e 30)) (println (e 54)) (println (e 97)) (println (e 100))  Try it online! # Julia 1.0, 12 bytes x->(x+8)÷10  Try it online! # GolfScript, 6 bytes GolfScript has no decimal support, that's why the / works. ~8+10/  Try it online! # Kotlin, 25 bytes {(1..it).count{it%10==2}}  Try it online! # R, 18 15 bytes Thanks @Giuseppe! Guess I didn't really know what the %/% operator did. (scan()+8)%/%10  Try it online! • – Giuseppe Mar 5 at 19:50 • %/% is integer division -- see here for details, under "Value" :-) – Giuseppe Mar 6 at 1:57 # Stax, 4 bytes 8+A/  Run and debug it Just a standard add 8, integer divide by 10 # Burlesque, 7 bytes 8|+10|/  Try it online! Using RGS's method 8|+ # Add 8 (Parse string implcit) 10|/ # Divide by 10  # Burlesque, 10 bytes riq[~GO2CN  Try it online! ri # Read int q[~ # Boxed tail (last digit) GO # Generate from 1, N 2CN # Count number of 2's  # Rail, 87 bytes $'main'
-0(!a!)-/-(a)ia(!a!)\
#oda*8(a)-\ /e-----@
@-(!a!)/      >-
\m(a)--/


Try it online!

### Explanations:

0(!a!)         Put 0 into variable "a"
(a)ia(!a!)     Add "a" with input (one number at a time), put into variable "a"
e              check if it's EOF, then go left or right at the next junction

if false:
(a)m(!a!)  multiply "a" by 10, put into variable "a"

if true:
(a)8ado#   add a with 8, then divide by 10, print the output. Fin.

The rest of symbols are tracks


# Erlang (escript), 18 bytes

f(N)->(N+8)div 10.


Try it online!

# Explanation

f(N)->  % Function taking N as input
(N+8)   % Add input by 8
div 10. % Floor division by 10


# Pyt[sic!], 3 bytes

Finally found the right language. I had a now-deleted answer in Vim, but it returned the empty string for an input of 1 :(

8+₀


Explanation:

8    In fact, I have no idea whether is this language stack-based, I guess it pushes 8
+   add that 8 to the seemingly-implicit input
₀  divide by 10. There are also instructions to divide by numbers from 2 to 11 :)


Try it online!

# Pyth, 5 bytes

/+8QT


Try it online!

### Explanation

/+8QT
Q   : Variable containing evaluated input
+8    : Add 8 to it
/   T  : Divide result of add by 10


## Javascript (node) - 25 Bytes

f=n=>n?(n%10==2)+f(n-1):0


Try it online!

• -1 byte: f=n=>n&&(n%10==2)+f(n-1) – milk Mar 4 at 2:23
• 13 bytes – Steffan Mar 11 at 14:15

# JavaScript (V8), 15 bytes

f=n=>(n+8)/10|0


Try it online!

# PHP, 18 17 bytes

<?=\$argn/10+.8|0;


Try it online!

-1 bytes thanks to @oxgeba

# Python 3, 22 18 bytes

lambda n:(n+8)//10


Thanks to @JoKing for suggesting to use integer division.

• This approach can be generalized for any digits 0..9. To note 8 = 10-2 then we can rewrite the function in this way lambda n,d: (n+10-d)//10. – n1k9 Mar 4 at 7:14
• Where d is the digit to find in sequence. – n1k9 Mar 4 at 7:20