Illustrate the Least Common Multiple

51

2

Given two positive integers, A and B, illustrate their least common multiple by outputting two lines of dashes (-) with length LCM(A, B) after replacing every Ath dash in the first line and every Bth dash in the second line with vertical bars (|).

In this way, the end of each line will be the only place two |'s line up.

For example, if A = 6 and B = 4, LCM(6, 4) = 12, so:

two lines of 12 dashes:
------------
------------

replace every 6th dash in the first line with a vertical bar:
-----|-----|
------------

replace every 4th dash in the second line with a vertical bar:
-----|-----|
---|---|---|

Thus the final output would be

-----|-----|
---|---|---|

The order of the input numbers should correspond to the order of the lines.

The shortest code in bytes wins.

Testcases

A B
line for A
line for B

1 1
|
|

1 2
||
-|

2 1
-|
||

2 2
-|
-|

6 4
-----|-----|
---|---|---|

4 6
---|---|---|
-----|-----|

2 3
-|-|-|
--|--|

3 2
--|--|
-|-|-|

3 6
--|--|
-----|

2 5
-|-|-|-|-|
----|----|

4 3
---|---|---|
--|--|--|--|

10 10
---------|
---------|

10 5
---------|
----|----|

10 6
---------|---------|---------|
-----|-----|-----|-----|-----|

24 8
-----------------------|
-------|-------|-------|

7 8
------|------|------|------|------|------|------|------|
-------|-------|-------|-------|-------|-------|-------|

6 8
-----|-----|-----|-----|
-------|-------|-------|

13 11
------------|------------|------------|------------|------------|------------|------------|------------|------------|------------|------------|
----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|

Calvin's Hobbies

Posted 2017-09-25T18:40:10.173

Reputation: 84 000

3

@LeakyNun Extending an answer from https://codegolf.stackexchange.com/q/94999 seems easier than from that one. Either way, people will have fun doing this one which is a decent reason imo.

– Calvin's Hobbies – 2017-09-25T19:02:49.083

1Can I output an array with two strings, one for each line? – BlackCap – 2017-09-25T19:05:19.153

@BlackCap No. Print the strings to stdout or a file or return the whole multiline string. – Calvin's Hobbies – 2017-09-25T19:06:37.397

2Bonus for handling arbitrary number of inputs? – Adám – 2017-09-25T23:49:04.073

Is a leading new-line allowed if the rest of the output is correct? – Kevin Cruijssen – 2017-09-27T17:25:09.377

@KevinCruijssen that sounds ok. – Calvin's Hobbies – 2017-09-27T18:44:33.587

1@HelkaHomba Ok thanks; saved 1 more byte. :) (As if there is any other reason to ask such questions on codegolf challenges. ;p) – Kevin Cruijssen – 2017-09-28T06:58:11.673

Answers

11

Python 3, 80 bytes

Saved 1 byte thanks to Halvard Hummel and 1 byte thanks to Jonathan Allan.

import math
def f(*l):
 for k in 0,1:print(l[~k]//math.gcd(*l)*(~-l[k]*"-"+"|"))

Test it online!

lambda*l:"\n".join(l[0]*l[1]//math.gcd(*l)//k*(~-k*"-"+"|")for k in l)
import math

Test it online! (82 bytes - initial answer)

This is the best I could do in Python 2 (81 bytes). It seems like I cannot comment on that answer, I'll just post this here instead:

from fractions import*
l=a,b=input()
for k in l:print a*b/gcd(*l)/k*(~-k*"-"+"|")

Test it online!

First attempt here, probably sub-optimal!

user74686

Posted 2017-09-25T18:40:10.173

Reputation:

2Welcome to PPCG! – Laikoni – 2017-09-25T19:46:11.517

3@Laikoni Thank you! This seems like a fun community :-) – None – 2017-09-25T19:47:06.897

81 bytes – Halvard Hummel – 2017-09-25T20:02:12.133

@HalvardHummel Thanks, will post as an alternative! – None – 2017-09-25T20:03:33.967

While trying a diferent approach entirely I realised your version could be done in 80.

– Jonathan Allan – 2017-09-25T21:20:23.903

@JonathanAllan Thanks a lot! – None – 2017-09-26T04:35:55.517

10

Haskell, 57 bytes

x%y=unlines[["-|"!!(0^mod a b)|a<-[1..lcm x y]]|b<-[x,y]]

Try it online!

Laikoni

Posted 2017-09-25T18:40:10.173

Reputation: 23 676

I've never seen that 0^0=1 trick before - clever – BlackCap – 2017-09-25T19:36:47.297

@BlackCap I can't claim it myself because I have seen it a few times before, though I don't remember where I saw the trick first. – Laikoni – 2017-09-25T19:42:48.233

7

Jelly, 12 bytes

Ṭ€ị⁾|-ṁ€æl/Y

Try it online!

Erik the Outgolfer

Posted 2017-09-25T18:40:10.173

Reputation: 38 134

6

MATL, 16 15 bytes

'-|'!i&Zm:G\go)

Input is a column vector with the two numbers. Try it online!

As a bonus, the input can contain more than two numbers. Try it online!

Explanation

'-|'   % Push this string
!      % Transpose. This is needed because of input [1; 1]
i      % Input column vector of 2 (or N) numbers
&Zm    % LCM of the 2 (or N) numbers, say L
:      % Range
G      % Push input again
\      % Modulus, element-wise with broadcast. Gives a 2×L (or N×L) matrix
g      % Convert to logical: gives false for zeros, true for nonzeros
o      % Convert to double: gives 0 for false, 1 for true
)      % Index into string (modular, 1-based). Implicitly display

Luis Mendo

Posted 2017-09-25T18:40:10.173

Reputation: 87 464

I think you left in a stray He? – Sanchises – 2017-09-28T08:27:10.907

@Sanchises Thanks! Yes, it was in the previous version, but it's not necessary – Luis Mendo – 2017-09-28T08:50:14.000

Also, this seems to work just fine without the transpose? You've been overthinking things... ;) – Sanchises – 2017-09-28T09:37:55.920

@Sanchises Without the transpose it doesn't work for input [1; 1], due to how MATL(AB) handles array shape with indexing. (Alternatively, the transpose could be replaced by He at the end, which is why it was initially there) – Luis Mendo – 2017-09-28T10:21:18.247

Ah yes I figured that it was there because of row behaviour but I didn't think of this edge case. – Sanchises – 2017-09-28T10:49:40.327

5

R, 109 105 bytes

function(a,b){q=1:a*b
l=min(q[!q%%a])
x=rep("-",l*2)
x[c(seq(0,l,a),l+seq(0,l,b))]="|"
write(x,"",l,,"")}

Try it online!

Anonymous function. Computes l=lcm(a,b), then generates a range from 0 to l by a, then from l to 2*l by b, setting the indices to | and printing as a matrix with l columns.

Giuseppe

Posted 2017-09-25T18:40:10.173

Reputation: 21 077

5

Python 2, 66 bytes

l=a,b=input()
while a%b:a+=l[0]
for x in l:print a/x*('-'*~-x+'|')

Try it online!

xnor

Posted 2017-09-25T18:40:10.173

Reputation: 115 687

4

Mathematica, 63 bytes

(s=LCM@##;Print[""<>If[i~Mod~#<1,"|","-"]~Table~{i,s}]&/@{##})&

Try it online!

and another version which user202729 really, really, really wants to see posted

Mathematica, 59 bytes

(s=LCM@##;Print[""<>If[#∣i,"|","-"]~Table~{i,s}]&/@{##})&  

this one uses special character \[Divides]

J42161217

Posted 2017-09-25T18:40:10.173

Reputation: 15 931

If this is Mathematica you can probably use \[Divides] instead of Mod operator to represent divisibility, which saves 4 bytes. Also Mathics TIO should not print the {Null, Null}. – user202729 – 2017-09-27T10:04:11.017

@user202729 I fixed the Mathics print. – J42161217 – 2017-09-27T10:14:55.323

3I think that your first comment was clear enough. Please stop pressing me in order to make the changes that you want, the exact time you want. Give the users some hours to respond. Some of us have a life out of this place – J42161217 – 2017-09-27T12:51:48.810

4

C, 72 bytes

i;f(x,y){for(i=1;i%y|i%x;)putchar(i++%x?45:124);puts("|");y>0&&f(y,-x);}

orlp

Posted 2017-09-25T18:40:10.173

Reputation: 37 067

4

Husk, 12 bytes

†?'-'|TUṪ`%N

Try it online!

Yeah, there is a lcm builtin in Husk. No, I don't need it.

Bonus: works with any number of input values

Explanation

†?'-'|TUṪ`%N    input:[2,3]
        Ṫ`%N    table of all remainders of positive naturals divided by
                input numbers:
                             [[1,1],[0,2],[1,0],[0,1],[1,2],[0,0],[1,1],[0,2],...
       U        get all elements before the first repeated one:
                             [[1,1],[0,2],[1,0],[0,1],[1,2],[0,0]]
      T         transpose:   
                             [[1,0,1,0,1,0],[1,2,0,1,2,0]]
†?'-'|          replace all truthy elements with '-' and all falsy elements
                with '|': 
                             ["-|-|-|","--|--|"]
                implicit: since this is a full program, join the resulting array
                of strings with newlines, and print to stdout

Leo

Posted 2017-09-25T18:40:10.173

Reputation: 8 482

3

Octave, 46 38 bytes

-8 bytes thanks to several suggestions by Luis Mendo

@(a,b)'-|'.'(~mod(1:lcm(a,b),[a;b])+1)

Try it online!

Giuseppe

Posted 2017-09-25T18:40:10.173

Reputation: 21 077

3

05AB1E, 13 bytes

ʒ<'-×'|«¹.¿∍,

Uses the 05AB1E encoding. Try it online!

Adnan

Posted 2017-09-25T18:40:10.173

Reputation: 41 965

Filter was a good idea, I got 17 doing it entirely different .¿Lε²¹‚%_„-|è}øJ». – Magic Octopus Urn – 2017-10-04T16:22:33.177

3

JavaScript (ES6), 69 bytes

f=(a,b,S,A=1)=>(A%a?'-':'|')+(A%a|A%b?f(a,b,S,A+1):S?'':`
`+f(b,a,1))

Recursively runs until A is divisible by both a and b – outputting a dash or pipe based on a's divisibility by A.

The function then calls itself, swapping a and b.

The S variable prevents the function from calling itself infinitely.

Test Cases:

f=(a,b,S,A=1)=>(A%a?'-':'|')+(A%a|A%b?f(a,b,S,A+1):S?'':`
`+f(b,a,1))

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


Previous answers:

JavaScript (ES8), 91 bytes

f=(a,b,i=2,g=(c,d)=>d?g(d,c%d):c)=>i?'|'.padStart(a,'-').repeat(b/g(a,b))+`
`+f(b,a,i-1):''

Uses the algorithms:

lcm(a, b) = ab / gcd(a, b)
gcd(c, d) = d ? gcd(d, c%d) : c

Recursively calls itself just once to output the second line.

Test Cases:

f=(a,b,i=2,g=(c,d)=>d?g(d,c%d):c)=>i?'|'.padStart(a,'-').repeat(b/g(a,b))+`
`+f(b,a,i-1):''

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

JavaScript (ES6), 93 bytes

f=(a,b,i=2,g=(c,d)=>!d=>d?c:g(d,c%d):c)=>i?('-'.repeat(a-1)+'|').repeat(a*bb/g(a,b)/a)+`
`+f(b,a,i-1):''

Same algorithm as before, using repeat instead of padStart.

Rick Hitchcock

Posted 2017-09-25T18:40:10.173

Reputation: 2 461

1I thought padStart was ES8? – Neil – 2017-09-25T23:34:28.847

1f=(a,b,A=1)=>(A%a?'-':'|')+(A%a|A%b?f(a,b,A+1):a<0?'':`\n`+f(-b,a)) – l4m2 – 2018-10-22T06:05:38.680

@l4m2, I can barely understand code that I wrote a year ago, but it does look like yours does shave off some bytes, thanks! – Rick Hitchcock – 2018-10-22T19:24:47.967

3

APL (Dyalog), 22 bytes

Assumes ⎕IO←0. Takes A,B as right argument. Bonus: handles input list of any length!

{'|-'[⌽×⍵∘.|⍳∧/⍵]}

Try it online!

{} anonymous lambda where represents the right argument

'|-'[] index the string with:

  ∧/ LCM across the input

   first that many ɩntegers (0 through N-1)

  ⍵∘.| division remainder table with the input vertically and that horizontally

  × signum

   flip horizontally

Adám

Posted 2017-09-25T18:40:10.173

Reputation: 37 779

What does that first assumption mean? – Calvin's Hobbies – 2017-09-26T01:00:07.703

@HelkaHomba It means that arrays index starting at 0, a default on APL interpreters, I believe. – Conor O'Brien – 2017-09-26T01:04:29.173

@HelkaHomba Since APL systems come in both 0-based and 1-based flavours, I just write the assumption. Otherwise one would have to have two APLs. E.g. ngn/apl can run this very same code without specifying ⎕IO←0, as that is the default there.

– Adám – 2017-09-26T06:19:29.730

3

Scala, 98 bytes

print((a to a*b).find(l=>l%a+l%b==0).map(l=>("-"*(a-1)+"|")*(l/a)+"\n"+("-"*(b-1)+"|")*(l/b)).get)

Try it online

cubic lettuce

Posted 2017-09-25T18:40:10.173

Reputation: 181

Hi, welcome to PPCG! This looks like a great first answer, so +1 from me. I'm not sure, since I've never programmed in Scala, but can *(a-1) be golfed to *~-a and *(b-1) to *~-b? Also, could you perhaps add a TIO link with test code? (Oh, and that avatar doesn't seem very cubic to me. ;p)

– Kevin Cruijssen – 2017-09-26T14:31:09.933

2Thanks! The trick with *~-a is great, but unfortunately Scala requires more brackets: *(~(-a)) to make clear that the concatenations *~-, *~, ~- are not fancy function names. I added a TIO link. – cubic lettuce – 2017-09-27T07:12:04.547

Ah yes, ~- can be function names in Scala. I remember someone mentioning that before quite a while ago. That's unfortunate regarding golfing. Again welcome, and nice first answer. – Kevin Cruijssen – 2017-09-27T07:37:25.037

3

Java 8, 125 118 117 bytes

a->b->{String A="\n",B=A,t="|";for(int i=1;!A.endsWith(t)|!B.endsWith(t);B+=i++%b<1?t:"-")A+=i%a<1?t:"-";return A+B;}

-7 bytes thanks to @Nevay.
-1 byte by starting with a trailing new-line (A="",B="\n" replaced with A="\n",B=A).

Explanation:

Try it here.

a->b->{             // Method with two integer parameters and String return-type
  String A="\n",    //  String top line (starting with a trailing new-line)
         B=A,       //  String bottom-line (starting with a new-line)
         t="|";     //  Temp String "|" which is used multiple times
  for(int i=1;      //  Index-integer, starting at 1
      !A.endsWith(t)|!B.endsWith(t);
                    //  Loop as long as both Strings aren't ending with "|"
      B+=           //    After every iteration: append `B` with:
         i++%b<1?   //     If `i` is divisible by `b`:
                    //     (and increase `i` by 1 in the process)
          t         //      `t` (holding "|")
         :          //     Else:
          "-")      //      A literal "-"
    A+=             //   Append `A` with:
       i%a<1?       //    If `i` is divisible by `a`
        t           //     `t` (holding "|")
       :            //    Else:
        "-";        //     A literal "-"
                    //  End of loop (implicit / single-line body)
  return A+B;       //  Return both lines, separated by the new-line `B` started with
}                   // End of method

Kevin Cruijssen

Posted 2017-09-25T18:40:10.173

Reputation: 67 575

1118 bytes: a->b->{String A="",B="\n",k="|";for(int i=0;!A.endsWith(k)|!B.endsWith(k);B+=i%b<1?k:"-")A+=++i%a<1?k:"-";return A+B;} – Nevay – 2017-09-27T17:16:20.970

@Nevay Thanks. Can't believe I missed the most obvious thing !A.endsWith(t)|!B.endsWith(t) when I was looking for a short way to check whether both are ending with |.. And starting B with a new-line instead of putting it between them at the return is also smart. – Kevin Cruijssen – 2017-09-27T17:24:22.253

3

Java (OpenJDK 8), 103 bytes

a->b->{String l="",r="|\n";for(int m=0;(++m%a|m%b)>0;r+=m%b<1?'|':'-')l+=m%a<1?'|':'-';return l+r+'|';}

Try it online!

110 bytes, n input values

a->{String s="";for(int v:a){for(int i=1,z=1;z>(z=0);s+=i++%v<1?'|':'-')for(int k:a)z|=i%k;s+='\n';}return s;}

Try it online!

Nevay

Posted 2017-09-25T18:40:10.173

Reputation: 421

2

Python 2, 96 88 bytes

Edit: Saved 4 bytes thanks to @Leaky Nun

Edit: Saved 4 bytes thanks to @Rod

lambda a,b:b/gcd(a,b)*("-"*~-a+"|")+"\n"+a/gcd(a,b)*("-"*~-b+"|")
from fractions import*

Try it online!

Halvard Hummel

Posted 2017-09-25T18:40:10.173

Reputation: 3 131

88 bytes or 77 bytes for a more flexible output – Rod – 2017-09-25T18:56:56.210

88 bytes – ovs – 2017-09-25T18:57:08.090

1Apparently outputting a list of strings isn't acceptable. :I Replace [...] with '\n'.join(...) to fix. – totallyhuman – 2017-09-25T19:22:37.743

2

Haskell, 66 60 bytes

a#b=do x<-[a,b];lcm a b`take`cycle(([2..x]>>"-")++"|")++"\n"

Try it online!


Same length:

a#b=unlines[take(lcm a b)$cycle$([2..x]>>"-")++"|"|x<-[a,b]]

Old solution:

l!x=[1..div l x]>>([2..x]>>"-")++"|"
a#b|l<-lcm a b=l!a++'\n':l!b

BlackCap

Posted 2017-09-25T18:40:10.173

Reputation: 3 576

1You can save a byte with '\n':. – Laikoni – 2017-09-25T21:05:08.203

@Laikoni Careful, I am closing in on you – BlackCap – 2017-09-26T09:54:52.393

2

Python 2, 89 bytes

Not the shortest Python 2 entry, but a different approach than gcd which may still be golfable.

a,b=input()
h,p='-|'
x=b*(h*~-a+p),a*(h*~-b+p)
for v in x:print v[~zip(*x).index((p,p)):]

Try it online!

Jonathan Allan

Posted 2017-09-25T18:40:10.173

Reputation: 67 804

1

SOGL V0.12, 19 16 bytes

2{H┌*┐+..*..g/mP

Try it Here!

Explanation:

2{                two times do
  H                 decreate ToS - input - by 1
   ┌*               get that many dashes
     ┐+             append a vertical bar
       ..*          push both inputs multiplied           \
          ..g       push gcd(input1, input2)              | LCM - 7 bytes :/
             /      divide the multiblication by the GCD  /
              m     mold the string to that length
               P    print that in a new line

dzaima

Posted 2017-09-25T18:40:10.173

Reputation: 19 048

You documented before implementing? o0 – totallyhuman – 2017-09-25T19:23:39.710

1@icrieverytim SOGL has many, many documentated things that aren't implemented. :p The documentation is basically my TODO list (which I rarely do :p) – dzaima – 2017-09-25T19:24:24.900

1

C (gcc), 121 99 93 92 89 bytes

This should be much shorter, hmmmm....

#define L(x)for(j=-1,i=a;j<i;i+=i%b||i%a)putchar(++j?j%x?45:124:10);
i,j;f(a,b){L(a)L(b)}

Try it online!

cleblanc

Posted 2017-09-25T18:40:10.173

Reputation: 3 360

1

J, 20 bytes

'-|'{~*.$&>;&(<:=i.)

Try it online!

miles

Posted 2017-09-25T18:40:10.173

Reputation: 15 654

this is pretty J – Jonah – 2017-09-26T01:33:05.090

1

Ruby, 64 57 bytes

->a,b{[a,b].map{|n|(1..a.lcm(b)).map{|x|x%n>0??-:?|}*''}}

-7 bytes thanks to G B.

Try it online!

Snack

Posted 2017-09-25T18:40:10.173

Reputation: 251

You don't need to include the 'puts', if the function returns 2 strings it's ok. And you can make it shorter by using the array * operator (array*'' is equivalent to array.join) – G B – 2017-09-29T06:16:10.180

@GB thanks for your help! – Snack – 2017-09-29T17:07:39.327

1

Charcoal, 32 30 29 bytes

NθNη≔θζW﹪ζη≦⁺θζE⟦θη⟧…⁺×-⁻ι¹|ζ

Try it online! Link is to verbose version of code. Edit: Saved 1 byte thanks to @ASCII-only.

Neil

Posted 2017-09-25T18:40:10.173

Reputation: 95 035

29 bytes – ASCII-only – 2017-10-04T01:35:20.500

@ASCII-only Something new to document! – Neil – 2017-10-04T12:32:33.273

Thanks for documenting! (sorry, I don't really feel like documenting things although I should really do it), if you don't mind there's some more to document https://chat.stackexchange.com/transcript/240?m=40270513#40270513 https://chat.stackexchange.com/transcript/240?m=40270838#40270838 (not sure where the directional casts should go, they're not commands nor are they operators)

– ASCII-only – 2017-10-04T12:54:44.153

1

Stacked, 42 38 bytes

[:...lcm@z:[:z\/\#-'-'*\rep'|'+out]"!]

Try it online!

Input in the form of a pair of numbers. All the test cases put together look kinda like buildings.

Explanation

This first takes the lcm of the two input numbers into z. Then, for each number k, we generate z / k strings of - of length k - 1, adding | to the end of each, and outputting each.

Previous counted attempts

42 bytes: [:...lcm@z:[:z\/\#-'-'*\rep'|'+''#`out]"!]

Other attempts

43 bytes: [:...lcm@z:[:z\/\#-'-'*\rep'|'#`'|'+out]"!]

45 bytes: ['@lcm'!#~@z,[:z\/\#-'-'*\rep'|'#`'|'+out]"!]

45 bytes: [:...lcm@x[x'-'*\#<$betailmap'|'#`'|'+out]"!]

53 bytes: [:...lcm'-'*@z#-'.'*'('\+')'+'.'+[z\'$1|'repl out]"!]

54 bytes: [:...lcm@x{!x'-'*('('n#-'.'*').')''#`'$1|'repl out}"!]

Conor O'Brien

Posted 2017-09-25T18:40:10.173

Reputation: 36 228

1

VBA (Excel) , 144 142 bytes

Sub q()
a=[a1]
b=[a2]
Do Until c=d And d="|"
e=e+1
c=IIf(e Mod a,"-","|")
d=IIf(e Mod b,"-","|")
f=f& c
g=g& d
Loop
Debug.? f& vbCr& g
End Sub

-2 bytes. thanks Sir Washington Guedes.

remoel

Posted 2017-09-25T18:40:10.173

Reputation: 511

Yes Thank you @WashingtonGuedes. :) – remoel – 2017-09-27T02:35:14.507

1

JavaScript (ES6), 89

f=(a,b,t=`
`,l=0,R=n=>'-'.repeat(n-1)+'|')=>l||1/t?f(a,b,l<0?t+R(b,l+=b):R(a,l-=a)+t,l):t

Evaluating the LCM with repeated addictions.

Less golfed

F=(a,b, sa='', sb='', la=0, lb=0)=>
{
    var R=n=>'-'.repeat(n-1)+'|'
    if (la != lb || la == 0)
    {
        if (la < lb) {
            sa += R(a)
            la += a
        }
        else
        {
            sb += R(b)
            lb += b
        }
        return F(a, b, sa, sb, la, lb)
    }
    else
        return sa+'\n'+sb
}

Test

f=(a,b,t=`
`,l=0,R=n=>'-'.repeat(n-1)+'|')=>l||1/t?f(a,b,l<0?t+R(b,l+=b):R(a,l-=a)+t,l):t

function update()
{
  var [a,b]=I.value.match(/\d+/g)
  R.textContent = f(+a,+b)
}  

update()
<input id=I oninput='update()' value='4 6'>
<pre id=R></pre>

edc65

Posted 2017-09-25T18:40:10.173

Reputation: 31 086

1

Google Sheets, 77 Bytes

Anonymous worksheet formula that takes input from range A1:B1 and outputs to the calling cell

=REPT(REPT("-",A1-1)&"|",LCM(1:1)/A1)&"
"&REPT(REPT("-",B1-1)&"|",LCM(1:1)/B1

-4 Bytes thanks to @EngineerToast

Taylor Scott

Posted 2017-09-25T18:40:10.173

Reputation: 6 709

1Can you presume that the nothing else is input to row 1? If so, you can shorted LCM(A1,B1) to just LCM(1:1) to save 4 bytes. I think it's reasonable to presume a blank starting sheet and specify where both the inputs and formula are. – Engineer Toast – 2018-10-19T20:41:30.873

1

Excel VBA, 79 Bytes

Anonymous VBE immediate window function that takes input from the range [A1:B1] and outputs a visualization of their LCM to the VBE immediate window.

This is a port of my Google Sheets answer.

?[Rept(Rept("-",A1-1)&"|",LCM(1:1)/A1)]:?[Rept(Rept("-",B1-1)&"|",LCM(1:1)/B1)]

Taylor Scott

Posted 2017-09-25T18:40:10.173

Reputation: 6 709

1

Japt, 12 bytes

£×/Ury)î|ù-X

Japt Interpreter

Inputs as an array of numbers. Outputs as an array of strings. The -R flag slightly improves how the output looks, but isn't necessary for the logic.

Explanation:

£              For each of the two inputs as X, print...
        |           The string "|"
         ù-X        Left-padded with "-" until it is X characters long
       î            Repeated until its length is
 ×/Ury)             The Least Common Multiple

Extra thanks to Shaggy for finding some bytes to save.

Kamil Drakari

Posted 2017-09-25T18:40:10.173

Reputation: 3 461

115 bytes – Shaggy – 2018-10-23T19:10:11.967

@Shaggy Interesting, I never thought to use that part of Japt in that exact way. – Kamil Drakari – 2018-10-23T19:17:37.270

1Knocked another few bytes off – Shaggy – 2018-10-23T19:33:16.970

1

Whispers v2, 131 bytes

> Input
> Input
>> 1⊔2
>> (3]
>> L∣1
>> L∣2
>> Each 5 4
>> Each 6 4
> '-|'
>> 9ⁿL
>> Each 10 7
>> Each 10 8
>> Output 11 12

Try it online!

How it works

If you're unfamiliar with Whispers' program structure, I'd recommend reading the first part of this post.

In this explanation, we'll refer to the two inputs as \$x\$ and \$y\$ respectively. Our first two lines simply take the inputs in, and store them on lines 1 (\$x\$) and 2 (\$y\$). We then move to line 3, which returns \$\alpha = \mathrm{lcm}(x, y)\$ and to line 4, which returns the range \$A = [1, 2, ..., \alpha]\$.

Next, we reach our first two Each statements, operating on each of the inputs:

>> L∣1
>> L∣2
>> Each 5 4
>> Each 6 4

These four lines both operate on \$A\$, but return two different arrays, which we will call \$A_x\$ and \$A_y\$. While being different arrays, they are both formed in similar ways, as can be noted from the similarities in the two pairs of lines. In fact, we can define \$A_x\$ and \$A_y\$ as

$$A_x := [(i \div x) \in \mathbb{Z} \: | \: i \in A]$$ $$A_y := [(i \div y) \in \mathbb{Z} \: | \: i \in A]$$

This leaves us with two lists consisting of a \$1\$ where we'd expect there to be a | character, and a \$0\$ where there should be a -. This takes us to the next section of our code:

> '-|'
>> 9ⁿL
>> Each 10 7
>> Each 10 8

First, we yield the string -|, then we create our next two arrays \$B_x\$ and \$B_y\$. Helpfully, we can use the same function to map \$A_x\$ to \$B_x\$ and \$A_y\$ to \$B_y\$, namely 9ⁿL. This function yields the \$n^{th}\$ element of the string on line 9 i.e. -|, where \$n\$ is either \$0\$ or \$1\$, depending on the element from the respective \$A\$ arrays. This yields the two arrays \$B_x\$ and \$B_y\$ as defined below:

$$(B_x)_i = \begin{cases} \text{"-"}, & (A_x)_i = 0 \\ \text{"|"}, & (A_x)_i = 1 \end{cases}$$

$$(B_y)_i = \begin{cases} \text{"-"}, & (A_y)_i = 0 \\ \text{"|"}, & (A_y)_i = 1 \end{cases}$$

The Each command is special-cased for when yielding an array of strings, where it returns a single string, rather than an array. Finally, we reach the statement

>> Output 11 12

which outputs \$B_x\$, then a newline, then \$B_y\$

caird coinheringaahing

Posted 2017-09-25T18:40:10.173

Reputation: 13 702

I believe is one byte shorter than Each, Try it online!, though I can't shake the feeling that there's some way to combine the processes

– Jo King – 2020-01-13T05:59:11.763

0

Pyth - 20 bytes

j.e*/@QtkiFQ+*\-tb\|

Test Suite.

Maltysen

Posted 2017-09-25T18:40:10.173

Reputation: 25 023

0

Retina, 67 bytes

\d+
$*-
^((-+)(\2)*) ((\2)+)$
$1$#5$*1¶$4$#3$*11
-1
|
+`(-*.)1
$1$1

Try it online! Link includes test cases.

Neil

Posted 2017-09-25T18:40:10.173

Reputation: 95 035

0

PHP, 142 bytes

list(,$a,$b)=$argv;for($n=$a;$n<=$a*$b;$n+=$a)if($n%$b==0)break;while($i<$n)echo++$i%$a==0?'|':'-';echo"
";while($j<$n)echo++$j%$b==0?'|':'-';

Try it online !

roberto06

Posted 2017-09-25T18:40:10.173

Reputation: 351

0

Javascript (ES6), 84 bytes

f=(a,b,c=1,o=x='-'.repeat(a-1)+'|')=>(o.length%b?f(a,b,0,o+x):o)+(c?`
`+f(b,a,0):'')

Test Cases:

f=(a,b,c=1,o=x='-'.repeat(a-1)+'|')=>(o.length%b?f(a,b,0,o+x):o)+(c?`
`+f(b,a,0):'')

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

Old answer Javascript (ES6), 91 bytes

f=(a,b)=>{for(x=y='',i=0;!i++||q||r;)q=i%a,r=i%b,x+=q?'-':'|',y+=r?'-':'|';return x+'\n'+y}

Patrick Stephansen

Posted 2017-09-25T18:40:10.173

Reputation: 103

0

AWK, 80 bytes

{for(;++m%$1||m%$2;)for(i=0;i++<2;)r[i]=r[i](m%$i?"-":"|");$0=r[1]"|\n"r[2]"|"}1

Try it online!

For 2 or more values you can use the following 94 bytes:

{m=0;for(x=1;x&&++m;)for(x=i=0;i++<NF;){x+=r=m%$i;R[i]=R[i](r?"-":"|")}for(;++z<i;)print R[z]}

Try it online!

I'm probably missing something obvious to shorten this. :)

Robert Benson

Posted 2017-09-25T18:40:10.173

Reputation: 1 339

0

J, 20 bytes

*./($'-|'#~<:,1:)"0]

Try it online!

FrownyFrog

Posted 2017-09-25T18:40:10.173

Reputation: 3 112

0

Ruby, 45 bytes

->a,b{[a,b].map{|x|(?-*~-x+?|)*(a.lcm(b)/x)}}

Try it online!

G B

Posted 2017-09-25T18:40:10.173

Reputation: 11 099

0

PHP, 85 82 bytes

for([,$a,$b]=$argv;++$i%$a|$y=$i%$b;$t.="-|"[!$y])$s.=$i%$a?"-":"|";echo"$s|
$t|";

Try it online.

Titus

Posted 2017-09-25T18:40:10.173

Reputation: 13 814

0

Perl 6, 39 bytes

{@^a>>.&{('-'x$_-1~'|')x([lcm] @a)/$_}}

Try it online!

Takes input as a pair of numbers and returns a list of lines. Note that you can pass in as many integers as you like and it will print the lcm graph of all of them.

Jo King

Posted 2017-09-25T18:40:10.173

Reputation: 38 234

0

Powershell, 79 bytes

param($a,$b)do{$x+='-|'[!($c=++$i%$a)];$y+='-|'[!($d=$i%$b)]}while($c+$d)
$x;$y

Ungolfed test script:

$f = {

param($a,$b)
do{
    $x+='-|'[!($c=++$i%$a)]
    $y+='-|'[!($d=$i%$b)]
}while($c+$d)
$x;$y

}

@(
    ,(1,1,"| |")
    ,(1,2,"|| -|")
    ,(2,1,"-| ||")
    ,(6,4,"-----|-----| ---|---|---|")
    ,(4,6,"---|---|---| -----|-----|")
    ,(2,3,"-|-|-| --|--|")
    ,(3,2,"--|--| -|-|-|")
    ,(3,6,"--|--| -----|")
    ,(2,5,"-|-|-|-|-| ----|----|")
    ,(4,3,"---|---|---| --|--|--|--|")
    ,(10,10,"---------| ---------|")
    ,(10,5,"---------| ----|----|")
    ,(10,6,"---------|---------|---------| -----|-----|-----|-----|-----|")
    ,(24,8,"-----------------------| -------|-------|-------|")
    ,(7,8,"------|------|------|------|------|------|------|------| -------|-------|-------|-------|-------|-------|-------|")
    ,(6,8,"-----|-----|-----|-----| -------|-------|-------|")
    ,(13,11,"------------|------------|------------|------------|------------|------------|------------|------------|------------|------------|------------| ----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|")
) | % {
    $a,$b,$expected = $_
    $result = &$f $a $b
    "$(""$result"-eq"$expected"): $a,$b"
    $result
}

Output:

True: 1,1
|
|
True: 1,2
||
-|
True: 2,1
-|
||
True: 6,4
-----|-----|
---|---|---|
True: 4,6
---|---|---|
-----|-----|
True: 2,3
-|-|-|
--|--|
True: 3,2
--|--|
-|-|-|
True: 3,6
--|--|
-----|
True: 2,5
-|-|-|-|-|
----|----|
True: 4,3
---|---|---|
--|--|--|--|
True: 10,10
---------|
---------|
True: 10,5
---------|
----|----|
True: 10,6
---------|---------|---------|
-----|-----|-----|-----|-----|
True: 24,8
-----------------------|
-------|-------|-------|
True: 7,8
------|------|------|------|------|------|------|------|
-------|-------|-------|-------|-------|-------|-------|
True: 6,8
-----|-----|-----|-----|
-------|-------|-------|
True: 13,11
------------|------------|------------|------------|------------|------------|------------|------------|------------|------------|------------|
----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|

mazzy

Posted 2017-09-25T18:40:10.173

Reputation: 4 832

0

Julia 1.0, 52 47 bytes

p->println.(@.("-"^(p-1)*"|").^(lcm(p...).÷p))

Anonymous function; takes a list or tuple of two integers and prints the ASCII-art. Try it online!

Explanation

p->                 Function of p (a list or tuple of two integers):
 println.(   )       Print with newline, vectorized:
  @.(   )             Vectorize throughout the following expression:
    "-"^(p-1)*"|"      String of x-1 hyphens and one pipe for each x in p
   (   ).^(   )        each repeated this many times:
      lcm(p...)         LCM of the two integers in p
               .÷p      divided by each number in p

DLosc

Posted 2017-09-25T18:40:10.173

Reputation: 21 213

40 bytes – H.PWiz – 2019-01-01T20:00:36.223