13
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 popularity-contest. 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? – mmumboss – 2014-02-10T11:21:53.450
Z can be between 0 and Y. It doesn't make sense that Z can be bigger than Y. – Obl Tobl – 2014-02-10T11:31:58.170
@OblTobl Do you really want to explicitly exclude the Z>Y case? Why not just have expected output in that case be 0? – Cruncher – 2014-02-10T16:14:39.987
@Cruncher i don't mind! but it's a little bit useless i think ;-) – Obl Tobl – 2014-02-10T16:24:27.950
Does this mean that
N
can be123
and it would only match if the substring 123 exists? – Populus – 2014-02-10T16:40:08.433@Populus exactly! – Obl Tobl – 2014-02-10T16:43:56.033