# Shorten an already short mathematical expression

For once, I was doing some real work, updating old code, and bumped into an expression that is equivalent to what would be written as πx + ex in good old-fashioned math. I thought it would be possible to write it shorter than it was written in the language I work with (APL), and therefore present this very simple challenge:

Write a function or program that (by any means) accepts zero or more numbers, and returns (by any means) the result of the above expression for x = each of the given numbers with at least 3 significant digits for each result.

If your language does not have π and/or e, use the values 3.142 and 2.718.

Scoring is number of bytes, so preface your answer with # LanguageName, 00 bytes.

Standard loop-holes are not allowed.

Edit: Now the solution I came up with, ○+*, has been found. The original code was (○x)+*x.

• What domain are the inputs drawn from? Integers, reals, complex numbers? Dec 21, 2015 at 18:36
• @MartinBüttner Whatever you like, as long as the output isn't restricted to integer.
Dec 21, 2015 at 18:41

# Emotinomicon, 48 bytes / 13 characters

I do it, not because it is short, but because it is fun. Try it here. You'll have to copy+paste it into the textbox.

😼⏪🆙😦✖😎😿➕😨😼🆙😄⏩


Explanation:

😼  ⏪   🆙  😦  ✖   😎  😿  ➕   😼  🆙  😄  ⏩   explanation
😼                                              take numeric input
⏪                                           open loop
🆙                                      duplicate top of stack
😦                                  push pi
✖                               multiply top two elements on stack
😎                          reverse stack
😿                      pop N, push e^N
➕                   add top two elements on stack
😼              take numeric input
🆙          duplicate top of stack
😄      pop N, push N+1
⏩   close loop


Here is the program in its native environment, the mobile phone: • Definitely the most entertaining expression.
Dec 21, 2015 at 19:38
• Lol, a cat for cat? Dec 21, 2015 at 19:44
• I want this language. Dec 22, 2015 at 4:07
• I suppose you could say he use sub-expressions. (•_•) ( •_•)>⌐■-■ (⌐■_■) Feb 20, 2016 at 0:09

# Dyalog APL, 3 characters

As a tacit phrase.

○+*


Monadic ○ multiplies its argument with π, monadic * is the exponential function exp. ○+* is a train such that (○+*)ω is equal to (○ω)+(*ω). Since this is APL, the phrase works for arguments of arbitrary shape, e. g. you can pass a vector of arbitrary length.

The same solution is possible in J as o.+^ with o. being ○ and ^ being *.

• :-) See "Edit:" in OP.
Dec 21, 2015 at 19:36
• So, I down voted you by mistake and only just realized. Mind making some minor edit so I can change that? Dec 24, 2015 at 16:11
• @dohaqatar7 Like this? Dec 24, 2015 at 16:43

# R, 25 24 bytes

cat(exp(x<-scan())+pi*x)


Is this it? It gets input from user, assign it to x, calculates its exponential multiply it to pi, and finally cat()prints the result.

edit: 1 bytes saved thanks to Alex A.

• Looks right to me.
Dec 21, 2015 at 18:42
• 24 bytes: cat(exp(x<-scan())+pi*x) Dec 21, 2015 at 23:43
• In this case you have to use <- as I did in my suggestion rather than = because otherwise it's setting the x argument for exp but not assigning the variable x. In a fresh session the current code will fail. Dec 23, 2015 at 16:53

# JavaScript (ES6), 39 34 bytes

Saved 5 bytes thanks to @edc65

a=>a.map(x=>x*Math.PI+Math.exp(x))


Takes input as an array of numbers, and outputs in the same format.

Thanks to the reduction, there are now three equivalent 45-byte programs, all ES5-compliant:

for(;x=prompt();)alert(x*Math.PI+Math.exp(x))


Inputs should be entered one at a time. Press OK without entering anything to quit.

The third one highlights an interesting feature in JS: the with statement. While sometimes unsafe to use (thus disabled in strict mode), it can still be used to save typing out an object name and period every time you need to access it. For example, you can do this:

x=[];with(x)for(i=0;i<5;i++)push(length);


push and length are then used as properties of x, which will result with x being [0,1,2,3,4].

This works on any object, even non-variables, so for example, you can do this:

with("0123456789ABCDEF")for(i=0;i<length;i++)alert("0x"+charAt(i)-0);


charAt and length are called as properties of the string. "0x"+x-0 converts x from a hex value to a number, so this alerts the numbers 0 through 15.

• M.pow(M.E,x) is M.exp(x) by definition Dec 21, 2015 at 21:54
• @edc65 I should learn my Math ;) Thanks! Dec 21, 2015 at 22:38
• I didn't know with was deprecated. Dec 23, 2015 at 0:57
• @CᴏɴᴏʀO'Bʀɪᴇɴ My bad; it's not deprecated, but avoiding it is highly suggested. Dec 23, 2015 at 1:51
• That's what I remember reading. ^_^ I use it anyways in <canvas> rendering and (of course) golfing. Dec 23, 2015 at 1:52

# Mathematica, 11 10 bytes

N@Pi#+E^#&


With 1 byte saved thanks to LegionMammal978.

• This currently does not work. However, for 10 bytes: 1.Pi#+E^#& Dec 22, 2015 at 13:23
• There was a space missing between # and Pi. This is solved by using Pi# in place of #Pi. Also, N only needs to be applied to Pi#, not the whole expression. Dec 22, 2015 at 15:17

# Pyth, 11 13

VQ+*N.n0^.n1N


Now takes x as a list, e.g. [1.25, 2.38, 25]

Previous (11 bytes): +*Q.n0^.n1Q

VQ            +       * N .n0            ^ .n1 N
For each      Add     List Item * Pi     e ^ List Item
input item

• When I try this with the online interpreter, it only works for a single number. Or what is the input format? The specification says that the input is "zero or more numbers", and the expression has to be evaluated for "each of the given numbers." Dec 21, 2015 at 18:47
• @RetoKoradi you can run it with multiple numbers (on separate lines) by checking the "switch to test suite" box. I'm not sure if that's allowed now that you mention it. Dec 21, 2015 at 18:51

## Seriously, 10 bytes

,;e(╦*+M


Hex Dump:

2c603b6528cb2a2b604d


Try It Online

Takes inputs as a list (see link for example).

Explanation:

,                               Get input list
      M                      Map this function over it
;                             Copy the input value.
e                            exponentiate
(                           dig up the other copy
╦*                         multiply by pi


# MATLAB, 15 bytes

@(x)pi*x+exp(x)


# TI-BASIC, 5 bytes

πAns+e^(Ans


TI-BASIC doesn't use ASCII bytes, so each of these is stored as one byte in the calculator: π, Ans, +, e^(, and Ans. It assumes the previous expression is the input (like {1,2,3}).

# Python 2, 38 bytes (52 49 bytes w. math)

lambda l:[3.142*x+2.718**x for x in l]


If I have to use the math module:

from math import*
lambda l:[pi*x+e**x for x in l]


Input should be a list of numbers

f([1,2,3,4,5])

> [5.8599999999999994, 13.671524, 29.505290232, 67.143510850576007, 164.04623849186558]

• If your language does not have π and/or e, use the values 3.142 and 2.718. ... Python has pi and e in the math module. Dec 21, 2015 at 22:33
• @ZachGates Added a version with math module. Dec 22, 2015 at 17:38
• You can save 3 bytes on the math solution by using from math import* Dec 22, 2015 at 21:37
• @wnnmaw Thanks! Dec 22, 2015 at 21:51
• You can also shave off another by using for x in l:lambda l:pi*x+e**x instead of the comprehension in both answers Dec 22, 2015 at 21:54

# MATL, 9 bytes

This answer uses the current version of the language (3.1.0), which is earlier than the challenge.

itYP*wZe+


Input is a vector containing all numbers (list enclosed by square brackets and separated by spaces, commas of semicolons), such as [5.3 -7 3+2j]. Complex values are allowed. Output has 15 significant digits.

### Example

>> matl itYP*wZe+
> [1 2 3]
5.859874482048839 13.67224140611024 29.51031488395705


### Explanation

Straightforward operations:

i       % input
t       % duplicate
YP      % pi
*       % multiplication
w       % swap elements in stack
Ze      % exponential


## MATLAB: 70 bytes

@(x)num2str(arrayfun(@(x)(round(pi*x+exp(x),2-floor(log10(pi*x+exp(x))))),x))


Test:

ans(1:10)
5.86            13.7            29.5            67.2             164             422            1120            3010            8130           22100


Explanation: There were several issues with number formatting.

Firstly, the question requires 3 sig-figs. Matlab has no built-in function for rounding by sig-figs (only by decimal places), so the following workaround was required:

floor(log10(pi*x+exp(x)))) computes the largest significant digit.

@(x)(round(pi*x+exp(x),2-floor(log10(pi*x+exp(x))))),x)) takes input x and rounds to 3 significant digits.

Another requirement was to handle multiple inputs. The above code can work only with single number. To mitigate this, we use arrayfun to evaluate the function for each vector element.

The last problem, Matlab displays the result of arrayfun with its own rounding that leads to outputs like 1.0e+04 * 0.0006 which violates the 3 sig-fig requirement. So, num2str was used to turn array into char format.

Matlab is good for numerical analysis, but, frankly, it sucks when it comes to fine number formatting

UPD: well, that's embarrassing that I confused

with at least 3 significant digits

with

with 3 significant digits

Anyway, I'll leave my answer in this form because the 15 bytes Matlab solution is already given by @costrom

• What?! Why do you have to do all that?
Dec 21, 2015 at 19:39
• Is this code-bowling? Dec 21, 2015 at 19:46
• I'll add explanations for the answer Dec 21, 2015 at 19:49
• it only says a minimum of 3 sig figs, not exactly 3. if you specified that format longg was required before running the code, you'd drop 3/4 the length here Dec 21, 2015 at 20:08
• @costrom Yes, you're right, and I concede that you win :) Dec 21, 2015 at 20:11

# Julia, 12 bytes

x->π*x+e.^x


This is an anonymous function that accepts an array and returns an array of floats. To call it, give it a name, e.g. f=x->....

Julia has built-in constants π and e for—you guessed it—π and e, respectively. The .^ operator is vectorized exponentiation.

# Japt, 12 bytes

N®*M.P+M.EpZ


Takes input as space-separated numbers. Try it online!

### How it works

N®   *M.P+M.EpZ
NmZ{Z*M.P+M.EpZ

// Implicit: N = array of inputs, M = Math object
NmZ{    // Map each item Z in N to:
Z*M.P+  //  Z times PI, plus
M.EpZ   //  E to the power of Z.
// Implicit: output last expression

• I hated to upvote you when you're at 5,554 rep. Dec 21, 2015 at 20:07

# J, 4 bytes

o.+^


Same as APL ○+*, but J's pi times function is called o., which is one byte longer.

map(\x->pi*x+exp x)


Try it online!

Edit: -3 bytes thanks to @H.PWiz

# Par, 8 bytes

✶[″℗↔π*+


Accepts input as (1 2 3)

## Explanation

               ## [implicit: read line]
✶              ## Parse input as array of numbers
[              ## Map
″             ## Duplicate
℗             ## e to the power
↔             ## Swap
π*            ## Multiply by π


# Racket, 27 bytes

map(λ(x)(+(* pi x)(exp x)))


when put in the function position of an expression:

(map(λ(x)(+(* pi x)(exp x))) '(1 2 3 4))

> '(5.859874482048838 13.672241406110237 29.510314883957047 67.16452064750341)


# CJam, 13 bytes

q~{_P*\me+}%p


Takes input as an array separated by spaces (e.g. [1 2 3]). Try it online.

### Explanation

q~    e# Read the input and evaluate it as an array
{     e# Do this for each number x in the array...
_P* e# Multiply x by pi
\me e# Take the exponential of x (same as e^x)
+   e# Add the two results together
}%
p     e# Pretty print the final array with spaces


# Reng v.3.3, 53 bytes

Noncompeting because it postdates the challenge, but hey, not winning any awards for brevity. :P Try it here!

2²5³*:"G"(%3+i#II*ZA*9+(%2+#E1II0e1+ø
1-)E*(:0eø
$+n~  ## Line 0 Here is a view of the stack in line 0: Sequence read | Stack 2² | 4 5³ | 4 125 * | 500 : | 500 500 "G" | 500 500 71 ( | 500 71 500 % | 500 0.142 3+ | 500 3.142 i | 500 3.142 <i> #I | 500 3.142 ; I <- i I | 500 3.142 <i> * | 500 3.142*<i> ZA | 500 3.142*<i> 35 10 * | 500 3.142*<i> 350 9+ | 500 3.142*<i> 359 ( | 3.142*<i> 359 500 % | 3.142*<i> 0.718 2+ | 3.142*<i> 2.718 #E | 3.142*<i> ; E <- 2.718 1II0 | 3.142*<i> 1 <i> <i> 0 e | 3.142*<i> 1 <i> <i>==0 1+ | 3.142*<i> 1 <i> (<i>==0)+1  ø then goes to the next Nth line. When 0 is input, this goes straight to line 2. Otherwise, we go to line 1. ## Line 1 1-)E*(:0eø  This multiples E i times, which is e^i. We decrement the counter (initially I), multiply the STOS (our running e power) by E, go back to the counter, and do this (i' is the current counter): Sequence read | Stack (partial) | i' : | i' i' 0 | i' i' 0 e | i' i'==0  ø then does one of two things. If the counter is not 0, then we go to the "next" 0th line, i.e., the beginning of the current line. If it is zero, then 0e yields 1, and goes to the next line. ## Line 2 $+n~


\$ drops the counter (ON THE FLOOR!). + adds the top two results, n outputs that number, and ~ quits the program.

Case 1: input is 0. The TOS is 1 ("e^0") and the STOS is 0 (pi*0). Adding them yields the correct result.

Case 2: input is not 0. The result is as you might expect.