11
\$\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\$

31 Answers 31

13
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ How'd you come up with this? \$\endgroup\$ – S.S. Anne Mar 2 at 12:26
  • 3
    \$\begingroup\$ @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 \$\endgroup\$ – RGS Mar 2 at 13:13
  • 1
    \$\begingroup\$ My first crack at it was lambda n:n/10+(n%10>1) \$\endgroup\$ – Cruncher Mar 2 at 21:13
  • \$\begingroup\$ @Cruncher still a really nice try :D \$\endgroup\$ – RGS Mar 2 at 22:02
  • \$\begingroup\$ 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. \$\endgroup\$ – Belly Buster Mar 3 at 13:41
6
\$\begingroup\$

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

Alternative 4 byter:

+8:⁵

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

| improve this answer | |
\$\endgroup\$
  • 3
    \$\begingroup\$ Interesting method. Looks like the "add 8 then int-divide by 10" that everyone is doing is the same length: +8:⁵(TIO) \$\endgroup\$ – xnor Mar 1 at 17:42
  • \$\begingroup\$ "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 \$\endgroup\$ – RGS Mar 1 at 19:13
6
\$\begingroup\$

Perl 5 -pl, 13 10 bytes

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

$_+=1<chop

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ doesn't return 0 for 1 \$\endgroup\$ – Nahuel Fouilleul Mar 2 at 16:10
  • \$\begingroup\$ Isn't an empty string a form of 0? :) I've fixed it. \$\endgroup\$ – Xcali Mar 2 at 16:22
  • 1
    \$\begingroup\$ 10 bytes: $_+=1<chop. Properly outputs 0 for input 1 (relevant meta). \$\endgroup\$ – Grimmy Mar 2 at 16:28
  • \$\begingroup\$ @Grimmy nice fix, i had 11 bytes but using -Minteger \$\endgroup\$ – Nahuel Fouilleul Mar 2 at 16:49
  • \$\begingroup\$ 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..? \$\endgroup\$ – ilkkachu Mar 3 at 19:07
4
\$\begingroup\$

APL (Dyalog Unicode), 8 7 bytes

-1 byte thanks to Bubbler

Full program

⌊.1×8+⎕

Try it online!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ -1 byte using .1×. \$\endgroup\$ – Bubbler Mar 2 at 4:45
3
\$\begingroup\$

Haskell, 14 Bytes

f x=div(x+8)10

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ you can replace floor and / with div. \$\endgroup\$ – ovs Mar 1 at 20:16
  • 1
    \$\begingroup\$ The point-free version (`div`10).(+8) scores the same. \$\endgroup\$ – Jonathan Frech Mar 1 at 23:55
3
\$\begingroup\$

W, 4 bytes

8+T/

Just another boring formula: Add eight, divide by 10. (W performs integer division if both operands are integers.)

W d, 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
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 2= is a really frequent operation, I need to make a built-in for that. :D Wk = K too... \$\endgroup\$ – Λ̸̸ Mar 2 at 1:10
  • \$\begingroup\$ Unless that divison in the 4 byter is integer division, then that's an invalid answer, as most n results in a decimal answer. \$\endgroup\$ – Lyxal Mar 2 at 1:52
  • \$\begingroup\$ 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. \$\endgroup\$ – Lyxal Mar 2 at 1:57
3
\$\begingroup\$

05AB1E, 4 bytes

8+T÷

Same approach as everyone else.

Try it online or verify all test cases.

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)
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I like the fact that you are "trutifying" in your last explanation. Almost like "making look like a trout"! \$\endgroup\$ – RGS Mar 3 at 23:11
3
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Jelly, 7 6 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.
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ R%⁵ċ2 saves 1 \$\endgroup\$ – Nick Kennedy Mar 1 at 21:08
2
\$\begingroup\$

Powershell, 50 20 bytes

1.."$args"-match"2$"

$args are arguments to pass as number.

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

APL+WIN, 9 bytes

Prompts for integer n:

+/2=10|⍳⎕

Try it online! Courtesy of Dyalog Classic

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ This only works if ⎕IO is 1. Does that need to be a part of the program? \$\endgroup\$ – mappo Mar 5 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 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.

| improve this answer | |
\$\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.)

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

C (gcc), 17 bytes

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

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

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!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

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!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Julia 1.0, 12 bytes

x->(x+8)÷10

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

GolfScript, 6 bytes

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

~8+10/

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Kotlin, 25 bytes

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

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

R, 18 15 bytes

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

(scan()+8)%/%10

Try it online!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ 15 bytes? \$\endgroup\$ – Giuseppe Mar 5 at 19:50
  • \$\begingroup\$ %/% is integer division -- see here for details, under "Value" :-) \$\endgroup\$ – Giuseppe Mar 6 at 1:57
1
\$\begingroup\$

Stax, 4 bytes

8+A/

Run and debug it

Just a standard add 8, integer divide by 10

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

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!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Javascript (node) - 25 Bytes

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

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ -1 byte: f=n=>n&&(n%10==2)+f(n-1) \$\endgroup\$ – milk Mar 4 at 2:23
  • \$\begingroup\$ 13 bytes \$\endgroup\$ – Steffan Mar 11 at 14:15
0
\$\begingroup\$

JavaScript (V8), 15 bytes

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

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

PHP, 18 17 bytes

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

Try it online!

-1 bytes thanks to @oxgeba

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Python 3, 22 18 bytes

lambda n:(n+8)//10

Thanks to @JoKing for suggesting to use integer division.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – n1k9 Mar 4 at 7:14
  • \$\begingroup\$ Where d is the digit to find in sequence. \$\endgroup\$ – n1k9 Mar 4 at 7:20

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.