17
\$\begingroup\$

Task

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!

\$\endgroup\$
1

53 Answers 53

1
2
2
\$\begingroup\$

Stax, 4 bytes

8+A/

Run and debug it

Just a standard add 8, integer divide by 10

\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

Vyxal, 4 bytes

8+ij∖

the easy solution.

Vyxal Ms, 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)
\$\endgroup\$
3
  • 1
    \$\begingroup\$ ƛSt\2=with s flag as well. i.e. Vyxal, Ms, 6 bytes \$\endgroup\$
    – lyxal
    Oct 17 '20 at 7:03
  • 1
    \$\begingroup\$ Vyxal, 8+Τ⳹ \$\endgroup\$
    – lyxal
    Jan 19 at 7:23
  • 1
    \$\begingroup\$ Vyxal, ƛSt\2= \$\endgroup\$
    – lyxal
    Jan 19 at 7:23
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

Io, 24 bytes

method(i,((i-1)/10)ceil)

Try it online!

1 byte saved, thanks to xigoi

\$\endgroup\$
1
  • 1
    \$\begingroup\$ -1 byte by using ((i-1)/10)ceil instead of ((i+8)/10)floor \$\endgroup\$
    – xigoi
    Jan 13 at 19:16
2
\$\begingroup\$

Nim, 29 28 bytes

func f[I](n:I):I=(n+8)div 10

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Oh, I heard about Nim ;) \$\endgroup\$ Jan 13 at 19:30
2
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

APL+WIN, 9 bytes

Prompts for integer n:

+/2=10|⍳⎕

Try it online! Courtesy of Dyalog Classic

\$\endgroup\$
2
  • \$\begingroup\$ This only works if ⎕IO is 1. Does that need to be a part of the program? \$\endgroup\$
    – mappo
    Mar 5 '20 at 13:28
  • \$\begingroup\$ @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. \$\endgroup\$
    – Graham
    Mar 5 '20 at 17:35
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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.)

\$\endgroup\$
1
\$\begingroup\$

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).

\$\endgroup\$
1
\$\begingroup\$

Java (JDK), 11 bytes

n->(n+8)/10

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Groovy, 18 bytes

{(it+8).intdiv 10}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Kotlin, 13 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

C, 29 21 20 17 bytes

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

Try It Online

Which works only if optimizations are turned off (more info).
If you love optimizations, then I got this 18 bytes solution for you:

#define N (n+8)/10

Try It Online

\$\endgroup\$
14
  • \$\begingroup\$ It's not golfed ;) \$\endgroup\$ Jan 10 at 15:09
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Jan 10 at 15:32
  • 1
    \$\begingroup\$ Thank You! It's nice to golf :) \$\endgroup\$ Jan 10 at 17:29
  • 1
    \$\begingroup\$ -1 byte: int f(int n){return(n+8)/10;} \$\endgroup\$ Jan 10 at 17:34
  • 1
    \$\begingroup\$ Oh thank you so much! I didn't know I could do it \$\endgroup\$ Jan 10 at 17:36
1
\$\begingroup\$

Coconut, 12 bytes

n->(n+8)//10

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

Risky, 4 bytes

?++//+/

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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)

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.