How many times, are they multiples?

34

3

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

Rui Silva

Posted 2019-12-02T09:53:09.587

Reputation: 401

8Nice challenge! Presumably the winning criterion is shortest code? In which case you should say so explicitly, and add the [tag:code-golf] tag. – Robin Ryder – 2019-12-02T10:16:44.887

9Can we assume that start will always be less than end? – Galen Ivanov – 2019-12-02T11:49:28.517

You say integers. So start, end or the elements in list may be non-positive? – Seb – 2019-12-03T11:17:53.697

Thanks for the feedback, I was lacking that in the problem. – Rui Silva – 2019-12-03T14:43:48.607

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 – 2019-12-06T08:52:01.907

Answers

10

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

Expired Data

Posted 2019-12-02T09:53:09.587

Reputation: 3 129

10

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?

tsh

Posted 2019-12-02T09:53:09.587

Reputation: 13 072

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 – 2019-12-03T09:50:19.563

1@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 – 2019-12-03T09:55:05.867

9

Haskell, 33 bytes

(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

flawr

Posted 2019-12-02T09:53:09.587

Reputation: 40 560

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 – 2019-12-03T09:58:26.733

@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 – 2019-12-03T15:45:23.780

7

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

Jo King

Posted 2019-12-02T09:53:09.587

Reputation: 38 234

7

MATL, 5 bytes

:i\~z

Inputs are a cell array containing the start and end values, and then a column vector defining list.

Try it online! Or verify all test cases.

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

Luis Mendo

Posted 2019-12-02T09:53:09.587

Reputation: 87 464

6

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!

Henry T

Posted 2019-12-02T09:53:09.587

Reputation: 381

5

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

Robin Ryder

Posted 2019-12-02T09:53:09.587

Reputation: 6 625

Though it removes your first use of Negate, https://tio.run/##K/qfpmD7P600L7kkMz9PI1EnSSdHs7g0V0Mxv7QktUgj0QoooJOgqpqgqfk/TcNQR8HQAIitjDS5wDwLAyDX3MoCxDU2AAqYGoBEkjUMjYFsoB4A is shorter.

– Nick Kennedy – 2019-12-02T11:08:24.657

@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 – 2019-12-02T12:11:26.613

35 using tsh's method. – Giuseppe – 2019-12-03T21:08:16.220

5

C++ (clang), 113 110 109 108 106 100 93 99 91 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)!!!

Noodle9

Posted 2019-12-02T09:53:09.587

Reputation: 2 776

You can use #import<vector> to save 1 – AZTECCO – 2019-12-02T15:23:59.447

@AZTECCO Thanks, didn't know about #import. :-) – Noodle9 – 2019-12-02T17:42:16.943

#include is standard.. See https://stackoverflow.com/a/172264/7699953 btw you can save 2 by using s+=a%i<1; instead of s+=a%i?0:1;

– AZTECCO – 2019-12-02T20:58:59.803

@AZTECCO Ah, good to see Microsoft doing something useful for the code-golfing community. Thanks for the -2! :-) – Noodle9 – 2019-12-02T21:12:20.937

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 – 2019-12-03T15:22:19.650

@bznein Good point! No, vector isn't mandatory. It's more usual to allow you to input and output as suits you best. Thanks! :-) – Noodle9 – 2019-12-03T15:28:14.987

If we can choose also output as we wish, can we use int&s as parameter to the function? #import<set> void f(int a,int b,std::set<int>l,int&s){for(;a<=b;++a)for(int i:l)s+=a%i<1;} is 90 bytes.

This will however require us to set s to 0 before calling the function, which might not follow the rules, but even initializing s inside the function leads to 94 bytes: #import<set> void f(int a,int b,std::set<int>l,int&s){s=0;for(;a<=b;++a)for(int i:l)s+=a%i<1;} – bznein – 2019-12-03T16:20:23.577

@bznein Ingenious! Totally valid and oft used way to return data from a C++ function, so I'm guessing it's ok? – Noodle9 – 2019-12-03T17:08:09.417

It's valid : https://codegolf.meta.stackexchange.com/a/4942/84844

– AZTECCO – 2019-12-08T11:23:27.683

@AZTECCO Thanks for the clarification, that's then another way to snip the lettery return! – Noodle9 – 2019-12-08T11:50:19.147

Yes, anyway I'm not sure but I think you have to include declaration of s ''int s;'' in the bytecount – AZTECCO – 2019-12-08T12:49:38.720

Sounds like the right thing to do, otherwise you could initialise it outside the byte count too. Still shorter! :-) – Noodle9 – 2019-12-08T13:56:24.530

Suggestion : if you don't need to use a particular algorithm or many containers, it's better to use plain arrays most of the times. 85+ 6??

– AZTECCO – 2019-12-08T14:45:21.163

Nice! Was using a standard container for the for (int i:l) but this is shorter! :-) – Noodle9 – 2019-12-08T15:14:38.277

1@AZTECCO So many ints! Managed to save 2 bytes using using. :-) – Noodle9 – 2019-12-08T16:09:20.677

4

Ruby, 41 bytes

->a,e,l{l.sum{|x|(a..e).count{|r|r%x<1}}}

Try it online!

G B

Posted 2019-12-02T09:53:09.587

Reputation: 11 099

4

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=⎕∘.|⎕

Graham

Posted 2019-12-02T09:53:09.587

Reputation: 3 184

+/+/+/, – Adám – 2019-12-02T11:50:53.540

s+⍳1+⎕-s←⎕⎕↓0,⍳⎕ – Adám – 2019-12-02T11:53:09.057

4

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!

Arnauld

Posted 2019-12-02T09:53:09.587

Reputation: 111 334

4

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)

Galen Ivanov

Posted 2019-12-02T09:53:09.587

Reputation: 13 815

1Some explanation would be good – AZTECCO – 2019-12-02T21:29:37.403

1@AZTECCO I added an explanation of the K answer. I'm going to add the J one soon. – Galen Ivanov – 2019-12-03T08:03:18.260

3

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!

Jitse

Posted 2019-12-02T09:53:09.587

Reputation: 3 566

Cool trick using the recursion and division! – justhalf – 2019-12-03T07:51:26.263

3

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

Adám

Posted 2019-12-02T09:53:09.587

Reputation: 37 779

3

Bash, 40 bytes

eval set !\$[{$1..$2}%{$3}]+;echo $[$*0]

Try it online!

Nahuel Fouilleul

Posted 2019-12-02T09:53:09.587

Reputation: 5 582

3

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

RosLuP

Posted 2019-12-02T09:53:09.587

Reputation: 3 036

2

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.

Nick Kennedy

Posted 2019-12-02T09:53:09.587

Reputation: 11 829

1

Nice answer! My version was also a 7 byter (tryadic link).

– Mr. Xcoder – 2019-12-02T18:11:50.047

2

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.

Lyxal

Posted 2019-12-02T09:53:09.587

Reputation: 5 253

1

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.

– Olivier Grégoire – 2019-12-02T22:30:42.143

1

Icon, 59 bytes

procedure f(a,b,l)
n:=0
(a to b)%!l<1&n+:=1&\z
return n
end

Try it online!

Galen Ivanov

Posted 2019-12-02T09:53:09.587

Reputation: 13 815

1

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

Shaggy

Posted 2019-12-02T09:53:09.587

Reputation: 24 623

1

Python 54 bytes

lambda K,M,H:sum(n%l<1for n in range(K,M+1)for l in H)

JulianMota

Posted 2019-12-02T09:53:09.587

Reputation: 11

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 – 2019-12-02T20:31:02.497

0

Charcoal, 11 bytes

I⁻Σ÷ηζΣ÷⊖θζ

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
I           Cast to string
            Implicitly print

Neil

Posted 2019-12-02T09:53:09.587

Reputation: 95 035

0

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

ar4093

Posted 2019-12-02T09:53:09.587

Reputation: 531

-5 bytes using multiple inputs and pfns – frank – 2019-12-04T23:12:55.207

0

Perl 5 (-ap), 33 bytes

for$i(<>..<>){$i%$_||$\++for@F}}{

Try it online!

Nahuel Fouilleul

Posted 2019-12-02T09:53:09.587

Reputation: 5 582

0

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

Marix

Posted 2019-12-02T09:53:09.587

Reputation: 21

1It's recursive, but you don't define f. You should define it like this: f=(a,b,l)=>... – Olivier Grégoire – 2019-12-02T22:39:53.507

@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 – 2019-12-03T04:12:11.100

1if you take in int[] instead of IEnumerable<int> you can use Length instead of Count(), removing 1 byte – Ivan García Topete – 2019-12-03T18:15:47.097

0

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.


You can try it on http://sandbox.onlinephpfunctions.com/code/b26cd7f75e0c4676038573c4274180891a890895

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

Ismael Miguel

Posted 2019-12-02T09:53:09.587

Reputation: 6 797

0

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

Ian Johnson

Posted 2019-12-02T09:53:09.587

Reputation: 41

0

Kotlin, 69 bytes

{s:Int,e:Int,l:List<Int>->(s..e).toList().sumBy{v->l.count{v%it==0}}}

Try it online!

JohnWells

Posted 2019-12-02T09:53:09.587

Reputation: 611