20
2
For those who didn't know, Sigma is a Greek letter which is heavily used in mathematics as the summation sign. Given a string representing an expression depending on \$k\$ that we will denote \$E(k)\$, compute the sum of the results for \$E(k)\$, for each \$k\in\{1,2,3,4,5\}\$. Concisely, you should find \$S\$ such that:
$$S=\sum^5_{k=1}E(k)$$
An example of an expression: \$E(k)=\frac k 2 + k^2\$
Specs
- You are guaranteed:
- that the expression is valid, so it contains no errors, and that it's dependent of the syntax chosen (e.g: if you only support
2*k
, there will be no2k
) - to only have defined values amongst your results, so no values like
1/0
,inf
ornan
will appear
- that the expression is valid, so it contains no errors, and that it's dependent of the syntax chosen (e.g: if you only support
- You can assume that the expression above is fitting the limits of the programming language of your choice, so it won't result in overflows or any other limit-related errors
- Any other non-whitespace ASCII Character instead of
k
can be chosen - Your program must support the following operations:
- addition (
+
,plus()
,add()
,sum()
) - subtraction (
-
,minus()
,subtract()
) - exponentiation (
**
,^
,pow()
or others, should be specified), with support to negative bases and exponents - square root in the form of
sqrt(k)
,k^0.5
,k**0.5
, or however else you wish - multiplication and division
- addition (
- The submission can either be a full program or a function, while mentioning its usage
- Any trailing / leading whitespace is allowed when outputting
- Minimum precision: 2 decimal places
Test Cases (with k
)
+---------------+--------------+
| Input = E(k) | Output |
|---------------+--------------|
|2*k | 30 |
|---------------+--------------|
|sqrt(k) | 8.38 | (* with minimum decimal precision)
|---------------+--------------|
|k+k/2+k**2 | 77.5 |
|---------------+--------------|
|k**2 | 55 |
+---------------+--------------+
The score will be the number of bytes (of the source + compiler flags). The lowest scoring valid submission wins, while taking note that these loopholes are strictly forbidden. Here is a Python pseudo-code, to make things clearer.
Can we take
plus()
instead of+
? (Same questions for all other operators too) – Stewie Griffin – 2017-05-13T11:08:01.747@StewieGriffin Yes,
plus()
,add()
,sum()
and equivalents are allowed. See the edit. – Mr. Xcoder – 2017-05-13T11:08:42.650No, you may only take the expression once, @ComradeSparklePony – Mr. Xcoder – 2017-05-13T11:21:01.830
Can we use postfix notation? For example, test case 3 from the top would be something like:
N N2/+N2**+
. – Comrade SparklePony – 2017-05-13T11:33:40.337That's very strange, but it is allowed as long as you clearly state the "format" of
E(x)
@ComradeSparklePony – Mr. Xcoder – 2017-05-13T11:35:22.077@Mr.Xcoder That isn't very strange. There are languages which use the postfix notation, as well as languages which use the prefix notation. Putting those expressing in our native notation makes
eval
easier. – Leaky Nun – 2017-05-13T11:36:04.403@LeakyNun yes, I have totally understood the purpose, and thus have allowed it, but I said that it's strange for the tester, not to the author. – Mr. Xcoder – 2017-05-13T11:37:31.483
Does the program need to support multiplication and division? It's in the test cases, but not in the specs. – pajonk – 2017-05-13T14:29:33.447
Oh yes, sorry @pajonk yes, it must. – Mr. Xcoder – 2017-05-13T14:30:13.067
Borderline dupe – Peter Taylor – 2017-05-13T14:36:03.693
Do you really think this is a dupe @PeterTaylor ? I would say it is loosely related. – Mr. Xcoder – 2017-05-13T14:37:20.177
IMO the hard work is parsing the expression. Substituting a value for the string
k
and looping from 1 to 5 are pretty trivial in comparison. The main reason I didn't actually vote to close is that it's not clear what range of parameters for^
the other question requires, so I don't know whethersqrt
is covered. – Peter Taylor – 2017-05-13T14:39:02.587@PeterTaylor that one also restricts using
eval
, that's why I say it is just related – Mr. Xcoder – 2017-05-13T14:41:33.647Can we take something like
expression(k+k/2+k**2)
as input instead of a string? – JayCe – 2018-06-05T14:38:31.527@JayCe Like taking a black-box function representing the expression
E(k)
and just doingsum(map(E,1...5))
(pseudo-code)? If so, no. – Mr. Xcoder – 2018-06-05T14:40:16.033