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

• Nice challenge! Presumably the winning criterion is shortest code? In which case you should say so explicitly, and add the code-golf tag. Dec 2 '19 at 10:16
• Can we assume that start will always be less than end? Dec 2 '19 at 11:49
• You say integers. So start, end or the elements in list may be non-positive?
– Seb
Dec 3 '19 at 11:17
• Thanks for the feedback, I was lacking that in the problem. Dec 3 '19 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? Dec 6 '19 at 8:52

# 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


(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 '19 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.) Dec 3 '19 at 15:45 # 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 '19 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 '19 at 9:55 # 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! # 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 '19 at 11:50 • s+⍳1+⎕-s←⎕⎕↓0,⍳⎕ – Adám Dec 2 '19 at 11:53 # 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. Dec 2 '19 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. Dec 2 '19 at 12:11 • 35 using tsh's method. Dec 3 '19 at 21:08 # C++ (clang), 113110109108106100939991 89 bytes using I=int;I s;void f(I a,I b,I*l,I z,I&s){for(s=0;a<=b;++a)for(I i=z;i--;)s+=a%l[i]<1;}  Try it online! Saved 3 bytes thanks to @AZTECCO!!! Saved 6 13 bytes thanks to @bznein!!! Added 6 Saved 8 bytes thanks to @AZTECCO (and now it's playing by the rules)!!! • You can use #import<vector> to save 1 Dec 2 '19 at 15:23 • @AZTECCO Thanks, didn't know about #import. :-) Dec 2 '19 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; Dec 2 '19 at 20:58 • @AZTECCO Ah, good to see Microsoft doing something useful for the code-golfing community. Thanks for the -2! :-) Dec 2 '19 at 21:12 • @AZTECCO So many ints! Managed to save 2 bytes using using. :-) Dec 8 '19 at 16:09 # Ruby, 41 bytes ->a,e,l{l.sum{|x|(a..e).count{|r|r%x<1}}}  Try it online! # 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! # 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 Dec 2 '19 at 21:29 • @AZTECCO I added an explanation of the K answer. I'm going to add the J one soon. Dec 3 '19 at 8:03 # 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! Dec 3 '19 at 7:51 # Bash, 40 bytes eval set !\$[{$1..$2}%{$3}]+;echo$[$*0]  Try it online! # 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). Dec 2 '19 at 18:11 # 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. • Well, you can declare lambda methods: (s,e,d)->{...}. Also, you can remove spaces and probably use a few golfing tips. Here's my take on golfing your answer. Dec 2 '19 at 22:30 # 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)  • 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! Dec 2 '19 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  # Kotlin, 69 bytes {s:Int,e:Int,l:List<Int>->(s..e).toList().sumBy{v->l.count{v%it==0}}}  Try it online! # Husk, 6 bytes #0×%…  Try it online! Nothing very special. # 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 Dec 4 '19 at 23:12 # 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)=>... Dec 2 '19 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)=>... Dec 3 '19 at 4:12
• if you take in int[] instead of IEnumerable<int> you can use Length instead of Count(), removing 1 byte Dec 3 '19 at 18:15

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


# Excel VBA, 77 Bytes

Immediate window function that takes start from [A1], end from [B1], and list from column C ([C:C]). Output is directed to the immediate window.

[C:C].SpecialCells(2) is used to iterate j over only the cells in Column C which contain a value, as non-valued cells produce an error.

For i=[A1]To[B1]:For Each j In[C:C].SpecialCells(2):s=s-(i/j=i\j):Next j,i:?s
`