13
\$\begingroup\$

Based on the question How many positive integers < 1,000,000 contain the digit 2?. I'm looking for the most creative solution to count all the Integers from X to Y containing the Integer Z. Z can be from 0 to Y.

Every found Integer only counts once, even if the integer Z appears more often. For example:

Z = 2
123 counts 1
22222 also counts 1

I will start with a really simple algorithm written in Java (because it's beloved by everyone):

public class Count {
    public static void main(String[] args) {
        int count = 0;
        for (int i = Integer.parseInt(args[0]); i <= Integer.parseInt(args[1]); i++) {
            if (Integer.toString(i).contains(args[2])) {
                count++;
            }
        }
        System.out.println(count);
    }
}

if you run this with

java -jar Count.jar 0 1000000 2

you get this as the result:

468559

Because this problem is not hard to solve it's just a . Most upvoted answer posted by 28th of February wins!

\$\endgroup\$
  • \$\begingroup\$ It's not entirely clear from your post, but I guess Z can be between 0 and inf? Or just between 0 and 9? \$\endgroup\$ – mmumboss Feb 10 '14 at 11:21
  • \$\begingroup\$ Z can be between 0 and Y. It doesn't make sense that Z can be bigger than Y. \$\endgroup\$ – Obl Tobl Feb 10 '14 at 11:31
  • \$\begingroup\$ @OblTobl Do you really want to explicitly exclude the Z>Y case? Why not just have expected output in that case be 0? \$\endgroup\$ – Cruncher Feb 10 '14 at 16:14
  • \$\begingroup\$ @Cruncher i don't mind! but it's a little bit useless i think ;-) \$\endgroup\$ – Obl Tobl Feb 10 '14 at 16:24
  • \$\begingroup\$ Does this mean that N can be 123 and it would only match if the substring 123 exists? \$\endgroup\$ – Populus Feb 10 '14 at 16:40

38 Answers 38

1
\$\begingroup\$

Haskell (56 code, 16 import)

Thought I'd throw a little Haskell version into the mix!

import Data.List
c x y z=length$filter(isInfixOf(show z))$map show$[x..y]

It can be shorter if we are allowed to require that z be input as a string:

import Data.List
c x y z=length$filter(isInfixOf z)$map show$[x..y]

Usage:

> c 0 1000000 2
468559
\$\endgroup\$
1
\$\begingroup\$

k (28)

{+/($x+!y-x)like,/3#"*",,$z}

Takes three arguments, like this:

  {+/($x+!y-x)like,/3#"*",,$z}[0;1000000;42]
49401
\$\endgroup\$
1
\$\begingroup\$

Javascript - 55 54 chars (Loop)

(Shortest JS solution!)

Edit: 12/Feb/14 - Updated search condition - 1 char reduced

a=(x,y,z,c)=>{while(x<=y)c+=(x+++'').search(z)+1?1:0}

Using the new fat arrow notation.

Usage: a(1,20,2,0),c gives 3 (Note the fourth argument is compulsorily set to 0 and don't forget the ,c also. Just some work to save chars ;) ).

Javascript - 62 61 chars (Recursive)

Edit: 12/Feb/14 - Updated search condition - 1 char reduced

c=0
a=(x,y,z)=>{if(x<=y)c+=(x+'').search(z)+1?1:0,a(x+1,y,z)}

Usage: a(1,20,2),c gives 3

Feedback appreciated!

\$\endgroup\$
  • \$\begingroup\$ @ Everyone Just to let know, I miscounted the chars of recursive version. They are 62 instead of 72 :P Fixed. \$\endgroup\$ – Gaurang Tandon Feb 11 '14 at 16:30
1
\$\begingroup\$

perl [41 chars]

print 0+grep/$ARGV[0]/,$ARGV[1]..$ARGV[2]

Run:

> perl -le 'print 0+grep/$ARGV[0]/,$ARGV[1]..$ARGV[2]' 2 0 1000000
468559
\$\endgroup\$
  • \$\begingroup\$ Actually this are 43 needed chars. \$\endgroup\$ – Leo Pflug Feb 12 '14 at 9:44
  • 1
    \$\begingroup\$ You can remove the brackets () around the range, then it would be 41 chars. \$\endgroup\$ – Leo Pflug Feb 12 '14 at 9:58
  • \$\begingroup\$ sorry, my counting sucks. but thanks for the shortening \$\endgroup\$ – Tom Tanner Feb 12 '14 at 11:51
  • \$\begingroup\$ @LeoPflug your code doesn't work - try giving it '22' as the search parameter (i don't have enough rep to comment on your post), and also try giving it 1000 as the lower limit. \$\endgroup\$ – Tom Tanner Feb 12 '14 at 12:03
  • \$\begingroup\$ Thanks, now it works again. Although I still have to fix the lower limit, damn ... Should have used test cases. :( \$\endgroup\$ – Leo Pflug Feb 12 '14 at 12:07
1
\$\begingroup\$

Perl - 44 chars

$x+=/$ARGV[2]/ for$ARGV[0]..$ARGV[1];print$x

Usage

C:\strawberry\projects>perl county.pl 0 1000000 2
468559

My first golf with Perl(I'm learning it currently) so I'm open for suggestions! I'd appreciate them.

\$\endgroup\$
  • \$\begingroup\$ Okay, just saw after posting this, that one already posted a good golf with perl. atleast I was close! Will use grep in future since I understand it now after this example. :D \$\endgroup\$ – Leo Pflug Feb 12 '14 at 9:21
  • \$\begingroup\$ After some edits I beat the other one golfing with perl! \$\endgroup\$ – Leo Pflug Feb 12 '14 at 10:59
  • \$\begingroup\$ After fixing bugs I'm behind again. yey~ \$\endgroup\$ – Leo Pflug Feb 12 '14 at 12:18
1
\$\begingroup\$

Golfscript - 18

My contribution in golfscript

`:i;,\>{`i?1+},,

Explaination:

`:i;        #Convert Z to string, store in i, and pop
,\>         #Generate range 0 ... Y and then remove values < X
{`i?1+},    #Filter all non-matches out of the array
,           #Count the number of values in the array

Try it here

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

Javascript

function f(s,e,n){for(r=0;e-->s;){if(~(''+e).indexOf(n))r++}return r}

f(0,1e6,2) returns 468559

What is --> operator ? Not an operator

\$\endgroup\$
  • \$\begingroup\$ Since you are leaking return variable r to the global scope, one could omit the return.. \$\endgroup\$ – GitaarLAB Feb 11 '14 at 5:40
  • \$\begingroup\$ Also you can save a char by using a for loop. function f(s,e,n){for(r=0;e-->s;){if(e.toString().indexOf(n)>=0)r++}return r} \$\endgroup\$ – Danny Feb 11 '14 at 15:22
  • 1
    \$\begingroup\$ You can also change e.toString() to (e+'') and (...)>=0 to ~(...)`. \$\endgroup\$ – Noyo Feb 15 '14 at 12:00
0
\$\begingroup\$

TI-BASIC

As usual, -> represents the STO-> arrow:

:0->I
:Prompt X,Y,Z
:randIntNoRep(X,Y)
:Z=Ans
:cumSum(Ans)

Let's try it out:

X=?1
Y=?10
Z=?2
               1
            Done
\$\endgroup\$
  • 2
    \$\begingroup\$ This does not work. the Z=Ans line will return zero if the number is not exactly the same. It will for example not recognize the 2 in 12. \$\endgroup\$ – mmumboss Feb 10 '14 at 12:14

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