# 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!

• Oct 16, 2020 at 18:59
• Chameleon? (filler)
– null
Feb 1 at 15:50

# 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? Mar 2, 2020 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, 2020 at 13:13
• My first crack at it was lambda n:n/10+(n%10>1) Mar 2, 2020 at 21:13
• 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. Mar 3, 2020 at 13:41
• @BellyBuster I thank you for your compliment. I wouldn't call myself a genius of code-golf-mastery... But even the both of us can come up with some really nice golfs every now and then :)
– RGS
Mar 3, 2020 at 15:48

# 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, 2020 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, 2020 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 Mar 2, 2020 at 16:10 • Isn't an empty string a form of 0? :) I've fixed it. Mar 2, 2020 at 16:22 • 10 bytes: $_+=1<chop. Properly outputs 0 for input 1 (relevant meta). Mar 2, 2020 at 16:28
• @Grimmy nice fix, i had 11 bytes but using -Minteger Mar 2, 2020 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..? Mar 3, 2020 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×. Mar 2, 2020 at 4:45 # 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 Mar 1, 2020 at 21:08 # 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, 2020 at 23:11 • another four byte solution: <T/î Mar 28, 2021 at 19:07 # 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. # Desmos I know I'm 7 months late but this is my first code golf answer. I'm looking for some easier coding challenges to take down. I have two answers(one where I tried it without looking at any answers, then one after I looked through some answers.). ## First Answer, 62 bytes: f(N)=\sum_{n=1}^N\left\{\operatorname{mod}(n,10)=2:1,0\right\} Try it on Desmos! Explanation: f(N)= a function taking in an argument of N \sum_{n=1}^N summation from 1 to N \left\{ starting piecewise \operatorname{mod}(n,10)=2: if the remainder of n/10 is 2... 1 sum 1 , otherwise... 0 sum 0 \right\} end piecewise  Not too sure why I can't take out the \left and \right for the brackets({ and }). Theoretically it should work(I've taken out the \left's and \right's of all the other "left-right pairs"), but I guess Desmos doesn't allow it. ## Second Answer, 20 18 bytes: Saved two bytes thanks to @golf69 f(N)=floor(.1N+.8) Try it on Desmos! Explanation: My answer is equivalent to f(N)=floor((N+8)/10), which is explained in RGS's comment under his answer. • 18 bytes: f(N)=floor(.1N+.8) Oct 14, 2020 at 21:59 • @golf69 Thanks! I can't believe I didn't think to distribute the fraction... Oct 14, 2020 at 22:01 ## Python 3, 55 40 bytes Any help with golfing it would be appreciated lambda N:sum(x%10==2for x in range(N+1))  Lots of thanks to the commentors for helping me out( I have problems with memory, see names below) • can you help me then? – 4R1u Mar 2, 2020 at 13:30 • See the upper answer. Mar 2, 2020 at 13:37 • My best advice, for this challenge and in general, is to think about the problem and try to figure out a way to compute the result directly instead of iterating through all the numbers, etc. Iteration is slow, and sometimes it's the only solution - but very often there's A Better Way that you can find if you put some thought into it. Mar 2, 2020 at 17:22 • Welcome! Please see codegolf.stackexchange.com/questions/54/… for golfing in Python, and observe patterns in other questions that might help you get an idea on the practices in code golfing. Note the objective is not just to solve the problem at hand (in fact, generally we borrow solutions already posted and port into other languages) but also to implement the solution as efficiently as possible. – John Mar 2, 2020 at 19:29 • If you wish to solve it this way, with iteration, you may be best off using list-comprehension and check the len, it's definitely not the best way to approach it. Also, it's often best to express it as a lambda function to save chars. Try it online! Mar 6, 2020 at 10:48 # Haskell, 14 Bytes f x=div(x+8)10  • you can replace floor and / with div. – ovs Mar 1, 2020 at 20:16 • The point-free version (div10).(+8) scores the same. Mar 1, 2020 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... – user92069 Mar 2, 2020 at 1:10 # 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. Mar 2, 2020 at 12:32
• @S.SAnne how to omit that? Please help I am new to code golfing. Mar 2, 2020 at 16:44
• $args is an array of arguments Try it online!. See docs Mar 2, 2020 at 16:54 • Thanks I knew it but didn't thought I can use it here. Mar 2, 2020 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. Mar 2, 2020 at 17:11

# Julia 1.0, 12 bytes

x->(x+8)÷10


Try it online!

# Kotlin, 25 bytes

{(1..it).count{it%10==2}}


Try it online!

# 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!

• Pyt? Maybe Pyth... Mar 8, 2020 at 15:22

# Husk, 7 bytes

#ȯ=2→dḣ


Try it online!

• Nice - but there's an even golfier way... Oct 15, 2020 at 13:00
• 5 bytes. EDIT: Ah @DominicvanEssen already mentioned there is a shorter way, most likely referring to this. Oct 15, 2020 at 13:02
• @DominicvanEssen It was too boring to implement. Oct 15, 2020 at 13:08
• Haha I kind-of agree... but did it anyway! Oct 15, 2020 at 13:09

# Japt-x, 6 bytes

The straightforward solution of adding 8 and floor dividing by 10 would be a byte shorter: +8 zA. But where's the fun in that?!

õ_ìÌ¶2


Try it

õ_ìÌ¶2     :Implicit input of integer U
õ          :Range [1,U]
_         :Map
ì        :  To digit array
Ì       :  Last element
¶2     :  Is equal to 2?
:Implicit output of sum of resulting array


# Jelly, 10 7 bytes

ṾṪ=”2)S


Try it online!

But there's already 2 shorter answers I hear you say... Well, this doesn't use mathematics but rather uses strings

# Scala, 11 bytes

n=>(n+8)/10


Try it online!

• 9 bytes: _.+(8)/10
– user
Aug 29, 2021 at 13:59

# Husk, 5 4 bytes

Edit: -1 byte thanks to Jo King

hs+8


How?

            # implicit input
+8      # plus 8
s        # convert to string
h         # remove last character
# (so hs effectively divides by 10)


# 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!

# GolfScript, 6 bytes

GolfScript has no decimal support, that's why the / works.

~8+10/


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, 2020 at 2:23
• 13 bytes Mar 11, 2020 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, 2020 at 7:14
• Where d is the digit to find in sequence.
– n1k9
Mar 4, 2020 at 7:20

# R, 18 15 bytes

Thanks @Giuseppe! Guess I didn't really know what the %/% operator did.

(scan()+8)%/%10


Try it online!

• Mar 5, 2020 at 19:50
• %/% is integer division -- see here for details, under "Value" :-) Mar 6, 2020 at 1:57