20
\$\begingroup\$

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
$ java NotVerbose 127 -12 27
0

Snack Snippet

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

# Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

# Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

# Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the leaderboard snippet:

# [><>](http://esolangs.org/wiki/Fish), 121 bytes

/* Configuration */
var QUESTION_ID = 98470; // Obtain this from the url
// It will be like https://XYZ.stackexchange.com/questions/QUESTION_ID/... on any question page
var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";
var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk";
var OVERRIDE_USER = 41805; // This should be the user ID of the challenge author.
/* App */
var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page;
function answersUrl(index) {
 return "https://api.stackexchange.com/2.2/questions/" + QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER;
}
function commentUrl(index, answers) {
 return "https://api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER;
}
function getAnswers() {
 jQuery.ajax({
 url: answersUrl(answer_page++),
 method: "get",
 dataType: "jsonp",
 crossDomain: true,
 success: function (data) {
 answers.push.apply(answers, data.items);
 answers_hash = [];
 answer_ids = [];
 data.items.forEach(function(a) {
 a.comments = [];
 var id = +a.share_link.match(/\d+/);
 answer_ids.push(id);
 answers_hash[id] = a;
 });
 if (!data.has_more) more_answers = false;
 comment_page = 1;
 getComments();
 }
 });
}
function getComments() {
 jQuery.ajax({
 url: commentUrl(comment_page++, answer_ids),
 method: "get",
 dataType: "jsonp",
 crossDomain: true,
 success: function (data) {
 data.items.forEach(function(c) {
 if (c.owner.user_id === OVERRIDE_USER)
 answers_hash[c.post_id].comments.push(c);
 });
 if (data.has_more) getComments();
 else if (more_answers) getAnswers();
 else process();
 }
 }); 
}
getAnswers();
var SCORE_REG = /<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/;
var OVERRIDE_REG = /^Override\s*header:\s*/i;
function getAuthorName(a) {
 return a.owner.display_name;
}
function process() {
 var valid = [];
 
 answers.forEach(function(a) {
 var body = a.body;
 a.comments.forEach(function(c) {
 if(OVERRIDE_REG.test(c.body))
 body = '<h1>' + c.body.replace(OVERRIDE_REG, '') + '</h1>';
 });
 
 var match = body.match(SCORE_REG);
 if (match)
 valid.push({
 user: getAuthorName(a),
 size: +match[2],
 language: match[1],
 link: a.share_link,
 });
 
 });
 
 valid.sort(function (a, b) {
 var aB = a.size,
 bB = b.size;
 return aB - bB
 });
 var languages = {};
 var place = 1;
 var lastSize = null;
 var lastPlace = 1;
 valid.forEach(function (a) {
 if (a.size != lastSize)
 lastPlace = place;
 lastSize = a.size;
 ++place;
 
 var answer = jQuery("#answer-template").html();
 answer = answer.replace("{{PLACE}}", lastPlace + ".")
 .replace("{{NAME}}", a.user)
 .replace("{{LANGUAGE}}", a.language)
 .replace("{{SIZE}}", a.size)
 .replace("{{LINK}}", a.link);
 answer = jQuery(answer);
 jQuery("#answers").append(answer);
 var lang = a.language;
 if (/<a/.test(lang)) lang = jQuery(lang).text();
 
 languages[lang] = languages[lang] || {lang: a.language, user: a.user, size: a.size, link: a.link};
 });
 var langs = [];
 for (var lang in languages)
 if (languages.hasOwnProperty(lang))
 langs.push(languages[lang]);
 langs.sort(function (a, b) {
 if (a.lang > b.lang) return 1;
 if (a.lang < b.lang) return -1;
 return 0;
 });
 for (var i = 0; i < langs.length; ++i)
 {
 var language = jQuery("#language-template").html();
 var lang = langs[i];
 language = language.replace("{{LANGUAGE}}", lang.lang)
 .replace("{{NAME}}", lang.user)
 .replace("{{SIZE}}", lang.size)
 .replace("{{LINK}}", lang.link);
 language = jQuery(language);
 jQuery("#languages").append(language);
 }
}
body { text-align: left !important}
#answer-list {
 padding: 10px;
 width: 290px;
 float: left;
}
#language-list {
 padding: 10px;
 width: 290px;
 float: left;
}
table thead {
 font-weight: bold;
}
table td {
 padding: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b">
<div id="answer-list">
 <h2>Leaderboard</h2>
 <table class="answer-list">
 <thead>
 <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr>
 </thead>
 <tbody id="answers">
 </tbody>
 </table>
</div>
<div id="language-list">
 <h2>Winners by Language</h2>
 <table class="language-list">
 <thead>
 <tr><td>Language</td><td>User</td><td>Score</td></tr>
 </thead>
 <tbody id="languages">
 </tbody>
 </table>
</div>
<table style="display: none">
 <tbody id="answer-template">
 <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr>
 </tbody>
</table>
<table style="display: none">
 <tbody id="language-template">
 <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr>
 </tbody>
</table>

asked Nov 3, 2016 at 17:17
\$\endgroup\$
0

33 Answers 33

1
2
10
\$\begingroup\$

Bash, 20 bytes

the obvious answer

seq 2ドル 3ドル|grep -c 1ドル

example

$ bash golf 3 1 100
19
answered Nov 3, 2016 at 18:25
\$\endgroup\$
8
\$\begingroup\$

05AB1E, 6 bytes

Input in the form: upper bound, lower bound, number.

Ÿvy3åO

Explanation:

Ÿ # Inclusive range, [a, ..., b]
 vy # For each element...
 3å # Check if the third input is a substring of the number
 O # Sum up the results

Uses the CP-1252 encoding. Try it online!

answered Nov 3, 2016 at 17:28
\$\endgroup\$
4
  • 4
    \$\begingroup\$ Chooses Groovy {a,b,c->} Aww... dangit, I lost before I started again. \$\endgroup\$ Commented Nov 4, 2016 at 14:42
  • \$\begingroup\$ Congrats for winning this challenge! \$\endgroup\$ Commented Nov 10, 2016 at 16:39
  • \$\begingroup\$ @KritixiLithos Thank you! :) \$\endgroup\$ Commented Nov 10, 2016 at 16:40
  • \$\begingroup\$ 6-bytes alternative: Ÿʒ³å}g \$\endgroup\$ Commented Sep 13, 2018 at 11:13
6
\$\begingroup\$

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"
answered Nov 3, 2016 at 17:31
\$\endgroup\$
0
5
\$\begingroup\$

Python 2, (削除) 47 (削除ここまで) 43 Bytes

Relatively straightforward, making use of Python 2's repr short-form.

f=lambda n,a,b:a<b and(`n`in`a`)+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
answered Nov 3, 2016 at 17:36
\$\endgroup\$
2
  • \$\begingroup\$ Why did you have to be all fancy and use -~a instead of a+1? \$\endgroup\$ Commented Nov 4, 2016 at 18:15
  • 1
    \$\begingroup\$ @Artyer for fun! \$\endgroup\$ Commented Nov 4, 2016 at 19:05
5
\$\begingroup\$

Jelly, (削除) 7 (削除ここまで) 6 bytes

-1 thanks to caird coinheringaahing! (Use w and filtering to avoiding @.)

rAwƇ5L

TryItOnline!

Input: Start, End, ToFind

How?

rAwƇ5L - Main link: Start, End, ToFind
r - range: [Start, ..., End]
 A - absolute values
 5 - 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.

answered Nov 4, 2016 at 1:29
\$\endgroup\$
2
  • \$\begingroup\$ 6 bytes \$\endgroup\$ Commented Jul 14, 2021 at 21:50
  • \$\begingroup\$ Thanks @cairdcoinheringaahing that's great, I went with something closer to the original. \$\endgroup\$ Commented Jul 15, 2021 at 1:52
4
\$\begingroup\$

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.

answered Nov 3, 2016 at 17:39
\$\endgroup\$
1
  • \$\begingroup\$ !!match instead of includes. \$\endgroup\$ Commented Nov 3, 2016 at 17:56
4
\$\begingroup\$

PowerShell v2+, (削除) 64 (削除ここまで) (削除) 62 (削除ここまで) 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
answered Nov 3, 2016 at 17:40
\$\endgroup\$
2
  • \$\begingroup\$ Great! Thanks for 50K elements info. A couple of suggestions param($c,$a,$b)$(for(;$a-le$b){1|?{$a++-match$c}}).count \$\endgroup\$ Commented Sep 12, 2018 at 19:56
  • \$\begingroup\$ The param($c,$a,$b)($a..$b|?{$_-match$c}).count works with range -65536..65535 on Powershell 5.1 \$\endgroup\$ Commented Sep 12, 2018 at 20:43
3
\$\begingroup\$

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
answered Nov 3, 2016 at 17:43
\$\endgroup\$
2
\$\begingroup\$

Haskell, 65 bytes

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.

answered Nov 3, 2016 at 17:44
\$\endgroup\$
2
  • \$\begingroup\$ The usage example should output 24 because 12 appears 24 times between -200 and 200 \$\endgroup\$ Commented Nov 3, 2016 at 17:47
  • \$\begingroup\$ @KritixiLithos: Oh sorry! It does of course, it's just a copy & pase error. \$\endgroup\$ Commented Nov 3, 2016 at 17:49
2
\$\begingroup\$

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;}
answered Nov 3, 2016 at 17:53
\$\endgroup\$
0
2
\$\begingroup\$

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

answered Nov 4, 2016 at 16:48
\$\endgroup\$
2
\$\begingroup\$

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
 )
answered Nov 4, 2016 at 17:35
\$\endgroup\$
2
\$\begingroup\$

R, 32 bytes

Quite straightforward:

function(a,b,c)sum(grepl(a,b:c))
answered Nov 5, 2016 at 6:07
\$\endgroup\$
2
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Commented Nov 5, 2016 at 14:23
  • \$\begingroup\$ @Billywob thanks, will keep this in mind! edited the answer accordingly. \$\endgroup\$ Commented Nov 10, 2016 at 3:49
1
\$\begingroup\$

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;};
answered Nov 3, 2016 at 18:20
\$\endgroup\$
3
  • \$\begingroup\$ Java also has lambdas \$\endgroup\$ Commented Nov 3, 2016 at 18:27
  • \$\begingroup\$ 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 \$\endgroup\$ Commented Nov 3, 2016 at 18:28
  • \$\begingroup\$ Shamelessly stole @Grax s javascript answer (n,s,e)=>s>e?0:((""+s).Contains(n+"")?1:0)+f(n,++s,e); is way shorter \$\endgroup\$ Commented Nov 10, 2016 at 8:49
1
\$\begingroup\$

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
answered Nov 3, 2016 at 19:38
\$\endgroup\$
1
\$\begingroup\$

PHP, 62 bytes

Pretty straight forward approach:

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

Try it online

answered Nov 4, 2016 at 9:03
\$\endgroup\$
2
  • \$\begingroup\$ Save 4 bytes with underscore or any letter as regex delimiter. (needs no quotes) \$\endgroup\$ Commented Nov 28, 2016 at 15:22
  • \$\begingroup\$ You can save 3 Byte <?=count(preg_grep("/$argv[1]/",range($argv[2],$argv[3]))); \$\endgroup\$ Commented May 8, 2017 at 17:29
1
\$\begingroup\$

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));
}
answered Nov 3, 2016 at 23:12
\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Nov 4, 2016 at 6:59
  • \$\begingroup\$ 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... \$\endgroup\$ Commented Nov 4, 2016 at 7:24
  • \$\begingroup\$ 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;} \$\endgroup\$ Commented Sep 12, 2018 at 20:30
1
\$\begingroup\$

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

answered Nov 3, 2016 at 17:39
\$\endgroup\$
1
\$\begingroup\$

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

answered Nov 3, 2016 at 18:01
\$\endgroup\$
1
  • \$\begingroup\$ strstr($a[2]++,$a[1])>"" instead of strpos($a[2]++,$a[1])!==false saves 5 bytes. \$\endgroup\$ Commented Nov 28, 2016 at 15:27
1
\$\begingroup\$

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
answered Sep 12, 2018 at 20:08
\$\endgroup\$
1
\$\begingroup\$

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
answered May 22, 2017 at 15:57
\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Sep 13, 2018 at 11:16
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Sep 13, 2018 at 11:38
  • \$\begingroup\$ 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. :) \$\endgroup\$ Commented Sep 13, 2018 at 12:35
  • 1
    \$\begingroup\$ @KevinCruijssen: explanation added. \$\endgroup\$ Commented Sep 13, 2018 at 12:40
1
\$\begingroup\$

Vyxal s, 7 bytes

÷ṡƛS?hc

Try it Online!

answered Jul 11, 2021 at 11:46
\$\endgroup\$
0
\$\begingroup\$

Java, (削除) 92 (削除ここまで) (削除) 89 (削除ここまで) 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!

answered Nov 3, 2016 at 17:52
\$\endgroup\$
2
  • \$\begingroup\$ 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 \$\endgroup\$ Commented Nov 3, 2016 at 17:56
  • 1
    \$\begingroup\$ Basically the same as this answer. \$\endgroup\$ Commented Nov 3, 2016 at 19:52
0
\$\begingroup\$

Groovy, 48 bytes

{a,b,c->(a..b).collect{"$it".count("$c")}.sum()}
answered Nov 4, 2016 at 14:46
\$\endgroup\$
0
\$\begingroup\$

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
answered Nov 6, 2016 at 6:33
\$\endgroup\$
0
\$\begingroup\$

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
answered Nov 28, 2016 at 11:33
\$\endgroup\$
0
\$\begingroup\$

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

answered May 22, 2017 at 15:16
\$\endgroup\$
0
\$\begingroup\$

Clojure, 65 bytes

#(count(for[i(range %2(inc %3)):when(some(set(str %))(str i))]i))
answered May 22, 2017 at 15:40
\$\endgroup\$
0
\$\begingroup\$

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
answered Sep 12, 2018 at 19:17
\$\endgroup\$
0
\$\begingroup\$

Perl 6, 32 bytes

{+grep *.contains($^a),$^b..$^c}

Try it online!

answered Sep 13, 2018 at 12:24
\$\endgroup\$
1
2

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.