# How many times, are they multiples?

You are given three parameters: start(int), end(int) and list(of int);

Make a function that returns the amount of times all the numbers between start and end are multiples of the elements in the list. example:

start = 15; end = 18; list = [2, 4, 3];
15 => 1 (is multiple of 3)
16 => 2 (is multiple of 2 and 4)
17 => 0
18 => 2 (is multiple of 2 and 3)
result = 5


The function should accept two positive integer numbers and an array of integers as parameters, returning the total integer number. Assume that start is less <= end.

examples:

Multiple(1, 10, [1, 2]); => 15
Multiple(1, 800, [7, 8]); => 214
Multiple(301, 5000,[13, 5]); => 1301


The shortest solution is the victor!!! May he odds be ever in your favor...

New contributor
Rui Silva is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
• Nice challenge! Presumably the winning criterion is shortest code? In which case you should say so explicitly, and add the code-golf tag. – Robin Ryder Dec 2 at 10:16
• Can we assume that start will always be less than end? – Galen Ivanov Dec 2 at 11:49
• You say integers. So start, end or the elements in list may be non-positive? – Seb Dec 3 at 11:17
• Thanks for the feedback, I was lacking that in the problem. – Rui Silva Dec 3 at 14:43
• I'd like to ask a question (as I'm really new in this channel), how do I decide the winner? since each language has it's own limitations... Or is it simply a personal preference? – Rui Silva 7 hours ago

# 05AB1E, 5 bytes

ŸÑ˜åO


Try it online!

### Explanation

Ÿ       - the numbers between start and end
Ñ      - get their divisors
˜     - deep flatten this list
å    - find the instances of the elements in the list in these divisors
O   - Sum this


# JavaScript (Node.js), 44 bytes

(a,x,y)=>a.map(n=>t+=y/n-(~-x/n|0)|0,t=0)&&t


Try it online!

# Python 2, 38 bytes

lambda a,x,y:sum(y/i-~-x/i for i in a)


Try it online!

$$\ \sum _{i \in list} \lfloor \frac{end}{i} \rfloor - \lfloor \frac{start-1}{i} \rfloor \$$

The answer seems trivial and naive. So am I misunderstand something?

• Your Python answer is right -- I was going to post the same. Congrats on figuring out the simplification. I'm disappointed nobody else had found the floor-division idea earlier, at least on my quick glance of the answers. – xnor Dec 3 at 9:50
• @xnor the formula comes up with my mind just after I read the question. But since the question is posted ~1d ago (and no one using it), I can't quite believe it could be correct. After posting the JavaScript version, I realized that by using Python I could save many bytes.. – tsh Dec 3 at 9:55

(a#b)x=sum[1|0<-mod<$>[a..b]<*>x]  Try it online! Explanation: (a#b)x --take two values a and b and the list x [a..b] --generate the range a, a+1, ... , b mod<$>[a..b]      --partially apply "mod" to each entry of the list
mod<$>[a..b]<*>x --apply the partially applied functions to all values of the list x [1|0<-mod<$>[a..b]<*>x] --generate a new list with a one for every zero in the previously computed list
(a#b)x=sum[1|0<-mod<$>[a..b]<*>x] --sum it all up  • Nice method. I found a variant that ties and tried to shorten it point-free but could tie at best -- maybe you have a better idea. – xnor Dec 3 at 9:58 • @xnor I've also tried to get rid of the x, but without success, the (0^) trick is neat, please consider posting it as an own answer! (So far I have had no success in golfing your suggestions further.) – flawr Dec 3 at 15:45 # Perl 6, 21 bytes {sum$^a..$^b X%%@^c}  Try it online! ### Explanation { } # Anonymous codeblock returning sum # The sum of$^a..$^b # How many numbers in the range a to b X%% # Are divisible by any of @^c # The third parameter  # MATL, 5 bytes :i\~z  Inputs are a cell array containing the start and end values, and then a column vector defining list. ### Explanation : % Range, with implicit input: cell array {start, end}. This gives the range % from start to end i % Input: list of numbers in the form of a column vector \ % Modulo, with boradcast. This gives a matrix with each number in the range % modulo each number in the list ~ % Logical negation z % Number of nonzero entries. Implicit display  # Python 3, 54 bytes lambda s,e,L:sum(n%l<1for n in range(s,e+1)for l in L)  Try It Online! # C++ (clang), 113110109108106100 93 bytes Saved 3 bytes thanks to @AZTECCO! Saved 6 13 bytes thanks to @bznein! Note: this function returns the sum through a parameter reference, is that ok? #import<set> void f(int a,int b,std::set<int>l,int&s){for(s=0;a<=b;++a)for(int i:l)s+=a%i<1;}  Try it online! Without returning the sum through a parameter: # C++ (clang), 100 bytes #import<set> int f(int a,int b,std::set<int>l){int s=0;for(;a<=b;++a)for(int i:l)s+=a%i<1;return s;}  Try it online! • You can use #import<vector> to save 1 – AZTECCO Dec 2 at 15:23 • @AZTECCO Thanks, didn't know about #import. :-) – Noodle9 Dec 2 at 17:42 • #include is standard.. See stackoverflow.com/a/172264/7699953 btw you can save 2 by using s+=a%i<1; instead of s+=a%i?0:1; – AZTECCO Dec 2 at 20:58 • @AZTECCO Ah, good to see Microsoft doing something useful for the code-golfing community. Thanks for the -2! :-) – Noodle9 Dec 2 at 21:12 • I am not that familiat to codegolf rules, but is it mandatory to use vector? std::set would save 6 bytes. If we are to allow duplicated entries, std::list is still 102 bytes – bznein Dec 3 at 15:22 # R, 35 bytes function(a,b,l)sum(b%/%l-(a-1)%/%l)  Try it online! Implementation of tsh's method by Giuseppe. 3 bytes shorter than the previous version: # R, 45 38 bytes -7 bytes thanks to Nick Kennedy and Xi'an. function(a,b,l)sum(!outer(a:b,l,%%))  Try it online! Boils down to summing the values of !(x %% y) for x in a:b and y in l (%% is $$\mod\$$ in R, so x %% y == 0 iff x is a multiple of y). • Though it removes your first use of Negate, tio.run/##K/… is shorter. – Nick Kennedy Dec 2 at 11:08 • @NickKennedy Yes, of course, thanks! Xian just told me as much… Don't know why I didn't think of it! I guess I was too eager to use Negate. – Robin Ryder Dec 2 at 12:11 • 35 using tsh's method. – Giuseppe 2 days ago # Ruby, 41 bytes ->a,e,l{l.sum{|x|(a..e).count{|r|r%x<1}}}  Try it online! # APL+WIN, 20 15 bytes 5 bytes saved thanks to Adam. Prompts for end integer, start integer and then list: +/,0=⎕∘.|⎕↓0,⍳⎕  Try it online! Courtesy of Dyalog Classic Could be reduced to 9 bytes if we could enter two lists and not have to generate the first: +/,0=⎕∘.|⎕  • +/+/+/, – Adám Dec 2 at 11:50 • s+⍳1+⎕-s←⎕⎕↓0,⍳⎕ – Adám Dec 2 at 11:53 # JavaScript (ES6), 47 bytes Takes input as (list, start)(end). (a,x,s=0)=>g=y=>y<x?s:g(y-1,a.map(c=>y%c||s++))  Try it online! # APL (Dyalog Extended), 9 bytesSBCS Full program, prompting for end, start, list, from stdin. ≢⍸=⎕|\⎕…⎕  Try it online! ⎕ prompt for end ⎕… prompt for start and generate progression vector ⎕|\ prompt for **list* and make division remainder table = Boolean mask indicating where equal to zero ⍸ get the indices of the Trues ≢ tally them # Python 2, 54 bytes f=lambda n,m,l:m/n and sum(n%i<1for i in l)+f(n+1,m,l)  Try it online! • Cool trick using the recursion and division! – justhalf Dec 3 at 7:51 # APL(NARS), chars 13, bytes 26 {+/∊0=⍺∣../⍵}  ⍺ it is the list of divisors, ⍵ it is the range. Test:  1 2{+/∊0=⍺∣../⍵}1 10 15 7 8{+/∊0=⍺∣../⍵}1 800 214 13 5{+/∊0=⍺∣../⍵}301 5000 1301 13 5 3 11{+/∊0=⍺∣../⍵}301 5000 3294  # Jelly, 7 bytes r/ḍ@€FS  Try it online! A dyadic link taking a list of [start, end] as the left argument and the list of possible divisors as the right argument. Returns an integer indicating the total number of possible divisions. • Nice answer! My version was also a 7 byter (tryadic link). – Mr. Xcoder Dec 2 at 18:11 # Bash, 40 bytes eval set !\$[{$1..$2}%{$3}]+;echo$[$*0]  Try it online! # Java (JDK), 115 67 bytes (s,e,d)->{int t=0;for(;s<=e;s++)for(int j:d)t+=s%j<1?1:0;return t;}  48 bytes saved thanks to Olivier Try it online! First time golfing in Java, so this is a new experience for me. # K (oK), 16 bytes {+//~z!\:x_!1+y}  Try it online! ## Explanation: {+//~z!\:x_!1+y} - function with 3 arguments [x;y;z] <- (start; end; list) !1+y - a list 0 to end (inclusive) x_ - drop the first start numbers z!\: - find the modulo of each number in the range start..end with each of list ~ - negate (0 becomes 1, non-zero becomes 0) +// - reduce by addition and converge (repeat until result stops changing)  # J, 22 bytes 1#.1#.0=[|/(}.i.,])/@]  Try it online! ## Explanation: The verbs (functions) in J can take one (right) or two (left and right) arguments. The left argument is the list, the right one - a list of the start and end numbers.  ( )/@ insert the verb in () between the elements of ] the right argument (start and end) , append i. a list 0..end-1 ] to the right argument (end) }. drop start elements |/ insert modulo verb between the above list and [ the left argument (the list) 0= compare each with 0 1#. sum by base 1 conversion 1#. sum by base 1 conversion (we do it twice, because the result of |/ is a table)  • Some explanation would be good – AZTECCO Dec 2 at 21:29 • @AZTECCO I added an explanation of the K answer. I'm going to add the J one soon. – Galen Ivanov Dec 3 at 8:03 # Icon, 59 bytes procedure f(a,b,l) n:=0 (a to b)%!l<1&n+:=1&\z return n end  Try it online! # Python 54 bytes lambda K,M,H:sum(n%l<1for n in range(K,M+1)for l in H)  New contributor JustinMark is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. • Welcome to the site! I've edited your answer to fit the standard format, feel free to edit in any explanations, or links to online testing environments such as Try it online! – caird coinheringaahing Dec 2 at 20:31 # Japt-x, 7 6 bytes rõ ïvV  Try it rõ ïvV :Implicit input of arrays U=[start,end] and V=list > e.g., U=[15,18] V=[2,4,3] r :Reduce U by õ : Inclusive range > [15,16,17,18] ï V :Cartesian product with V > [[15,2],[15,4],[15,3],[16,2],[16,4],[16,3],[17,2],[17,4],[17,3],[18,2],[18,4],[18,3]] v :Reduce each pair by testing the divisibility of the first by the second > [0,0,1,1,1,0,0,0,0,1,0,1] :Implicit output of sum of resulting array > 5  # Charcoal, 11 bytes Ｉ⁻Σ÷ηζΣ÷⊖θζ  Try it online! Link is to verbose version of code. Explanation:  η Second input (End) ζ Third input (List) ÷ Vectorised integer division Σ Sum θ First input (Start) ⊖ Decremented ζ Third input (List) ÷ Vectorised integer division Σ Sum ⁻ Subtract Ｉ Cast to string Implicitly print  # Pyth, 15 bytes /.nm%LdeQ}.*PQ0  Try it online! Takes input as start, end, list Explanation:  } # Inclusive range .* # Splat (list becomes list of arguments) PQ # All but the last element of the input # --> inclusive range from start to end m # map each number of that L eQ # to a map of each member of Q[-1] = list % d # remainder modulo member (2, 4, 3 in example) # --> List of lists of remainders .n # flatten / 0 # count zeroes  • -5 bytes using multiple inputs and pfns – frank yesterday # Perl 5 (-ap), 33 bytes for$i(<>..<>){$i%$_||\$\++for@F}}{


Try it online!

# C# (Visual C# Interactive Compiler), 54 bytes

(a,b,l)=>l.Where(x=>a%x<1).Count()+(++a>b?0:f(a,b,l));


Try it online!

Recursive approach

• It's recursive, but you don't define f. You should define it like this: f=(a,b,l)=>... – Olivier Grégoire Dec 2 at 22:39
• @OlivierGrégoire If it is recursive, you have to include the whole function definition: Func<int,int,IEnumerable<int>,int>f=(a,b,l)=>..., since f=(a,b,l)=>... is not a valid expression (f does not exist yet). However, it would just be shorter to do int f(int a,int b,IEnumerable<int>l)=>... – Embodiment of Ignorance Dec 3 at 4:12
• if you take in int[] instead of IEnumerable<int> you can use Length instead of Count(), removing 1 byte – Ivan García Topete 2 days ago

# SimpleTemplate, 86 bytes

Yeah, kinda long, but it works!
Receives 2 numbers (in any order) and an array/string of 1-digit numbers (e.g.: "243"), passed to the render() method of the compiler.

{@fori fromargv.0 toargv.1}{@eachargv.2}{@ifi is multiple_}{@incR}{@/}{@/}{@/}{@echoR}


Displays the result or nothing, if none is

Ungolfed

{@set count 0}
{@for i from argv.0 to argv.1}
{@each argv.2 as number}
{@if i is multiple of number}
{@inc by 1 count}
{@/}
{@/}
{@/}
{@echo count}


Should be mostly straightforward.

Change line 986 to test the golfed and ungolfed versions, and line 988 to get the input.

## Mathematica, 52 bytes

f[s_,e_,l_]:=(q=Quotient;Total[q[e,#]-q[s-1,#]&/@l])


This uses the simplification formula from @tsh

A less clever solution at 66 bytes but more directly following the OP:

f[s_,e_,l_]:=Total[Length@Intersection[Divisors@#,l]&/@Range[s,e]]