# Count occurrences of an integer [closed]

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!

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

# 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


# k (28)

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


Takes three arguments, like this:

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


## 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!

• @ Everyone Just to let know, I miscounted the chars of recursive version. They are 62 instead of 72 :P Fixed. Feb 11, 2014 at 16:30

# 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

• Actually this are 43 needed chars. Feb 12, 2014 at 9:44
• You can remove the brackets () around the range, then it would be 41 chars. Feb 12, 2014 at 9:58
• sorry, my counting sucks. but thanks for the shortening Feb 12, 2014 at 11:51
• @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. Feb 12, 2014 at 12:03
• Thanks, now it works again. Although I still have to fix the lower limit, damn ... Should have used test cases. :( Feb 12, 2014 at 12:07

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.

• 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 Feb 12, 2014 at 9:21
• After some edits I beat the other one golfing with perl! Feb 12, 2014 at 10:59
• After fixing bugs I'm behind again. yey~ Feb 12, 2014 at 12:18

# 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

# 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

• Since you are leaking return variable r to the global scope, one could omit the return.. Feb 11, 2014 at 5:40
• 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} Feb 11, 2014 at 15:22
• You can also change e.toString() to (e+'') and (...)>=0 to ~(...).
– Noyo
Feb 15, 2014 at 12:00

## 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
`
• 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. Feb 10, 2014 at 12:14