| Bytes | Lang | Time | Link |
|---|---|---|---|
| 014 | Ruby rmatrix | 161002T000420Z | Jordan |
| 025 | Zsh | 210312T114746Z | roblogic |
| 001 | Vyxal s | 210604T091637Z | emanresu |
| 003 | APL Dyalog Classic | 210329T154924Z | Andrew O |
| 013 | Julia 1.0 | 210325T162724Z | amelies |
| 003 | BQN | 210314T112351Z | Razetime |
| 036 | PowerShell | 210314T104251Z | mazzy |
| 043 | PowerShell | 210314T093308Z | wasif |
| 056 | C | 210312T200805Z | wojciech |
| 014 | Julia 1.0 | 210312T205442Z | Donat |
| 033 | JavaScript | 210312T103850Z | user1006 |
| 005 | R | 210312T100003Z | Kirill L |
| 003 | 05AB1E | 210312T003238Z | Makonede |
| 003 | TIBASIC | 161004T203829Z | Hactar |
| 018 | R | 161002T144636Z | rturnbul |
| 013 | Perl 6 | 161002T205527Z | Brad Gil |
| 025 | C# | 161002T192013Z | Taco |
| 067 | Java | 161002T191732Z | Linnea G |
| 012 | TIBasic | 161002T165210Z | Timtech |
| 001 | Mathematica | 161002T032147Z | JungHwan |
| 004 | MATLAB / Octave | 161002T025639Z | Luis Men |
| 002 | MATL | 161002T025206Z | Luis Men |
| 003 | Pyth | 161002T005750Z | Steven H |
| 033 | Python | 161002T002817Z | Dennis |
| 017 | Haskell | 161002T002742Z | xnor |
| 001 | Actually | 161002T002317Z | lynn |
| 002 | Jelly | 161002T001232Z | lynn |
| 046 | PHP | 161002T000147Z | Jör |
Ruby -rmatrix, 14 bytes
If we can take two Vectors as input:
->p,a{p.dot a}
Ruby -rmatrix, 23 bytes
If we have to take arrays as input:
->p,a{Vector[*p].dot a}
Zsh, 31 25 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.
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)
PowerShell, 43 bytes
function d($a,$b){$a|%{$r+=$_*$b[$i++]};$r}
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;
}
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]]).
05AB1E, 3 bytes
øPO
ø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 *)
MATL, 2 bytes
*s
* % 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
Jelly, 2 bytes
æ.
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]));