# How many integers contain a number in a specific range

The program should take input the number, the start of the range and the end of the range, and output how many integers the number appears between the start and end of the range, inclusive. Both programs and functions are allowed.

## Example Inputs

For example:

//Input example 1
3,1,100
//Input example 2
3
1
100
//Input example 3
3 1 100
//Input example 4
a(3, 1, 100);

All the above four input examples are valid and all of them mean that 3 is the number in question, 1 is the beginning of the range and 100 the the end of the range.

And then the program should output how many times 3 appears in the range from 1 to 100 inclusive. 3 appears in the integers 3, 13, 23, 30, 31, 32, 33, ..., 93 at a total of 19 times. So the program should output 19 as the output because that is how many times 3 appears in the range from 1 to 100.

## Rules

• Both programs and functions are allowed.
• All numbers will be integers, meaning that there will not be any floats or doubles.
• Note: the sought number will always be in the range 0≤x≤127. There will be no cases where it will be outside this 0≤x≤127 range.
• As in the first example, with the case as 33, the number 3 will be counted as appearing only once, not twice.
• The values of the start and end of the range will be between -65536 and 65535 inclusive.
• The value of range's start will never exceed or equal to range's end. start < end
• Also the range is inclusive. For example if the input was 8 8 10, the range would be 8≤x≤10 and hence the output will be 1.
• Input can be taken in any of the ways shown in the examples. Input can be taken as a string or as a number, any way you wish.

## Test Cases

3 1 100
19

3 3 93
19

12,-200,200
24          //This is because 12 appears in -129, -128, ..., -112, -12, 12, 112, 120, 121, 122, ...

123,1,3
0           //This is because all of 123's digits have to appear in the same order

3 33 34
2           //Because 3 appears in 2 numbers: 33 and 34

a(0,-1,1);
1

example

$bash golf 3 1 100 19 # 05AB1E, 6 bytes Input in the form: upper bound, lower bound, number. Ÿvy³åO Explanation: Ÿ # Inclusive range, [a, ..., b] vy # For each element... ³å # Check if the third input is a substring of the number O # Sum up the results Uses the CP-1252 encoding. Try it online! • Chooses Groovy {a,b,c->} Aww... dangit, I lost before I started again. Nov 4 '16 at 14:42 • Congrats for winning this challenge! Nov 10 '16 at 16:39 • @KritixiLithos Thank you! :) Nov 10 '16 at 16:40 • 6-bytes alternative: Ÿʒ³å}g Sep 13 '18 at 11:13 ## Perl, 20 bytes Saved 2 bytes by using grep as in @ardnew's answer. Bytecount includes 18 bytes of code and -ap flags.$_=grep/@F/,<>..<>

Give the 3 numbers on three separate lines :

perl -ape '$_=grep/@F/,<>..<>' <<< "3 1 100" # Python 2, 47 43 Bytes Relatively straightforward, making use of Python 2's repr short-form. f=lambda n,a,b:a<b and(nina)+f(n,-~a,b) Ouput: f( 3, 1, 100) -> 19 f( 3, 3, 93) -> 19 f( 12, -200, 200) -> 24 f(123, 1, 3) -> 0 f( 3, 33, 34) -> 2 f( 0, -1, 1) -> 1 f(127, 12, 27) -> 0 • Why did you have to be all fancy and use -~a instead of a+1? Nov 4 '16 at 18:15 • @Artyer for fun! – Kade Nov 4 '16 at 19:05 # Jelly, 7 6 bytes -1 thanks to caird coinheringaahing! (Use w and filtering to avoiding @.) rAwƇ⁵L TryItOnline! Input: Start, End, ToFind ### How? rAwƇ⁵L - Main link: Start, End, ToFind r - range: [Start, ..., End] A - absolute values ⁵ - third input: ToFind Ƈ - filter keep those (absolute values, a) for which: w - first index of sublist (implicit digits of ToFind) in (implicit digits of a) L - length The default casting of an integer to an iterable for the sublist existence check casts to a decimal list (not a character list), so negative numbers have a leading negative value (e.g. -122->[-1,2,2] which won't find a sublist of [1,2]) so taking the absolute value first seems like the golfiest solution. • 6 bytes Jul 14 at 21:50 • Thanks @cairdcoinheringaahing that's great, I went with something closer to the original. Jul 15 at 1:52 ## JavaScript (ES6), 46 45 bytes f=(n,s,e)=>s<=e&&!!${s++}.match(n)+f(n,s,e)

(My best nonrecursive version was 61 bytes.) Edit: Saved 1 byte thanks to @edc65.

• !!match instead of includes. Nov 3 '16 at 17:56

## PowerShell v2+, 6462 56 bytes

param($c,$a,$b)$(for(;$a-le$b){1|?{$a++-match$c}}).count

-6 bytes thanks to mazzy

Input via command-line arguments of the form number lower_bound upper_bound. A little goofy on the notation, because of the semicolons inside the for causing parse errors if it's not surrounded in $(...) to create a script block. We basically loop upward through$a until we hit $b, using Where-Object (the |?{...}) to pull out those numbers that regex -match against$c. That's encapsulated in parens, we take the .count thereof, and that's left on the pipeline and output is implicit.

If, however, we guarantee that the range will be no more than 50,000 elements, we can skip the loop and just use the range operator .. directly, for 45 43 bytes. Since that's not in the challenge specifications, though, this isn't valid. Bummer.

param($c,$a,$b)($a..$b|?{$_-match$c}).count • Great! Thanks for 50K elements info. A couple of suggestions param($c,$a,$b)$(for(;$a-le$b){1|?{$a++-match$c}}).count Sep 12 '18 at 19:56 • The param($c,$a,$b)($a..$b|?{$_-match$c}).count works with range -65536..65535 on Powershell 5.1 Sep 12 '18 at 20:43

# Vim, 46, 41 bytes

C<C-r>=r<tab><C-r>")<cr><esc>jC0<esc>:g/<C-r>"/norm G<C-v><C-a>
kd{

Input is in this format:

1, 100
3

import Data.List
(s#e)i=sum[1|x<-[s..e],isInfixOf(show i)$show x] The import ruins the score. Usage example: ((-200)#200)12 -> 24. • The usage example should output 24 because 12 appears 24 times between -200 and 200 Nov 3 '16 at 17:47 • @KritixiLithos: Oh sorry! It does of course, it's just a copy & pase error. – nimi Nov 3 '16 at 17:49 # Java 7 85 bytes int x(int a,int b,int c){int t=0;for(;b<=c;)if((b+++"").contains(a+""))t++;return t;} # Swift 3, 96 93 bytes import Cocoa func c(n:Int,s:Int,e:Int){print((s...e).filter{"\($0)".contains("\(n)")}.count)}

Edit 1:

Saved 3 bytes by using shorthand parameters

# Scala, 50 bytes

(c:String)=>(_:Int)to(_:Int)count(""+_ contains c)

takes the first input curried; call it like this: f("12")(-200,200)

Explantion:

(c:String)=>  //define an anonymous function taking a string parameter
(_:Int)     //create a range from an anonymous int parameter
to          //to
(_:Int)     //another anonymous int parameter
count(      //and count how many...
""+_       //elements converted to a string
contains c //contain c
)

# R, 32 bytes

Quite straightforward:

function(a,b,c)sum(grepl(a,b:c))
• Welcome to PPCG! Nice answer but assuming that input is already specified is generally not accepted. To make your answer qualify you would either have to read input from stdin such as: a=scan();sum(grepl(a,a[2]:a[3])) or as arguments to a function: function(a,b,c)sum(grepl(a,b:c)), both equivalent in this case. Nov 5 '16 at 14:23
• @Billywob thanks, will keep this in mind! edited the answer accordingly.
– runr
Nov 10 '16 at 3:49

# C#, 71 bytes

Beat my Java answer thanks to lambdas

(t,l,u)=>{int d=0;for(;l<=u;)if((l+++"").Contains(t+""))d++;return d;};
• Java also has lambdas Nov 3 '16 at 18:27
• Yeah I just started reading about it, but don't they require some boilerplate stuff that would make the bytecount larger, or can I not count it Nov 3 '16 at 18:28
• Shamelessly stole @Grax s javascript answer (n,s,e)=>s>e?0:((""+s).Contains(n+"")?1:0)+f(n,++s,e); is way shorter Nov 10 '16 at 8:49

# Ruby 44 bytes

m=->(n,s,f){(s..f).count{|x|x.to_s[/#{n}/]}}

Test Cases:

m.(3,1,100)     #=> 19
m.(3,3,93)      #=> 19
m.(12,-200,200) #=> 24
m.(123,1,3)     #=>  0
m.(3,33,34)     #=>  2
m.(0,-1,1)      #=>  1
m.(127,-12,27)  #=>  0

# PHP, 62 bytes

Pretty straight forward approach:

<?=count(preg_grep('/'.($a=$argv)[1].'/',range($a[2],$a[3])));

Try it online

• Save 4 bytes with underscore or any letter as regex delimiter. (needs no quotes) Nov 28 '16 at 15:22
• You can save 3 Byte <?=count(preg_grep("/$argv[1]/",range($argv[2],$argv[3]))); May 8 '17 at 17:29 # C, 143 135 bytes Thanks to @Kritixi Lithos for helping save 8 bytes Surely this can be done better, but its the best I've got for now. C doesn't handle strings very gracefully, so naturally it takes quite a few operations. int C(int N,int l,int h){char b[99],n[99];int t=0,i=1;sprintf(n,"%d",N);for(;i<=h;i++){sprintf(b,"%d",i);if(strstr(b,n))++t;}return t;} Ungolfed + program #include <string.h> #include <stdlib.h> #include <stdio.h> int C(int N,int l,int h) { char b[99], n[99]; int t=0,i=1; sprintf(n,"%d",N); for(;i<=h;i++) { sprintf(b,"%d",i); if(strstr(b,n)) ++t; } return t; } int main() { printf("%d\n", C(3, 1, 100)); } • I think you can remove the int i=l from the for-loop and instead initialise it with int t=0 like such int t=0,i=l to save a few bytes. Nov 4 '16 at 6:59 • This not compile? C( N, l, h){char b[99], n[99];int t=0,i=l;sprintf(n,"%d",N);for(;i<=h;i++){sprintf(b,"%d",i);if(strstr(b,n))++t;}return t;} I think compile even with no include... – user58988 Nov 4 '16 at 7:24 • 93 bytes b[9],n[9],t;C(N,l,h){for(t=!sprintf(n,"%d",N);l<=h;strstr(b,n)&&++t)sprintf(b,"%d",l++);N=t;} Sep 12 '18 at 20:30 # JavaScript, 46 45 bytes f=(i,s,e)=>s>e?0:RegExp(i).test(s)+f(i,++s,e) Recursively count until start > end Edit: Switch to RegExp test to save a byte # PHP, 68 63 bytes for($a=$argv;$a[2]<=$a[3];)$o+=strstr($a[2]++,$a[1])>'';echo$o; use like: php -r "for($a=$argv;$a[2]<=$a[3];)$o+=strstr($a[2]++,$a[1])>'';echo$o;" 3 1 100 edit: 5 bytes saved thanks to Titus • strstr($a[2]++,$a[1])>"" instead of strpos($a[2]++,$a[1])!==false saves 5 bytes. Nov 28 '16 at 15:27 # Powershell, 48 bytes According to the rule, the range can contain more than 50,000 elements. So we can't use the range operator .. directly. Thanks AdmBorkBork. Straightforward: param($c,$a,$b)for(;$a-le$b){$i+=$a++-match$c}$i

Test script:

$f = { param($c,$a,$b)for(;$a-le$b){$i+=$a++-match$c}$i

}

@(
,(19, 3,1,100)
,(19, 3,3,93)
,(24, 12,-200,200)
,(0, 123,1,3)
,(2, 3,33,34)
,(1, 0,-1,1)
,(0, 127,-12,27)
,(44175, 0,-65536,65535)
) | % {
$e,$a = $_$r = &$f @a "$($e-eq$r): $r" } Output: True: 19 True: 19 True: 24 True: 0 True: 2 True: 1 True: 0 True: 44175 # Japt, 14 8 bytes Takes the integer to be found as the last input value. õV èÈsøW Try it online ## Explanation :Implicit input of integers U=start, V=end & W=number õV :Range [U,V] È :Map s : Convert to string øW : Contains W? è :Count truthy values • Since the previous versions lack an explanation I'm not sure about those, but your current 6-byte solution is incorrect I'm afraid. See this rule: "As in the first example, with the case as 33, the number 3 will be counted as appearing only once, not twice." Your occurrence-count for W would count the 3 twice. Sep 13 '18 at 11:16 • Thanks, @KevinCruijssen, came back to it ~a month later and was wondering why I was doing it the way I was when there was a shorter way - should have reread the challenge before updating! I've rolled it back now. Sep 13 '18 at 11:38 • I had the same thing happen a few times. I see my answer, think: this can be much easier, am changing it. And right before I hit save changes I see I now misinterpret the challenge. Btw, I'm still curious about the explanation for the 8-byte solution. :) Sep 13 '18 at 12:35 • @KevinCruijssen: explanation added. Sep 13 '18 at 12:40 # Vyxals, 7 bytes ÷ṡƛS?hc Try it Online! # Java, 9289 71 bytes Now with lambdas! (t,l,u)->{int z=0;for(;l<=u;)if((l+++"").contains(t+""))z++;return z;}; Old 89 byte function solution: int d(int t,int l,int u){int a=0,i=l;for(;i<=u;)if((i+++"").contains(t+""))a++;return a;} Hooray for the super increment function! • You can remove int i=l from the for-loop and instead declare it with a like int a=0,i=l; to save few bytes Nov 3 '16 at 17:56 • Ah I knew I missed something, thanks! Nov 3 '16 at 18:10 • Basically the same as this answer. Nov 3 '16 at 19:52 ## GolfSharp (non competing), 41 bytes (w,q,e)=>r(q,1+e-q).w(n=>n.T().I(w)).L(); ## competing 45 bytes (w,q,e)=>r(q,1+e-q).w(n=>n.T().I(w.T())).L(); • The last commit was 8 minutes ago, so for this to be a competing answer requires that this worked before the challenge started. Can you verify this? – Kade Nov 3 '16 at 19:46 • I think so, if not i will change it to non competing, edit the bugs fixed are necessary. will change now (i use challenges to improve the language) Nov 3 '16 at 19:48 • The code commit 8 minutes ago changed the I function to convert the element to a string first before checking if it contains it. – Kade Nov 3 '16 at 19:49 # Groovy, 48 bytes {a,b,c->(a..b).collect{"$it".count("$c")}.sum()} ## Racket 91 bytes (for/sum((i(range s(+ 1 e))))(if(string-contains?(number->string i)(number->string d))1 0)) Ungolfed: (define(f d s e) (for/sum ((i (range s (+ 1 e)))) (if(string-contains? (number->string i) (number->string d)) 1 0 ))) Testing: (f 3 1 100) (f 3 3 93) (f 12 -200 200) (f 123 1 3) (f 3 33 34) (f 0 -1 1) Output: 19 19 24 0 2 1 # Axiom bytes 90 f(y,a,b)==(c:=0;for x in a..b repeat(if position(y::String,x::String,1)~=0 then c:=c+1);c) results (3) -> f(3,1,100)=19,f(3,3,93)=19,f(12,-200,200)=24,f(123,1,3)=0,f(3,33,34)=2 (3) [19= 19,19= 19,24= 24,0= 0,2= 2] Type: Tuple Equation NonNegativeInteger (4) -> f(0,-1,1)=1, f(127,12,27)=0 (4) [1= 1,0= 0] Type: Tuple Equation NonNegativeInteger ## Mathematica, 70 bytes (w=ToString;t=0;Table[If[StringContainsQ[w@i,w@#1],t++],{i,#2,#3}];t)& input [12,-200,200] output 24 ## Clojure, 65 bytes #(count(for[i(range %2(inc %3)):when(some(set(str %))(str i))]i)) ## PHP, 56 Bytes run as pipe Try it online Input$argv = [number_to_find, range_start, range_end];

Code

<?=substr_count(join(range(($a=$argv)[1],$a[2])),$a[0]);

Explanation

#substrcount, counts the aparitions of a subtring in a string
substr_count(
join( range(($a=$argv)[1],$a[2])), # String with the range$a[0]);                            # The number you are looking for