Least Common Multiple

31

3

The least common multiple of a set of positive integers A is the smallest postive integer B such that, for each k in A, there exists a positive integer n such that k*n = B.

Given at least two positive integers as input, output their least common multiple.

Rules

  • Builtins are allowed, but if your solution uses one, you are encouraged to include an alternate solution that does not use GCD/LCM builtins. However, the alternate solution will not count towards your score at all, so it is entirely optional.
  • All inputs and outputs will be within the natively-representable range for your language. If your language is natively capable of arbitrarily-large integers, then your solution must work with arbitrarily large inputs and outputs.

Test cases

[7, 2] -> 14
[8, 1] -> 8
[6, 4, 8] -> 24
[8, 2, 1, 10] -> 40
[9, 6, 2, 1, 5] -> 90
[5, 5, 7, 1, 1] -> 35
[4, 13, 8, 8, 11, 1] -> 1144
[7, 2, 2, 11, 11, 8, 5] -> 3080
[1, 6, 10, 3, 4, 10, 7] -> 420
[5, 2, 9, 10, 3, 4, 4, 4, 7] -> 1260
[9, 7, 10, 9, 7, 8, 5, 10, 1] -> 2520

Mego

Posted 2016-09-30T18:47:53.850

Reputation: 32 998

6Because it's a reasonably frequent misconception: the formula LCM(a,b) = ab/GCD(a,b) does not extend to more than two numbers (or, for that matter, to one number!). – Greg Martin – 2016-10-01T02:56:55.273

Answers

5

Actually, 12 1 byte

Golfing suggestions are still welcome, though I'm not sure how to improve on the raw LCM built-in. Try it online!

A 12-byte version without the built-in. Golfing suggestions welcome. Try it online!

╗2`╜@♀%ΣY`╓N

Ungolfing

          Implicit input array.
╗         Save array in register 0.
2`...`╓   Starting with f(0), find the first (two) x where f(x) returns a truthy value.
          These two values will be 0 and our LCM.
  ╜         Push array from register 0.
  @         Swap the top two values. Stack: x, array
  ♀%        Map % over x and array, returning (x % item) for each item in array.
  ΣY        If the sum of all the modulos equals 0, x is either 0 or our LCM.

N         Push the last (second) value of our results. This is our LCM.
          Implicit return.

Sherlock9

Posted 2016-09-30T18:47:53.850

Reputation: 11 664

You do realize you're allowed to use the builtin, right? – Mego – 2016-10-01T05:39:49.200

1@Mego I'll add it in, but my understanding was that builtins were discouraged, so I didn't use it at first. – Sherlock9 – 2016-10-01T06:16:11.417

1Builtins are allowed. They're not discouraged at all - I simply wanted to encourage non-builtin solutions to also be included because they are often much more interesting than the builtin. – Mego – 2016-10-01T06:30:00.493

1I read that as actually, 1 byte. – programmer5000 – 2017-04-17T18:21:01.353

2@programmer5000 I think that may be why the language is called actually... – Socratic Phoenix – 2017-09-25T20:22:06.823

17

JavaScript (ES6), 36 bytes

f=(a,i=1)=>a.some(v=>i%v)?f(a,i+1):i

Starting from 1 it's the first number that can be divided by all.

f=(a,i=1)=>a.some(v=>i%v)?f(a,i+1):i
;

console.log(f([7, 2]));
console.log(f([8, 1]));
console.log(f([6, 4, 8]));
console.log(f([8, 2, 1, 10]));
console.log(f([9, 6, 2, 1, 5]));
console.log(f([5, 5, 7, 1, 1]));
console.log(f([4, 13, 8, 8, 11, 1]));
console.log(f([7, 2, 2, 11, 11, 8, 5]));
console.log(f([1, 6, 10, 3, 4, 10, 7]));
console.log(f([5, 2, 9, 10, 3, 4, 4, 4, 7]));
console.log(f([9, 7, 10, 9, 7, 8, 5, 10, 1]));

Hedi

Posted 2016-09-30T18:47:53.850

Reputation: 1 857

Of course... I thought about doing a loop with this technique, but recursion is way shorter. – ETHproductions – 2016-09-30T19:26:04.410

1This is genius... If I recall, some returns true if at least one element in the array satisfies the condition, right? – WallyWest – 2016-10-04T02:49:38.310

12

acrolith

Posted 2016-09-30T18:47:53.850

Reputation: 3 728

11

Jelly, 3 bytes

æl/

Reduces by LCM. Try it online! or verify all test cases.

Alternate version, 6 bytes

ÆE»/ÆẸ

Try it online! or verify all test cases.

How it works

ÆE»/ÆẸ  Main link. Argument: A (array)

ÆE      Yield all prime exponents of each integer in A.
  »/    Reduce columns (exponents that correspond to the same prime) by maximum.
    ÆẸ  Turn the resulting array of prime exponents into the corresponding integer.

Dennis

Posted 2016-09-30T18:47:53.850

Reputation: 196 637

8

Python, 69 65 52 50 bytes

A=lambda l,i=1:any(i%a for a in l)and A(l,i+1)or i

2 bytes saved thanks to Dennis!

Pretty straightforward recursive solution, you will need to make the recursion limit a bit higher for some of the test cases to work.

Loovjo

Posted 2016-09-30T18:47:53.850

Reputation: 7 357

1any takes a generator; you don't need the brackets. – Dennis – 2016-09-30T19:10:07.080

3A=lambda l,i=1:all(i%a<1for a in l)or-~A(l,i+1) saves a few more bytes. – Dennis – 2016-09-30T19:58:44.417

8

MATL, 7 bytes

&YFX>^p

No builtin.

Try it online!

Explanation

Let's take input [8, 2, 1, 10] as an example.

&YF    % Take array implicitly. Push vector of prime factors and matrix of exponents 
       % of factorization, where each row represents one of the input numbers
       %   STACK: [2 3 5], [3 0 0; 1 0 0; 0 0 0; 1 0 1]
X>     % Maximum of each column
       %   STACK: [2 3 5], [3 0 1]
^      % Element-wise power
       %   STACK: [8 1 5]
p      % Product of array
       %   STACK: 40
       % Implicitly display

EDIT (June 9, 2017): YF with two outputs has been modified in release 20.1.0: non-factor primes and their (zero) exponents are skipped. This doesn't affect the above code, which works without requiring any changes.

Luis Mendo

Posted 2016-09-30T18:47:53.850

Reputation: 87 464

6

PowerShell v2+, 73 60 bytes

param($a)for($i=1;($a|?{!($i%$_)}).count-ne$a.count){$i++}$i

Takes input $a, loops upward from $i=1 with $i++, based on a conditional. The condition is ($a|?{!($i%$_)}).count being -notequal to $a.count. Meaning, the loop ends when the elements of $a that are divisors of $i is equal to the elements of $a. Then, a solitary $i is left on the pipeline, and output is implicit.

Test Cases

PS C:\Tools\Scripts\golfing> @(7,2),@(8,1),@(6,4,8),@(8,2,1,10),@(9,6,2,1,5),@(5,5,7,1,1),@(4,13,8,8,11,1)|%{($_-join',')+" -> "+(.\least-common-multiple.ps1 $_)}
7,2 -> 14
8,1 -> 8
6,4,8 -> 24
8,2,1,10 -> 40
9,6,2,1,5 -> 90
5,5,7,1,1 -> 35
4,13,8,8,11,1 -> 1144

PS C:\Tools\Scripts\golfing> @(7,2,2,11,11,8,5),@(1,6,10,3,4,10,7),@(5,2,9,10,3,4,4,4,7),@(9,7,10,9,7,8,5,10,1)|%{($_-join',')+" -> "+(.\least-common-multiple.ps1 $_)}
7,2,2,11,11,8,5 -> 3080
1,6,10,3,4,10,7 -> 420
5,2,9,10,3,4,4,4,7 -> 1260
9,7,10,9,7,8,5,10,1 -> 2520

AdmBorkBork

Posted 2016-09-30T18:47:53.850

Reputation: 41 581

6

Julia (3 Bytes) [Working on Non-Built-in]

lcm     # Using LCM built-in (3 Bytes)

As Dennis pointed out, I keep forgetting that Julia automatically vectorizes inputs.

Example:

println(lcm(1,2,3,4,5,6,7,8,9)) #Prints 2520

Magic Octopus Urn

Posted 2016-09-30T18:47:53.850

Reputation: 19 422

4

JavaScript (ES6), 63 59 bytes

f=([x,...a])=>a[0]?x*f(a)/(g=(m,n)=>n?g(n,m%n):m)(x,f(a)):x

Recursively finds the LCM of the last two elements.

ETHproductions

Posted 2016-09-30T18:47:53.850

Reputation: 47 880

This is what my solution would have been: a=>a.reduce((l,n)=>l*n/(g=(m,n)=>n?g(n,m%n):m)(l,n)) – Neil – 2016-09-30T19:15:10.863

@Neil You can post that if you'd like. I doubt my technique can get that short... – ETHproductions – 2016-09-30T19:32:30.080

4

Mathematica, 3 bytes

LCM

Usage:

In[1]:= LCM[9, 7, 10, 9, 7, 8, 5, 10, 1]                                        

Out[1]= 2520

alephalpha

Posted 2016-09-30T18:47:53.850

Reputation: 23 988

6The day that Mathematica matched Jelly is a day I never thought I'd see. – Steven H. – 2016-10-01T03:28:09.843

3

Dyalog APL, 2 bytes

∧/

Reduces by LCM. Test it on TryAPL.

Dennis

Posted 2016-09-30T18:47:53.850

Reputation: 196 637

4Congrats on 100k! – Copper – 2016-09-30T20:45:16.613

3

Cheddar, 33 bytes

(n,i=1)f->n.any(i&(%))?f(n,i+1):i

Nothing super new.

Ungolfed

(n, i = 1) f ->
  n.any(j -> i % j) ?
    f(n, i + 1) :
    i

Basically this starts at one and keeps increasing until it finds an LCM

Downgoat

Posted 2016-09-30T18:47:53.850

Reputation: 27 116

3

Java 8, 75 59 121 89 bytes

Uses the Euclidean Algorithm and the fact that LCM(A, B)= A * B / GCD(A, B)

  • 16 bytes off. Thanks to @carusocomputing
  • Added Multi-Input +62 bytes
  • 32 bytes off. Thanks to @Olivier Grégoire

Code:

public static int lcm(int l, int c){
  for(int i=1;i<=l&&i<=c;++i) 
    if (i%l==0&&i%c==0)
      return l*c/i;
}
public static int lcm(int...x){
  int y=x[0];
  for(int j:x){
    y=lcm(j,y);
  }
  return y;
}

Remove line-breaks:

int g(int a,int b){return b<1?a:g(b,a%b);}

l->{int l=1;for(int n:a)l=l*n/g(l,n);return l;}

Roman Gräf

Posted 2016-09-30T18:47:53.850

Reputation: 2 915

Technically a snippet, but if you add n->{...} I believe it becomes valid Java 8. – Magic Octopus Urn – 2016-09-30T20:27:34.310

Thanks. I'm trying to get used to see lambda in Java. With lambda you can probably golf some of the for-loop. But I don't know how. – Roman Gräf – 2016-09-30T20:29:28.140

Yeah, all that stuff is an afterthought in Java; you'd likely be better off learning it in Python :). – Magic Octopus Urn – 2016-09-30T20:32:21.377

Unless I'm missing something, this doesn't support more than two inputs – pinkfloydx33 – 2016-10-01T16:51:20.983

If you compute the GCD, you can golf much more: int g(int a,int b){return b<1?a:g(b,a%b);}. LCM can then become int l(int[]a){int l=1;for(int n:a)l=l*n/g(l,n);return l;}, for a total of 99 bytes. – Olivier Grégoire – 2016-10-03T09:47:42.887

Your declarations are totally wrong with your try to include my updates. You have simply golfed for Java 8 without checking if anything would compile. If you had compiled, you'd have seen that you need to fully declare int g(int a,int b) and then use it in a Java 8 version of the lcm. Reason: you can't declare g=(a,b)->{} and use it as g(a,b): you can only use it as g.applyAsInt(a,b). That's why I fully declared the function. TLDR: your code doesn't compile. – Olivier Grégoire – 2016-10-03T11:20:24.827

3

JavaScript (ES6), 52 bytes

a=>a.reduce((l,n)=>l*n/(g=(m,n)=>n?g(n,m%n):m)(l,n))

I reduced this answer as much as I could but I'm obviously not going to get anywhere near the simplicity of @Hedi's answer.

Neil

Posted 2016-09-30T18:47:53.850

Reputation: 95 035

2

R, 36 bytes (not builtin)

v=scan();i=1;while(any(i%%v))i=i+1;i

Takes the input. Then tests each positive integer by taking the mod.

user5957401

Posted 2016-09-30T18:47:53.850

Reputation: 699

I believe you need a cat around your last i – Giuseppe – 2017-10-13T20:10:40.663

@Giuseppe when I run it, the value prints fine. – user5957401 – 2017-10-13T20:19:29.893

see the discussion here, but I suppose ec=T is fine for +4 rather than +5 for cat(). – Giuseppe – 2017-10-13T20:22:11.843

1regardless, this can be golfed down some v=scan();while(any((F=F+1)%%v)){};F with cat() or ec=T making it 40 or 39 bytes, respectively. And +1, very nice approach. – Giuseppe – 2017-10-13T20:23:38.353

2

MATL, 3 bytes

&Zm

This uses the builtin function with array input.

Try it online!

Luis Mendo

Posted 2016-09-30T18:47:53.850

Reputation: 87 464

2

Brachylog, 17 bytes

,.#>=g:?z:%a#=h0,

Try it online!

Explanation

,.#>=               Output is a strictly positive integer
     g:?z           Zip the Output with the Input
         :%a        Compute Output mod I for each I in the Input
            #=h0,   All results must be equal to 0

Fatalize

Posted 2016-09-30T18:47:53.850

Reputation: 32 976

2

Perl 6, 10 bytes

{[lcm] @_}

basically the same as:

sub ( *@_ ) { @_.reduce: &infix:< lcm > }

Brad Gilbert b2gills

Posted 2016-09-30T18:47:53.850

Reputation: 12 713

2

J, 11 bytes

>./&.(_&q:)

There is a solution for 3 bytes using the LCM builtin.

*./

Explanation

>./&.(_&q:)  Input: array of integers A
      _&q:   Get the prime exponents of each integer in A
>./&         Reduce by maximum on the lists
   &. _&q:   Convert the list of exponents back to an integer

*./  Input: array of integers A
  /  Reduce using
*.     LCM

miles

Posted 2016-09-30T18:47:53.850

Reputation: 15 654

2

Racket 13 bytes

lcm is a built-in function in Racket:

(apply lcm l)

Testing:

(define (f l)
   (apply lcm l))

(f (list 7 2)) 
(f (list 8 1)) 
(f (list 6 4 8)) 
(f (list 8 2 1 10)) 
(f (list 9 6 2 1 5))
(f (list 5 5 7 1 1)) 
(f (list 4 13 8 8 11 1))
(f (list 7 2 2 11 11 8 5))
(f (list 1 6 10 3 4 10 7))
(f (list 5 2 9 10 3 4 4 4 7)) 
(f (list 9 7 10 9 7 8 5 10 1))

Output:

14
8
24
40
90
35
1144
3080
420
1260
2520

rnso

Posted 2016-09-30T18:47:53.850

Reputation: 1 635

Ahh. How can you use that syntax. I always gave up when I tried to learn Racket. – Roman Gräf – 2016-10-01T17:22:11.827

1First word in brackets is a procedure name, rest are its arguments. If an argument is a procedure, it has to be in its own brackets. Values (non-procedures) are written without brackets. I find it to be an excellent general-purpose language with additional advantage of stress on functional programming. Being derived from Lisp, one also gets a sense of covering that area of programming. – rnso – 2016-10-01T18:19:27.693

I find coding keywords and language to be easier in Racket & Scheme than Lisp. – rnso – 2016-10-01T18:26:01.307

Yes, but did I said I understand Lisp? I more like languages like Jelly or Java. – Roman Gräf – 2016-10-01T18:27:40.070

1Main syntax difference between Java and Racket is f(a, b) vs (f a b), x+y+z vs (+ x y z), x == y vs (eq? x y) and x=2 vs (define x 2), or if already defined, (set! x 2). Also no need to declare types like public static void or int char string etc. Hope that gets you interested in Racket again. – rnso – 2016-10-02T03:53:09.420

Clojure https://en.wikipedia.org/wiki/Clojure is combination of Lisp and Java.

– rnso – 2016-10-02T04:02:07.813

2

CJam, 18 17 16 bytes

1 byte saved thanks to Martin Ender.

Incrementing until the LCM is found.

q~0{)_2$f%:+}g\;

Try it online

Neorej

Posted 2016-09-30T18:47:53.850

Reputation: 179

1I'm not entirely familiar with CJam, but the reusability rule is for functions, not full programs. If your 17-byte solution is a full program that consistently works across runs, it's fine. – Mego – 2016-10-02T17:31:55.047

1

Prolog (SWI), 46 bytes

l([],1).
l([X|Y],P):-l(Y,Q),P is X*Q/gcd(X,Q).

Try it online!

Another solution, 59 bytes:

l(A,X):-between(1,inf,X),forall(member(Y,A),X mod Y=:=0),!.

eush77

Posted 2016-09-30T18:47:53.850

Reputation: 1 280

1

Python 3, 83 bytes

import math,functools as i
t=lambda t:i.reduce(lambda a,b:int(a*b/math.gcd(a,b)),t)

Hydreigos

Posted 2016-09-30T18:47:53.850

Reputation: 11

Welcome to PPCG! – Laikoni – 2018-07-10T17:36:53.323

You may want to include a link to an online testing site like Try it online! so it's easier for others to verify your answer.

– Laikoni – 2018-07-10T17:40:50.983

1

Brachylog v2, 8 bytes

{×↙Xℕ₁}ᵛ

Try it online!

It's funny just how directly this maps on to the definition given in the challenge.

{     }ᵛ    Each element of
            the input
 ×          multiplied by
  ↙X        some arbitrary and inconsistent integer
    ℕ₁      is a natural number,
       ᵛ    which is the same for each element,
            and is the output.

A suspiciously slow but significantly shorter solution:

Brachylog v2, 5 bytes

f⊇p~d

Try it online!

Takes input through the output variable and gives output through the input variable. Rips right through the first four test cases but I'm still waiting on the fifth... Ordinarily, I'd still make it my primary solution and just trust that it works correctly, but I don't know why it hasn't confirmed that 90 is the LCM of 9, 6, 2, 1, 5 when I gave it 90 twenty minutes ago.

(Edit: It confirmed the answer after no more than 16 hours, and generated it alongside the LCM of 5, 5, 7, 1, 1 after about two days.)

         The output variable
   ~d    with duplicates removed
  p      is a permutation of
 ⊇       a sublist of
f        the factors of
         the input variable.

And another completely different predicate that accidentally more-or-less translates Fatalize's Brachylog v1 solution:

Brachylog v2, 10 bytes

;.gᵗ↔z%ᵛ0<

Try it online!

This was salvaged from a solution I'd made for this challenge before I realized the output wasn't restricted to being an integer.

 .            The output
; gᵗ↔z        paired with each element of
              the input,
      %ᵛ      when the first element of each pair is taken mod the second, is always
        0     zero.
              Furthermore, the output
         <    is strictly greater than
        0     zero.

Unrelated String

Posted 2016-09-30T18:47:53.850

Reputation: 5 300

1

Pyth, 9 bytes

.U/*bZibZ

A program that takes input of a list on STDIN and prints the result.

Try it online or verify all test cases

How it works

.U/*bZibZ  Program. Input: Q
.U         Reduce Q by (implicit input fill):
   *bZ      Product of current and next value
  /   ibZ   divided by GCD of current and next value
           Implicitly print

TheBikingViking

Posted 2016-09-30T18:47:53.850

Reputation: 3 674

1

Haskell, 10 bytes

foldr1 lcm

Usage example: foldl1 lcm [5,2,9,10,3,4,4,4,7] -> 1260.

nimi

Posted 2016-09-30T18:47:53.850

Reputation: 34 639

1

C#, 50+18 = 68 bytes

50 bytes for method defintion, +18 bytes for LINQ import.

using System.Linq;int L(int[]n,int i=1)=>n.All(x=>1>i%x)?i:L(n,i+1);

Pretty much the same as a lot of other answers. Counts up recursively until it finds the LCM. I was a bit surprised this didn't get a StackOverflowException, so I also have a non-recursive version which is actually just 1 byte longer.

using System.Linq;n=>{for(int i=1;;i++)if(n.All(x=>1>i%x))return i;};

Ungolfed:

using System.Linq;            // Import LINQ
int L(int[] n, int i = 1) =>  // Function declaration
    n.All(x => 1 > i % x)     // Check if each x in n divides i
        ? i                   // And if so return i
        : L(n, i + 1)         // Otherwise increment i and recurse
;

milk

Posted 2016-09-30T18:47:53.850

Reputation: 3 043

1

PHP, 42 74 bytes

for(;($p=++$f*$argv[1])%$argv[2];);echo$p;

straight forward:
loop $f from 1 upwards; if $f*$a divides through $b without a remainder, the LCM is found.


I totally had overread the at least ... here´s the code for any number of parameters:

for(;$i<$argc;)for($p=$argv[$i=1]*++$f;++$i<$argc&$p%$argv[$i]<1;);echo$p;

Loop $f from 1 upwards while inner loop has not run to $argc.
Loop $i from 2 to $argc-1 while $f*$argv[1] divides through $argv[$i] without a remainder.
both loops broken: print $f*$argument 1.

Titus

Posted 2016-09-30T18:47:53.850

Reputation: 13 814

1

Pip, 10 bytes

W$+o%g++oo

Uses the "try every number until one works" strategy. Try it online!

            o is preinitialized to 1, g is list of cmdline args
   o%g      Mod o by each arg
 $+         Sum (truthy if any nonzero, falsy if all zero)
W           Loop while that expression is truthy:
      ++o     Increment o
         o  Autoprint o

DLosc

Posted 2016-09-30T18:47:53.850

Reputation: 21 213

0

R, 30 21 bytes

Using the mLCM function from the numbers package:

numbers::mLCM(scan())

Or without builtins (81 bytes):

L=function(x,y,g=function(x,y,r=x%%y)`if`(r,g(y,r),y))x*y/g(x,y)
Reduce(L,scan())

We first define a function L for the pair-wise LCM and then apply it iteratively using Reduce.

Billywob

Posted 2016-09-30T18:47:53.850

Reputation: 3 363

How would this work for testcases with more than 2 numbers as input? – JAD – 2016-12-25T13:32:33.547

@JarkoDubbeldam Thanks for pointing that out. Missed that part of the spec. – Billywob – 2016-12-25T14:23:20.160

0

Axiom, 19 bytes

f(x)==reduce(lcm,x)

Could be perhaps better and right but longer

f(x:List PI):PI==reduce(lcm,x)

RosLuP

Posted 2016-09-30T18:47:53.850

Reputation: 3 036

0

J, 27 bytes

not using built ins, naive solution

>:0 i.~+/"1 i|"1 0>:i.*/i=.

usage

>:0 i.~+/"1 i|"1 0>:i.*/i=.9 6 2 1 5

returns 90

protist

Posted 2016-09-30T18:47:53.850

Reputation: 570

0

Japt, 8 bytes

@e!vX}a1

Try it

Shaggy

Posted 2016-09-30T18:47:53.850

Reputation: 24 623

0

Perl 5, 52 + 2 (-pa) = 54 bytes

$r=$\=$_;while($r){$r=0;$r||=$\%$_ for@F;$r&&$\++}}{

Try it online!

Xcali

Posted 2016-09-30T18:47:53.850

Reputation: 7 671

0

A little late to the party, but I didn't see an AWK answer.

AWK, 49 bytes

{for(x=1;x&&++m;)for(x=i=0;i++<NF;)x+=m%$i;$0=m}1

Try it online!

NOTE: The link has 2 extra bytes m= in front of the x=1;x... to simplify testing/allow multiple inputs/outputs. However, the m= fails for single/repeated input value of 1. The code as written handles 1 fine.

Robert Benson

Posted 2016-09-30T18:47:53.850

Reputation: 1 339

0

Jq 1.5, 67 bytes

Not very smart. Just tests each i from 2 to product of numbers.

first(range(2;1+reduce.[]as$x(1;.*$x))as$i|select(all($i%.==0))|$i)

More efficient but longer:

def g(a;b):if b<1then a else g(b;a%b)end;reduce.[]as$x(1;.*$x/g(.;$x))

Try it online!

jq170727

Posted 2016-09-30T18:47:53.850

Reputation: 411

0

J, 34 bytes

*/@((0 1+])^:(*@+/@:|*/)^:_>./,1:)

A while loop iterating through multiples of the largest number in input.

Try it online!

FrownyFrog

Posted 2016-09-30T18:47:53.850

Reputation: 3 112

0

Scala, 44 bytes

Stream.from(1).find(i=>x.forall(i%_==0)).get

Try it online!

cubic lettuce

Posted 2016-09-30T18:47:53.850

Reputation: 181

0

Pyth, 7 bytes

f!s%LTQ

Try it online!

Sok

Posted 2016-09-30T18:47:53.850

Reputation: 5 592

0

Pyth - 7 6 bytes

No builtin.

*F{sPM

Try it online here.

Maltysen

Posted 2016-09-30T18:47:53.850

Reputation: 25 023

4Fails on [4], or anything else with a repeated prime factor. – isaacg – 2016-09-30T20:08:45.373

0

PHP, 65 Bytes

<?for($t=1;$t&&++$i;$t=!$t)foreach($_GET as$v)$t*=$i%$v<1;echo$i;

Try it online!

or

for(;++$i;!$t?:die("$i"))for($n=$t=1;$v=$argv[$n++];)$t*=$i%$v<1;

or

<?for($t=1;++$i;$t=!$t?:die("$i"))foreach($_GET as$v)$t*=$i%$v<1;

PHP, 96 Bytes

with 2 loops

while(1<count($a=&$_GET[i])){$e=array_pop($a);for($i=0;($m=++$i*$e)%$a[0];);$a[0]=$m;}echo$a[0];

based on set theory 128 Bytes

$p=array_product($a=$_GET[i]);echo min(array_intersect(...array_map($r=function($n)use($p){return range($n,$p,$n);},$_GET[i])));

Jörg Hülsermann

Posted 2016-09-30T18:47:53.850

Reputation: 13 026

0

Java 7, 75 bytes

int f(int[]a){int i=1,s=1;for(;s>0;i++){s=0;for(int b:a)s+=i%b;}return--i;}

A simple two-loop method. Start at one and check to see when all the remainders add to zero.

I had to jump through some dumb hoops with s. There's probably a better way to do that, but it's 1:00 AM and it'll have to wait :/

Geobits

Posted 2016-09-30T18:47:53.850

Reputation: 19 061