# Evaluating e^x

# Evaluating e^x

pacefist4 + 2 comments Guys, could you please stop demanding using that

**** input and output?The program became unreadable and the code is useless (clojure). The

*functional languages*are usually needed for systems with high concurrency and need for memory sharing.What is the sence of using that output (precision with 4 figures, every number must be on the separate line), reading lines in cycle and prohibition of

*def, defn*usage?It took 10 minutes to write that function. It is an easy task. But it took about an hour to make it compatible with the output/input format and without def, defn.

Of course, I passed all tests, but I lost too much time on useless tasks.

Could you please just ask to write a

*function*which returns the result? The first tasks did it well. Handling the output/input is a useless task if your function returns correct array/listmiguel_m_fontes + 0 comments That annoy me as hell too. On a real env i will tackle that kind of task but here is a place where i would REALLY like to just flex my brain with some cool challenges.

blubberdiblub + 0 comments I second that notion. (It's a similar problem for other languages, like Racket.)

I mean it's the

**Functional Programming**track for god's sake. Why do I have to use imperative or semi-imperative constructs?

schmitzjake + 0 comments Why no def and defn for clojure? Not using them as placeholders for values. Just breaking up a few functions for readability, can't keep track of the parens all that well otherwise. Also there was no mention of the I/O handling changing from the other challenges. This exercise is quite frustrating.

zolee + 2 comments Hint for the easier understainding:

1st term = 1

2nd term = x

3rd Term = x^2/2!

10th term = x^9/9!

noah4 + 3 comments Alternatively:

0th term: x^0/0!

1st term: x^1/1!

2nd term: x^2/2!

nth term: x^n/n!

VaNTa + 1 comment Not exactly, as when x=0 you got 0^0...

hackerrank152 + 1 comment yes, and x^0 is 1, by definition, and 0! is 1, by definition, and thus it's 1/1 = 1, as required.

jcisio + 0 comments The first term should explicitely be 1.

x^0 = x^(1-1) = x/x = 1 if and only if x != 0. When x = 0 you don't know (by definition). E.g. 0^x is 0 so 0^0 is 0. Btw we generally accept that 0^0 = 1 for (more) convenient.

balintant + 1 comment Never divide with

`0`

:)miguel_m_fontes + 0 comments It's a division by 0!, which is mostly know as 1 :P

chun_yaplim + 0 comments "10th term" stated in the question is actually the 9th term in your definition.

miguel_m_fontes + 0 comments Thanks for the hint!

On my head, i see this challenge like this:

Hope this helps as well.

stefichjo + 2 comments Hint for the Haskeller: Use

`**`

together with`/`

. (`^`

expects`Integral`

and`/`

expects`Fractional`

. No instance arises.`**`

on the other hand expects`Fractional`

, too, and`Double`

works.)onecrane + 1 comment Could you help me understand why I'm running out of memory (I'm assuming a stack overflow) with this code?

solve :: Double -> Double solve 0.0 = 1.0 solve x = pow 1.0 5 pow :: Double -> Integer -> Double pow n 0 = 1.0 pow n p = n * pow n p-1 fact :: Integer -> Double fact 0 = 1 fact 1 = 1 fact n = fromIntegral n * fact (n-1)

Obviously this isn't meant to be a solution, just highlighting what seems to be the cause. pow 1.0 5 should be 1.0 * (1.0 * (1.0 * (1.0 * (1.0 * (1.0))))), easy peasy, and I'm afraid I haven't at all figured out how to debug in Haskell yet. I'd appreciate any help.

onecrane + 1 comment Uuuugh... think I figured it out. Instead of:

pow n p-1

I needed:

pow n (p-1)

Is there any sort of visualizer I can use to tell how Haskell is breaking down expressions? Because that would help.

miguel_m_fontes + 0 comments Don't think you will find one of those. My suggestion is that you pratictice your eye to see code as left associative.

Functions has the higher precedente possible and infix operators like '-' are low precedence. Thus, we can see that 'pow' will be evaluated ASAP on that expression, consuming it's arguments on the right. If you don't state that (p-1) MUST be evaluated first, we're left with:

((pow n p) - 1)

Hence, your memory problems.

miguel_m_fontes + 0 comments (^^) Is also an option, since we are dealing with Integer exponents.

(^^) :: (Fractional a, Integral b) => a -> b -> a

(**) is mostly for floats

(**) :: Floating a => a -> a -> a

There's some nice info on the link below, also. https://wiki.haskell.org/Power_function

m4ks_io + 1 comment This is ridiculus, Scala stub does not even compile, another task with improper code. Total mess.

xavier_guihot + 0 comments To make it compile, replace: "int a0 = 0;" with "var a0 = 0"

But even this way this just java code translated to scala

griffk04 + 0 comments A very poor code stub for scala

object Solution { def main(args: Array[String]) { val sc = new java.util.Scanner (System.in); var n = sc.nextInt(); int a0 = 0; while(a0 < n){ var x = sc.nextDouble(); a0+=1; } } }

first: this challenge bans var and val.

second: ignoring the first there are 2 places where it should have used val instead of var.

third: semicolons;

forth:

`int a0 = 0`

isn't even valid scala syntax

wayoung + 0 comments The default scala code doesn't make any sense. I don't even think it's real scala...

jmbuchanan1995 + 2 comments Complete Haskell solution with different main function that is far more workable.

`main = interact $ unlines . map solve . map (read :: String -> Double) . tail . words e :: Double -> [Double] e x = [x**y / (product [1..y]) | y <- [0..]] solve :: Double -> String solve x = show `$ sum $` take 10 (e x)`

ftreale + 0 comments I don't (yet) know much about how lazy valuation works. So I have a doubt about how this code is evaluated.

Can someone explain if the factorials here are being calculated from 1 for each y? I'm assuming x**y are being calculated from scatch each time, I would also like to know if I'm wrong about that.

taufeqmdr + 0 comments Yeah definitely, this is a very readable main function. Thanks for the upload.

PaGrom + 0 comments Clojure:

(use '[clojure.string :only (split triml)]) (defn fact[x] (if (<= x 1) 1 (* x (fact (- x 1)) ))) ( let [ n_t (read-line) n (Integer/parseInt n_t) ] ( loop [a0 n] (when (> a0 0) ( let [ x_t (read-line) x (Float/parseFloat x_t) ] (println (reduce + (for [i (range 10)] (/ (Math/pow x i) (fact i)) ) ) ) ) (recur (- a0 1) ) ) ) )

kiran_allu + 1 comment My submission has failed because the values didn't match the expected output for some test cases.

The value of e(5.0000f) is given as 143.6895 in the problem. My program is getting 148.41316. I also checked with couple of online calculators(https://www.easycalculation.com/e-power-x.php), the value is 148. Also java.lang.Math.exp returns 148.

Am I missing something in the problem statement? Can somone please help.

jpathAsked to answer + 1 comment **Endorsed by kiran_allu**You are supposed to only sum the first 10 terms of the series. Your result is too accurate.

kiran_allu + 1 comment Thank you. I should have read the problem statement more carefully instead of assuming the default e^x.

jpath + 1 comment No Problem. ;)

sakthivelece402 + 0 comments can i help you

Sort 99 Discussions, By:

Please Login in order to post a comment