g | x | w | all
Bytes Lang Time Link
014Ruby rmatrix161002T000420ZJordan
025Zsh210312T114746Zroblogic
001Vyxal s210604T091637Zemanresu
003APL Dyalog Classic210329T154924ZAndrew O
013Julia 1.0210325T162724Zamelies
003BQN210314T112351ZRazetime
036PowerShell210314T104251Zmazzy
043PowerShell210314T093308Zwasif
056C210312T200805Zwojciech
014Julia 1.0210312T205442ZDonat
033JavaScript210312T103850Zuser1006
005R210312T100003ZKirill L
00305AB1E210312T003238ZMakonede
003TIBASIC161004T203829ZHactar
018R161002T144636Zrturnbul
013Perl 6161002T205527ZBrad Gil
025C#161002T192013ZTaco
067Java161002T191732ZLinnea G
012TIBasic161002T165210ZTimtech
001Mathematica161002T032147ZJungHwan
004MATLAB / Octave161002T025639ZLuis Men
002MATL161002T025206ZLuis Men
003Pyth161002T005750ZSteven H
033Python161002T002817ZDennis
017Haskell161002T002742Zxnor
001Actually161002T002317Zlynn
002Jelly161002T001232Zlynn
046PHP161002T000147ZJör

Ruby -rmatrix, 14 bytes

If we can take two Vectors as input:

->p,a{p.dot a}

Attempt This Online!

Ruby -rmatrix, 23 bytes

If we have to take arrays as input:

->p,a{Vector[*p].dot a}

Attempt This Online!

Zsh, 31 25 bytes

Try It Online. old 31 bytes

for i ($p)((S+=i*a[++j]))

This is the core function. The challenge didn't say anything about setting up variables or output so that's all external.

Vyxal s, 1 byte

*

Try it Online!

Magic.

APL (Dyalog Classic), 3 bytes

+.×  ⍝ My code is self-documenting

Try it online!

Julia 1.0, 13 bytes

Building up on Donat's solution. Thanks to Chartz Belatedly for pointing out the issue with inputs

x/y=sum(x.*y)

Try it online!

BQN, 3 bytes

+´×

Try it!

dot product

PowerShell, 36 bytes

param($p,$q)$p|%{$r+=$_*$q[$i++]}
$r

Try it online!

PowerShell, 43 bytes

function d($a,$b){$a|%{$r+=$_*$b[$i++]};$r}

Try it online!

Basically, just calculate the dot product.

C, 58 56 bytes

f(n,p,a,s)int*p,*a;{for(s=0;n;s+=p[--n]*a[n]);return s;}

Input is n: number of elements, p:array 1, a:array 2.

Somewhat un-golfed:

f(n,p,a,s)
    int*p,*a;
{
    for(s=0;n;s+=p[--n]*a[n]);
    return s;
}

Julia 1.0, 14 bytes

sum((*).(x,y))

Try it online!

JavaScript, 33 bytes

a=>b=>a.reduce((c,d,e)=>c+d*b[e])

Takes input as ([p[0],...,p[n-1])([a[0],...,a[n-1]]).

R, 5 bytes

`%*%`

Try it online!

That's the dot-product built-in in R...

05AB1E, 3 bytes

øPO

Try it online!

øPO  # full program
  O  # sum of...
 P   # products of...
     # (implicit) each element in...
     # implicit input...
ø    # with each element from the first sublist paired with the corresponding element from the second sublist
     # implicit output

TI-BASIC, 3 tokens

sum(L₁L₂

Input in L₁ and L₂, output in Ans.

Usage:

{1,2,3→L1
{4,5,6→L2
prgmCOST

Output: 32

R, 8 18 bytes

sum(scan()*scan())

Now takes input from stdin.

Perl 6, 13 bytes

{[+] [Z*] $_}

Input is a list of two lists

Example:

say {[+] [Z*] $_}(  ((2,7,5,1,9),(1,2,3,2,3))  );
# 60

Explanation:

{          # bare block lambda with implicit parameter 「$_」

  [+]      # reduce using addition operator

    [Z*]   # reduce using zip meta-operator combined with multiplication operator

      $_   # the argument ( list of lists )
}

C#, 25 bytes

p.Zip(a,(x,y)=>x*y).Sum()

Sample program:

using System.Linq;
using Xunit;
public class Tests {
    [Fact]
    public void Cost() {
        int[] p = new [] { 2, 7, 5, 1, 9 };
        int[] a = new [] { 1, 2, 3, 2, 3 };
        int r = p.Zip(a, (x, y) => x * y).Sum();
        Assert.Equal(60, r);
    }
}

Java, 67 bytes

a->{int p=0;for(int i=-1;++i<a[0];)p+=a[i+1]*a[i+a[0]+1];return p;}

Slightly ungolfed:

public static int pay(int...a){
  int p=0;
  for(int i=-1;++i<a[0];){
    p+=a[i+1]*a[i+a[0]+1];
  }
  return p;
}

TI-Basic, 12 bytes

Prompt L1,L2
sum(L1L2

Mathematica, 1 byte

.

Usage:

{1, 2, 3}.{4, 5, 6}

(* 32 *)

MATLAB / Octave, 4 bytes

@dot

This defines an anonymous function.

Try it at Ideone.

MATL, 2 bytes

*s

Try it online!

*     % Element-wise product of two implicit inputs
s     % Sum of array, implicitly displayed

Pyth, 3 bytes

s*V

(s)ums the (V)ectorized (*)product of the two input arrays.

Python, 36 33 bytes

lambda*x:sum(map(int.__mul__,*x))

Thanks to @xnor for golfing off 3 bytes!

Test it on Ideone.

Haskell, 17 bytes

(sum.).zipWith(*)

Multiply the lists entrywise, then sum. Shorter than importing (23 bytes)

import Data.Vector
vdot

Actually, 1 byte

*

Try it online!

Okay, this is the shortest dot product built-in I could find. >_>

Jelly, 2 bytes

æ.

Try it online!

A built-in computing the dot product between two input vectors: if, for each 1 ≤ i ≤ n, we buy ai items worth pi each, the formula for the total cost is a1p1 + a2p2 + … +anpn, which is precisely the definition of the dot product.

PHP, 46 Bytes

<?foreach($_GET[a]as$n)$s+=$n[0]*$n[1];echo$s;

48 Bytes

<?=array_sum(array_map(array_product,$_GET[a]));