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

19
\$\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.

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

\$\endgroup\$
2
  • 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 '20 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 '20 at 19:13
7
\$\begingroup\$

Perl 5 -pl, 13 10 bytes

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

$_+=1<chop

Try it online!

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

APL (Dyalog Unicode), 8 7 bytes

-1 byte thanks to Bubbler

Full program

⌊.1×8+⎕

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ -1 byte using .1×. \$\endgroup\$
    – Bubbler
    Mar 2 '20 at 4:45
4
\$\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.
\$\endgroup\$
1
  • 1
    \$\begingroup\$ R%⁵ċ2 saves 1 \$\endgroup\$ Mar 1 '20 at 21:08
4
\$\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)
\$\endgroup\$
2
  • \$\begingroup\$ I like the fact that you are "trutifying" in your last explanation. Almost like "making look like a trout"! \$\endgroup\$
    – RGS
    Mar 3 '20 at 23:11
  • 1
    \$\begingroup\$ another four byte solution: <T/î \$\endgroup\$ Mar 28 '21 at 19:07
4
\$\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.

\$\endgroup\$
0
4
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 18 bytes: f(N)=floor(.1N+.8) \$\endgroup\$
    – golf69
    Oct 14 '20 at 21:59
  • \$\begingroup\$ @golf69 Thanks! I can't believe I didn't think to distribute the fraction... \$\endgroup\$
    – Aiden Chow
    Oct 14 '20 at 22:01
4
\$\begingroup\$

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)

\$\endgroup\$
8
  • \$\begingroup\$ can you help me then? \$\endgroup\$
    – 4R1u
    Mar 2 '20 at 13:30
  • \$\begingroup\$ See the upper answer. \$\endgroup\$ Mar 2 '20 at 13:37
  • \$\begingroup\$ 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. \$\endgroup\$ Mar 2 '20 at 17:22
  • \$\begingroup\$ 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. \$\endgroup\$
    – John
    Mar 2 '20 at 19:29
  • \$\begingroup\$ 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! \$\endgroup\$ Mar 6 '20 at 10:48
4
\$\begingroup\$

Haskell, 14 Bytes

f x=div(x+8)10
\$\endgroup\$
2
  • 1
    \$\begingroup\$ you can replace floor and / with div. \$\endgroup\$
    – ovs
    Mar 1 '20 at 20:16
  • 2
    \$\begingroup\$ The point-free version (`div`10).(+8) scores the same. \$\endgroup\$ Mar 1 '20 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
\$\endgroup\$
1
  • \$\begingroup\$ 2= is a really frequent operation, I need to make a built-in for that. :D Wk = K too... \$\endgroup\$
    – user92069
    Mar 2 '20 at 1:10
3
\$\begingroup\$

Powershell, 50 20 bytes

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

$args are arguments to pass as number.

\$\endgroup\$
6
  • \$\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 '20 at 12:32
  • \$\begingroup\$ @S.SAnne how to omit that? Please help I am new to code golfing. \$\endgroup\$
    – wasif
    Mar 2 '20 at 16:44
  • \$\begingroup\$ $args is an array of arguments Try it online!. See docs \$\endgroup\$
    – mazzy
    Mar 2 '20 at 16:54
  • \$\begingroup\$ Thanks I knew it but didn't thought I can use it here. \$\endgroup\$
    – wasif
    Mar 2 '20 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 '20 at 17:11
3
\$\begingroup\$

Kotlin, 25 bytes

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

Try it online!

\$\endgroup\$
3
\$\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!

\$\endgroup\$
1
  • \$\begingroup\$ Pyt? Maybe Pyth... \$\endgroup\$ Mar 8 '20 at 15:22
3
\$\begingroup\$

Husk, 7 bytes

#ȯ=2→dḣ

Try it online!

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

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

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

\$\endgroup\$
3
\$\begingroup\$

Scala, 11 bytes

n=>(n+8)/10

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 9 bytes: _.+(8)/10 \$\endgroup\$
    – user
    Aug 29 '21 at 13:59
3
\$\begingroup\$

Husk, 5 4 bytes

Edit: -1 byte thanks to Jo King

hs+8

Try it online! or check all test cases

How?

            # implicit input
    +8      # plus 8            
   s        # convert to string
  h         # remove last character
            # (so hs effectively divides by 10)
\$\endgroup\$
0
2
\$\begingroup\$

C (gcc), 17 bytes

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

Try it online!

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

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

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

Julia 1.0, 12 bytes

x->(x+8)÷10

Try it online!

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

GolfScript, 6 bytes

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

~8+10/

Try it online!

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

Javascript (node) - 25 Bytes

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

Try it online!

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

JavaScript (V8), 15 bytes

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

Try it online!

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

PHP, 18 17 bytes

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

Try it online!

-1 bytes thanks to @oxgeba

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

Python 3, 22 18 bytes

lambda n:(n+8)//10

Thanks to @JoKing for suggesting to use integer division.

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

R, 18 15 bytes

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

(scan()+8)%/%10

Try it online!

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

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.