53
\$\begingroup\$

Given two positive integers, \$A\$ and \$B\$, illustrate their least common multiple by outputting two lines of dashes (-) with length \$\text{lcm}\$\$(A, B)\$ after replacing every \$A\$th dash in the first line and every \$B\$th 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\$, \$\text{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
------------|------------|------------|------------|------------|------------|------------|------------|------------|------------|------------|
----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|
\$\endgroup\$
7
  • 3
    \$\begingroup\$ @LeakyNun Extending an answer from 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. \$\endgroup\$ Sep 25 '17 at 19:02
  • 1
    \$\begingroup\$ Can I output an array with two strings, one for each line? \$\endgroup\$
    – BlackCap
    Sep 25 '17 at 19:05
  • \$\begingroup\$ @BlackCap No. Print the strings to stdout or a file or return the whole multiline string. \$\endgroup\$ Sep 25 '17 at 19:06
  • 3
    \$\begingroup\$ Bonus for handling arbitrary number of inputs? \$\endgroup\$
    – Adám
    Sep 25 '17 at 23:49
  • 1
    \$\begingroup\$ @HelkaHomba Ok thanks; saved 1 more byte. :) (As if there is any other reason to ask such questions on codegolf challenges. ;p) \$\endgroup\$ Sep 28 '17 at 6:58

43 Answers 43

1
2
1
\$\begingroup\$

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)]
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ 15 bytes \$\endgroup\$
    – Shaggy
    Oct 23 '18 at 19:10
  • \$\begingroup\$ @Shaggy Interesting, I never thought to use that part of Japt in that exact way. \$\endgroup\$ Oct 23 '18 at 19:17
  • 1
    \$\begingroup\$ Knocked another few bytes off \$\endgroup\$
    – Shaggy
    Oct 23 '18 at 19:33
1
\$\begingroup\$

Jelly, 11 bytes

,ḍþælị⁾|-ZY

Try it online!

How it works

,ḍþælị⁾|-ZY - Main link. Takes A on the left and B on the right
,           - Pair; [A, B]
   æl       - LCM; L := LCM(A, B)
  þ         - Create a 2×L matrix and for each cell (i,j) i ∈ (A,B), 1 ≤ j ≤ L:
 ḍ          -   Is i divisible by j?
     ị⁾|-   - Index into "|-", replacing 1 with "|" and 0 with "-"
         Z  - Transpose the matrix into a L×2 matrix
          Y - Join by newlines
\$\endgroup\$
0
\$\begingroup\$

Pyth - 20 bytes

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

Test Suite.

\$\endgroup\$
0
\$\begingroup\$

Retina, 67 bytes

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

Try it online! Link includes test cases.

\$\endgroup\$
0
\$\begingroup\$

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 !

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

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}
\$\endgroup\$
0
\$\begingroup\$

Ruby, 45 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

PHP, 85 82 bytes

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

Try it online.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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
------------|------------|------------|------------|------------|------------|------------|------------|------------|------------|------------|
----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ 40 bytes \$\endgroup\$
    – H.PWiz
    Jan 1 '19 at 20:00
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.