8
\$\begingroup\$

Kinda a short one, but yeah.

Y=MX+C is the equation we all learnt to represent a line... But can you Code Golf it?

Write a program that takes in a list in the format M, C, V, and then returns the first V worth of whole x values. V will always be 1 or greater, M and C can have any (float) value (inc 0).

Examples (these are in python format for readability, use any I/O method you want):

[2,0,5] -> [2,4,6,8,10]
[1,1,15] -> [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
[0.5,-1,1] -> [-0.5]
[0,0,5] -> [0,0,0,0,0]

Scored with Code-Golf (Least bytes wins), use any language :D

\$\endgroup\$
3
  • 3
    \$\begingroup\$ Please avoid cumberson I/O formats. Just let people decide how they want to take input and give output. \$\endgroup\$
    – Adám
    Commented Dec 4 at 12:05
  • 1
    \$\begingroup\$ Yes sorry, I was only specifying Python Format for the examples to make it easier. \$\endgroup\$
    – Natelolzzz
    Commented Dec 4 at 12:17
  • 2
    \$\begingroup\$ Now do Y=MX²+NX+C (or higher)… \$\endgroup\$
    – Adám
    Commented Dec 4 at 13:44

28 Answers 28

6
\$\begingroup\$

Vyxal 3, 3 bytes

ɾ×+

Vyxal It Online!

Input in the form V, M, C as separate args

\$\endgroup\$
2
  • \$\begingroup\$ nice! FYI since yesterday, you can name your groups with the desired output, and it will tell you whether the program passed each test: Vyxal It Online! \$\endgroup\$ Commented Dec 4 at 12:51
  • \$\begingroup\$ well that's definitely something \$\endgroup\$
    – pacman256
    Commented Dec 4 at 12:52
5
\$\begingroup\$

APL (Dyalog 20.0), 5 bytes

Anonymous tacit infix function, taking V as left argument and [M,C] as right argument.

⍪⍤⍳⍛⊥

Try it online! (emulation due to old TIO version)

⍛⊥ evaluate in mixed-base, but first pre-process the left argument as follows:

 …⍤⍳ expanding to the indices 1…V, and then:

   transform into a column vector (matches up the entire [M,C] with each of 1…V)

\$\endgroup\$
3
  • \$\begingroup\$ Jeez that's concise, Adám! looks like a newer operator? I don't think its on TryApl.org anyway; would you please give a little overview of what it does, or I guess how it differs from normal composition? \$\endgroup\$
    – Aaron
    Commented Dec 4 at 16:51
  • \$\begingroup\$ "pre-process the left argument" - as in X f g Y turns to (f X) g Y? \$\endgroup\$
    – Aaron
    Commented Dec 4 at 17:19
  • 1
    \$\begingroup\$ @Aaron Yes, that's exactly it. See APL Wiki and YouTube. \$\endgroup\$
    – Adám
    Commented Dec 4 at 17:54
5
\$\begingroup\$

05AB1E, 3 2 bytes

Two loose inputs in the order and format \$V\$ and \$[M,C]\$.

Try it online or verify all test cases.

Explanation:

L   # Push a list in the range [1, first (implicit) input V]
 β  # Convert the second (implicit) input-pair [M,C] from a base-v list to a
    # base-10 integer for each v in the [1,V]-ranged list
    # (after which this list is output implicitly)
\$\endgroup\$
4
\$\begingroup\$

brainfuck, 43 bytes

,[>+>+<<-],[>+<-],[>.>[>+<<+>-]>[<+>-]<<<-]

Try it online!

Takes M, C, V in ascii ordinals, some if not most implementations use unicode ordinals.

Doesn't work in floats.

Explanation:

,[>+>+<<-],[>+<-],[>.>[>+<<+>-]>[<+>-]<<<-]
,                                           Input M            ( [M] )
 [>+>+<<-]                                  copy into 2 cells  ( [0] M   M )
          ,                                 Input C            ( [C] M   M )
           [>+<-]                           Add C into M cell  ( [0] M+C M )
                 ,                          Input V            ( [V] M+C M )
                  [                       ] Loop till V becomes zero
                   >.                       Output second cell ( v [M+nC] M )
                     >[>+<<+>-]>            Add M into 2 cells ( v M+(n+1)C 0 [M])
                                [<+>-]      Move M cell        ( v M+(n+1)C [M] )
                                      <<<-  Decrement V cell   ( v-1 M+(n+1)C [M] )
\$\endgroup\$
3
\$\begingroup\$

Haskell + hgl, 19 bytes

m#c=ft[m+c,2*m+c..]

Attempt This Online!

Ungolfed:

f m c v = take v [m+c, 2*m+c]
\$\endgroup\$
3
\$\begingroup\$

Nim, 57 bytes

proc f(m,c,v:float)=
  if v>0.0:f(m,c,v-1.0);echo (m*v)+c

Attempt This Online!

\$\endgroup\$
3
\$\begingroup\$

R, 15 bytes

\(m,c,v)m*1:v+c

Try it online! (Note- TIO doesn't yet support \ for function)

\$\endgroup\$
3
\$\begingroup\$

Desmos, 19 bytes

f(m,c,v)=[1...v]m+c

Try It On Desmos!

What better way to solve a "graphing" calculator problem... than to literally use a graphing calculator!

\$\endgroup\$
2
\$\begingroup\$

Bash, non-competing 40 bytes

assuming actually flexible input format (M,C,V as arguments 1,2,3 respectively)

for((x=1;x<=$3;x++));{
echo $[$1*x+$2]
}

non-competing because bash has no float support.

Attempt This Online!

\$\endgroup\$
4
  • \$\begingroup\$ Hi! Nice answer, but for example 3 (0.5, -1, 1, expected 0.5), I got /ATO/code: line 2: 0.5*x+1: syntax error: invalid arithmetic operator (error token is ".5*x+1"). Seems the program cant handle floats? \$\endgroup\$
    – Natelolzzz
    Commented Dec 4 at 12:43
  • \$\begingroup\$ you're right, i missed the float part. Bash has no float support. i'll mark my answer as non-competing \$\endgroup\$ Commented Dec 4 at 12:45
  • \$\begingroup\$ Dw! its still a really good program, and I think its pretty neat! \$\endgroup\$
    – Natelolzzz
    Commented Dec 4 at 12:49
  • \$\begingroup\$ Unfortunately we don't use the "non-competing" flag here. Answers that don't meet the challenge specification have to be deleted. \$\endgroup\$ Commented 10 hours ago
2
\$\begingroup\$

APL+WIN, 6 bytes

Prompts for input of v followed by m and then c

⎕+⎕×⍳⎕

Try it online! Thanks to Dyalog Classic

\$\endgroup\$
2
\$\begingroup\$

Octave, 17 bytes

@(m,c,v)(1:v)*m+c

Try it online!

Basically the same code as Eonema posted in the answer in R.

\$\endgroup\$
1
\$\begingroup\$

C# (Visual C# Interactive Compiler) with /u:System.Linq.Enumerable flag, 36 bytes

(m,c,v)=>Range(1,v).Select(i=>m*i+c)

Try it online.

Explanation:

(m,c,v)=>           // Method with two double and one integer inputs
                    // and IEnumerable<double> return-type
  Range(1,v)        //  Create an IEnumerable in the range [1,v]
  .Select(i=>       //  Map each value `i` to:
             m*i+c) //   Multiply `m` by `i` and add `c`
\$\endgroup\$
1
\$\begingroup\$

Bash + common utilities, 27

jot -- $3 `bc<<<$2+$1` - $1

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Nekomata, 3 bytes

R*+

Attempt This Online!

Takes input in the order V, M, C.

R*+
R       [1,...,V]
 *      Times M
  +     Add C
\$\endgroup\$
1
\$\begingroup\$

PowerShell, 36 bytes

iex('1..{2}|%{{{0}*$_+{1}}}'-f$args)

Try it online!

The obvious solution would be something like for($i=1;$i-le$args[2];$i++){$args[0]*$i+$args[1]} for a whopping 50 bytes, mainly due to the $args elements.
Using iex (an alias for Invoke-Expression, which interprets a string as PowerShell), and the format operator -f, allows to golf 14 bytes from that.
Slightly ungolfed:
'1..{2} | % {{ {0} * $_ + {1} }}' -f $args
$args is an array which contains the three input values; they will be inserted into the string at {0}, {1}, {2}, respectively (the double {{ and }} escape the curly brackets required for the loop).
So with 2, 0, 5 as input, that would create the string
1..5 | % { 2 * $_ + 0 }
% is an alias for ForEach-Object, and $_ is the loop variable.
iex will then run this string as PowerShell.
Output is implicit.

\$\endgroup\$
1
\$\begingroup\$

Charcoal, 9 bytes

I⁺×…·¹NNN

Try it online! Link is to verbose version of code. Takes input in the order V, M, C. Explanation: Forms a range from 1 to V, vectorised multiplies it by M and vectorised adds C.

6 bytes by requiring input in JSON format:

I⁺×⊕…N

Try it online! Takes input in the order V, M, C. Explanation: Forms a range from 0 to V-1, increments it, then vectorised multiplies it by M and vectorised adds C.

\$\endgroup\$
1
\$\begingroup\$

Forth (gforth), 35 bytes

: a 0 DO 2DUP OVER I * + + . LOOP ;

Try it online!

Just works with integers, not with floats. Modifies global integer stack state.

\$\endgroup\$
1
\$\begingroup\$

Python, 40 39 35 bytes

lambda M,C,V:[C:=C+A for A in[M]*V]

-1 byte by tsh
-4 bytes by Albert.Lang

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You may chang to c-m*~x to save a byte. \$\endgroup\$
    – tsh
    Commented Dec 5 at 2:24
  • 2
    \$\begingroup\$ 35 bytes using the walrus. \$\endgroup\$ Commented Dec 5 at 6:13
1
\$\begingroup\$

Ruby, 25 bytes

-2 bytes thanks to G B.

->m,c,v{(1..v).map{c+=m}}

Attempt This Online!

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

Uiua, 5 bytes

+×+1⇡

Try it: Uiua pad

  • Add
    • the product of
      • the range 1 to V
      • and M
    • and C
\$\endgroup\$
0
\$\begingroup\$

JavaScript (ES6), 33 bytes

Expects (m)(c)(v) and returns an array of floats.

m=>c=>g=v=>v?[...g(v-1),m*v+c]:[]

Try it online!

Commented

m =>             // 1st function taking m
c =>             // 2nd function taking c
g = v =>         // g = recursive function taking v
v ?              // if v is not 0:
  [              //   update the output array:
    ...g(v - 1), //     append the result of a recursive call with v-1
    m * v + c    //     followed by the current value m*v+c
  ]              //   end of array
:                // else:
  []             //   stop the recursion
\$\endgroup\$
0
\$\begingroup\$

AWK, 30 bytes

{for(;i++<$3;)printf$1*i+$2FS}

Attempt This Online!

{for(;i++<$3;) # increment to V
printf         # print Y
$1*i+$2        # MX+C
FS}            # space between numbers
\$\endgroup\$
0
\$\begingroup\$

JavaScript (Node.js), 32 bytes

m=>c=>g=v=>v?[c+=m,...g(v-1)]:[]

Try it online!

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

Excel 365, 35 bytes

Input M, C, V into A1:C1.

In any other cell, write the formula:

=MAKEARRAY(C1,,LAMBDA(x,y,x*A1+B1))
\$\endgroup\$
2
  • \$\begingroup\$ 19 bytes: =A1*SEQUENCE(C1)+B1 \$\endgroup\$ Commented Dec 5 at 21:19
  • \$\begingroup\$ Very nice! I was wondering what other array functions Excel had. \$\endgroup\$ Commented Dec 7 at 2:30
0
\$\begingroup\$

Japt, 4 bytes

õ!ìV

Try it here

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

Zsh +coreutils, 24 bytes

echo $1\*{1..$3}+$2\;|bc

Try it online!

There's also a short solution using jot and setopt force_float but that would be unoriginal.

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

Tcl, 64 bytes

proc C {m c v} {time {lappend L [expr $m*[incr i]+$c]} $v
set L}

Try it online!

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

Raku (Perl 6) (rakudo), 45 bytes

my ($m,$c,$p)=@*ARGS;for 1..$p {say $m*$_+$c}

Attempt This Online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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