Skip to main content
added 8 characters in body
Source Link
Reto Koradi
  • 4.9k
  • 1
  • 13
  • 19

CJam, 2626 24 bytes

l~:N;:M{TN+Mmd:T;0a*1}*;W<*>

Try it online

Very straightforward, pretty much a direct implementation of a Bresenham type algorithm.

Explanation:

l~    Get input and convert to 2 integers.
:N;   Store away N in variable, and pop from stack.
:M    Store away M in variable.
{     Loop M times.
  T     T is the pending remainder.
  N+    Add N to pending remainder.
  M     Push M.
  md    Calculate div and mod.
  :T;   Store away mod in T, and pop it from stack
  0a    Wrap 0 in array so that it is replicated by *, not multiplied.
  *     Emit div 0s...
  1     ... and a 1.
}*      End of loop over M.
;W<>       Pop the last 1 and 0.

The last 01 needs to be popped because the loop went all the way to the end point, which is not part of he desired output. Note that we can not simply reduce the loop count by 1. Otherwise, for N > M, all the 0s from the last iteration will be missing, while we only need to get rid of the very last 0.

CJam, 26 bytes

l~:N;:M{TN+Mmd:T;0a*1}*;W<

Try it online

Very straightforward, pretty much a direct implementation of a Bresenham type algorithm.

Explanation:

l~    Get input and convert to 2 integers.
:N;   Store away N in variable, and pop from stack.
:M    Store away M in variable.
{     Loop M times.
  T     T is the pending remainder.
  N+    Add N to pending remainder.
  M     Push M.
  md    Calculate div and mod.
  :T;   Store away mod in T, and pop it from stack
  0a    Wrap 0 in array so that it is replicated by *, not multiplied.
  *     Emit div 0s...
  1     ... and a 1.
}*      End of loop over M.
;W<     Pop the last 1 and 0.

The last 01 needs to be popped because the loop went all the way to the end point, which is not part of he desired output. Note that we can not simply reduce the loop count by 1. Otherwise, for N > M, all the 0s from the last iteration will be missing, while we only need to get rid of the very last 0.

CJam, 26 24 bytes

l~:N;:M{TN+Mmd:T;0a*1}*>

Try it online

Very straightforward, pretty much a direct implementation of a Bresenham type algorithm.

Explanation:

l~    Get input and convert to 2 integers.
:N;   Store away N in variable, and pop from stack.
:M    Store away M in variable.
{     Loop M times.
  T     T is the pending remainder.
  N+    Add N to pending remainder.
  M     Push M.
  md    Calculate div and mod.
  :T;   Store away mod in T, and pop it from stack
  0a    Wrap 0 in array so that it is replicated by *, not multiplied.
  *     Emit div 0s...
  1     ... and a 1.
}*      End of loop over M.
>       Pop the last 1 and 0.

The last 01 needs to be popped because the loop went all the way to the end point, which is not part of he desired output. Note that we can not simply reduce the loop count by 1. Otherwise, for N > M, all the 0s from the last iteration will be missing, while we only need to get rid of the very last 0.

Source Link
Reto Koradi
  • 4.9k
  • 1
  • 13
  • 19

CJam, 26 bytes

l~:N;:M{TN+Mmd:T;0a*1}*;W<

Try it online

Very straightforward, pretty much a direct implementation of a Bresenham type algorithm.

Explanation:

l~    Get input and convert to 2 integers.
:N;   Store away N in variable, and pop from stack.
:M    Store away M in variable.
{     Loop M times.
  T     T is the pending remainder.
  N+    Add N to pending remainder.
  M     Push M.
  md    Calculate div and mod.
  :T;   Store away mod in T, and pop it from stack
  0a    Wrap 0 in array so that it is replicated by *, not multiplied.
  *     Emit div 0s...
  1     ... and a 1.
}*      End of loop over M.
;W<     Pop the last 1 and 0.

The last 01 needs to be popped because the loop went all the way to the end point, which is not part of he desired output. Note that we can not simply reduce the loop count by 1. Otherwise, for N > M, all the 0s from the last iteration will be missing, while we only need to get rid of the very last 0.