SimpleTemplate, 81 bytes
It's fairly chunky :/
Mostly due to limitations in my language...
{@eachargv.0}{@ifloop.last is equal0}{@set*_ _,argv.1}{@/}{@incby_ R}{@/}{@echoR}
Takes a string of numbers or an array as the first argument, and the n
as the 2nd argument.
You can try this on http://sandbox.onlinephpfunctions.com/code/4c7290781560c876ff1e72e5d1680ed7b98861a3
Ungolfed:
The code above is a little weird to read...
Here's a more readable version:
{@set result 0}
{@each argv.0 as number}
{@if loop.last is equal to false}
{@set* number number, argv.1}
{@/}
{@inc by number result}
{@/}
{@echo result}
Notes:
{@set result 0}
is optional, as {@inc}
will create the variable, if it doesn't exist
{@each argv.0 as number}
doesn't need need the as number
, defaulting to the variable _
to store values
{@if loop.last is equal to false}
uses the special variable loop
to know if it is the last item in the {@each}
Also, false
is the same as 0
.
{@set* number number, argv.1}
multiplies number
with argv.1
and stores it inside number
. If number
was an array, it would multiply with all the values.
{@inc by number result}
will increment the variable result
by number
(effectivelly, result = result + number
)
{@/}
is usually optional, but, with this code, they are all required.