| Bytes | Lang | Time | Link |
|---|---|---|---|
| 054 | Tcl | 170708T001816Z | sergiol |
| 054 | Janet | 250507T213528Z | Adam |
| 035 | R | 180605T143703Z | JayCe |
| 013 | Mathematica | 170513T110906Z | Martin E |
| 010 | Japt | 170708T040758Z | Justin M |
| 012 | TIBasic | 170521T194202Z | pizzapan |
| 184 | Swift | 170515T114225Z | Caleb Kl |
| 055 | Common Lisp | 170515T013046Z | user6516 |
| 024 | dc | 170514T023839Z | R. Kap |
| 016 | Stacked | 170514T002642Z | Conor O& |
| 029 | Octave | 170513T120349Z | user4180 |
| 009 | APL Dyalog | 170513T123420Z | user4180 |
| 023 | Octave | 170513T121348Z | Кирилл М |
| 005 | Jelly | 170513T121853Z | Leaky Nu |
| 006 | 05AB1E | 170513T114220Z | sporkl |
| 037 | Python 3 | 170513T110903Z | Leaky Nu |
| 030 | JavaScript ES7 | 170513T111443Z | Arnauld |
Tcl, 54 bytes
proc S f {time {incr k
append s +[expr $f]} 5
expr $s}
proc S {f s\ 0} {time {incr k
set s [expr $s+$f]} 5
set s}
If it only worked with integers, I could golf it more!
Janet, 54 bytes
|(+;(seq[k :down[5 0]](defglobal'k k)(eval-string $)))
Example input: (+ k (/ k 2) (math/pow k 2))
R, 35 bytes
k=1:5;sum(eval(parse(t=scan(,""))))
TIO link includes a function solution as well (38 bytes)
Mathematica, 17 14 13 bytes
Thanks to Ian Miller for saving 3 bytes.
Thanks to LegionMammal978 for saving 1 byte.
#~NSum~{k,5}&
The input should be an actual expression containing k, e.g.:
#~NSum~{k,5}&[Sqrt[k]^3+4]
Japt, 10 bytes
6ÆK=XOxUÃx
Input string should have variable as an uppercase K. sqrt(K) should be inputted as K**0.5.
Explanation
eval scope didn't work in my favor; had to redefine the counting variable X as a global K.
6ÆK=XOxUÃx // implicit: U = input string
6oXYZ{K=XOxU} x // expanded
6oXYZ{ } // create array [0, 6) and map to function:
K=X // redefine the array value to global K
OxU // eval the input string
x // sum the resulting array
TI-Basic, 12 bytes
Σ(expr(Ans),K,1,5
Call with "string":prgmNAME, where string is any valid TI-Basic expression of K.
Swift, 202 184 bytes
import Foundation;func s(i:String){print([1,2,3,4,5].map{NSExpression(format:i.replacingOccurrences(of:"k",with:"\($0).0")).expressionValue(with:nil,context:nil)as!Float}.reduce(0,+))}
For some reason this will only run locally :(.
Here is an explanation of what I am doing:
import Foundation // Import the Foundation module
func s(i:String){ // Create a function that takes in a String and returns a Float
print( // Print the result of the follow algorithm to strdout
[1,2,3,4,5].map{ //Conduct the follow code on numbers 1 - 5
NSExpression(format: // Create an expression with the following String and return it
i.replacingOccurrences(of:"k",with:"\($0).0")) // Create a string replacing all ocurrances of 'k' in `i` with the current Float from the map
.expressionValue(with:nil,context:nil)as!Float // Get the resulting value of the expression
}.reduce(0,+) // Add the result of all the expressions together
)
}
Thanks to @Mr. Xcoder for saving 15 bytes!
Common Lisp, 55 bytes
(defun f(x)#.(read))(print(+(f 1)(f 2)(f 3)(f 4)(f 5)))
Example input - output:
(* x 2) - 30
(sqrt x) - 8.382333
(+ (/ x 2) x (expt x 2)) - 155/2
(expt x 2) - 55
different, longer (58 bytes) version - starts getting shorter if you do sumation from 1 to 7.
(print #.`(+,@(mapcar #'(lambda(x)#.(read))'(1 2 3 4 5))))
yet another and longer method (65 64 bytes) - doesn't define function - just inserts your expression into a loop. Should get shorter for bigger sums.
(setf a(read)b 0)(loop as x from 1 to 5 do(incf b #.a))(print b)
dc, 31 24 bytes
?sa1k[lax+Kd1+k5>p]dspxp
The input must be given in reverse-Polish notation (also known as postfix notation) and enclosed in square brackets ([]) with:
Kreplacingkas the parameter;+representing addition;-representing subtraction and_followed by any number representing a negative number;*representing multiplication;/representing division;^representing exponentiation;vrepresenting the square-root.
For example, -2*k+k+3*k**2+k**0.5-k/2 would be input as [_2K*K+K2^3*+Kv+K2/-]. This takes due advantage of the fact that K is a dc command which returns the current precision (initially set to 1). Therefore, by the end, this returns the output with a precision of 6.
Stacked, 16 bytes
5~>[@k#~]2/"!sum
5~> is a range from 1 to 5 incluive. 2/ makes a func dyadic, " is pair-wise, and ! is execute. This thus maps the range [1, 5] with the input, which is then evaluated after defining the range member to be k. Then, the results are summed.
Octave, 50 46 31 29 bytes
@(d)eval(["k=1:5;sum(" d 41])
Exponentiation is denoted with the caret .^ and multiplication is denoted with .*.
This declares an anonymous function that takes in argument d. It sets k to be equal to the range 1:5 and sums the evaluated d and returns it.
APL (Dyalog), 9 bytes
+/⍎⎕⊣k←⍳5
Addition is +, subtraction is -, multiplication is ×, division is ÷ exponentiation is * and execution is right to left, so use () to group expressions.
Input is in terms of k.
Explanation
k←⍳5 Set k to be equal to the vector 1 2 3 4 5
⊣ The left argument:
+/ Sum of
⍎⎕ The evaluated input (the eval returns an array because k is an array)
And here's a solution that takes trains as input (like the Jelly answer): +/(⍎⎕)¨⍳5.
Jelly, 5 bytes
vЀ5S
Input a valid Jelly monadic chain (I golfed them down in my link).
How it works
vЀ5S
Ѐ for each of ...
5 5 (implicitly converted to [1,2,3,4,5]), ...
v evaluate the input with the above as argument
S and find the sum
05AB1E, 8 7 6 bytes
6G¹.VO
Input is in postfix notation, and uses the variable N. 05AB1E is a stack-based language, so only postfix notation works.
Format of E(N): write the number(s) you want to do the operation with, and then write the sign of the operation. For example, 3+4 would be 3 4+, 3*4+2*3 would be 3 4* 2 3* +. Also note that this uses t instead of sqrt, and m instead of **, so sqrt(N) would be Nt.
Explanation:
6G¹.VO
6G For N in range(1,6). This includes [1,2,3,4,5].
¹.V Read and eval input.
O Sum results.
Python 3, 40 37 bytes
3 bytes thanks to Arnauld.
Eval scope tricks \o/
f=lambda s,k=5:k and eval(s)+f(s,k-1)
Uses k**0.5 instead of sqrt(k).
JavaScript (ES7), 31 30 bytes
Uses k**0.5 for sqrt(k).
f=(e,k=6)=>--k&&f(e,k)+eval(e)
console.log(f("2*k"))
console.log(f("k**0.5"))
console.log(f("k+k/2+k**2"))
console.log(f("k**2"))