# 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 '20 at 18:59

# 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!)\ #od[01]a*8(a)-\ /e-----@ @-(!a!)/ >- \m(a)[01]--/  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: [10](a)m(!a!) multiply "a" by 10, put into variable "a" if true: (a)8a[10]do# 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  # Excel, 43 bytes =SUM(1*(RIGHT(ROW(OFFSET(A1,0,0,A1)))="2"))  OFFSET(A1,0,0,A1) generates a range starting at A1 that is A1's value rows tall. ROW(OFFSET(~)) returns an array of row numbers for that range ($$\[1,N]\$$). RIGHT(ROW(~)) returns an array of the right-most character of those row numbers as a string. RIGHT(~)="2" returns an array of TRUE or FALSE. 1*(~) transforms TRUE to 1 and FALSE to 0. SUM(~) sums. # Vyxal, 4 bytes 8+ĳ∖  the easy solution. # VyxalMs, 6 bytes ƛSt\2=  -2 bytes from Lyxal. ## Explanation ƛSt\2= ƛ map range 1..n to the following: S convert to string t get last character \2= is it equal to '2'? sum the results(s flag)  • ƛSt\2=with s flag as well. i.e. Vyxal, Ms, 6 bytes Oct 17 '20 at 7:03 • Vyxal, 8+Τ⳹ Jan 19 '21 at 7:23 • Vyxal, ƛSt\2= Jan 19 '21 at 7:23 # Brachylog, 3 bytes +₈k  Try it online! Since Brachylog likes to treat integers as lists of decimal digits when appropriate, the "remove last element" builtin k can be used to floor-divide by 10. # Io, 24 bytes method(i,((i-1)/10)ceil)  Try it online! 1 byte saved, thanks to xigoi • -1 byte by using ((i-1)/10)ceil instead of ((i+8)/10)floor Jan 13 '21 at 19:16 # Nim, 29 28 bytes func f[I](n:I):I=(n+8)div 10  Try it online! • Oh, I heard about Nim ;) Jan 13 '21 at 19:30 # Factor, 13 bytes [ 8 + 10 /i ]  Try it online! It's a quotation (anonymous function) that adds 8 to its input and then (integer) divides it by 10. # 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? Mar 5 '20 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. Mar 5 '20 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. # Bash + Unix utilities, 13 bytes 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.)

# AWK, 17 bytes

$1=int($1/10+.8)e


Try it online!

This substitutes the input $1 for the formula int($1/10+.8), and also appends the e variable, which is null (not defined variables return null). This causes \$1 to become a string, preventing the expression from evaluating 0 when input is 0 or 1. A false pattern wouldn't allow the line to be printed (0 is false).

# Java (JDK), 11 bytes

n->(n+8)/10


Try it online!

# Groovy, 18 bytes

{(it+8).intdiv 10}


Try it online!

# Kotlin, 13 bytes

{a->(a+8)/10}


Try it online!

# C, 292120 17 bytes

f(n){n=(n+8)/10;}


### Try It Online

If you love optimizations, then I got this 18 bytes solution for you:

#define N (n+8)/10


Try It Online

• It's not golfed ;) Jan 10 '21 at 15:09
• In code-golf you should implement either a function or a program. In this case program would take n as input and print the result to output and function would take n as argument and return the result. Jan 10 '21 at 15:32
• Thank You! It's nice to golf :) Jan 10 '21 at 17:29
• -1 byte: int f(int n){return(n+8)/10;} Jan 10 '21 at 17:34
• Oh thank you so much! I didn't know I could do it Jan 10 '21 at 17:36

# Coconut, 12 bytes

n->(n+8)//10


Try it online!

# TI-BASIC, 8 bytes

int(.1(N+8


There are a handful of equivalent solutions here because of the number of ways you can do implicit multiplication by .1 - there's also a solution available using the default value of Xmax from the graphscreen variables.

int(.1N+.8

int(N/Xmax+.8


# Ruby, 44 bytes

->x{(1..x).to_a.map{|e|(e%10)==2?1:0}.sum}


Try it online!

I don't use Ruby very often, I'm sure it can be golfed further.

# Risky, 4 bytes

?++//+/


Try it online!

# Mathematica, 14 bytes

⌊.1*#+.8⌋&


RGS did most of the work by coming up with the clever function to determine the answer. Putting it into Wolfram Langage was then trivial.

Try it online!

NB:

⌊ = \[LeftFloor] = U+230A (3 bytes)

⌋ = \[RightFloor] = U+230B (3 bytes)