# 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...

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

ApoorvGoel + 2 comments In testcase #1, why is the first expected output is

`2423600.1887`

?`e^20`

is`485165195.4098`

, a little over double the expected value.imad_bouteraa + 0 comments Actualy, they ask for the sum of the first 10 terms. the values expected for this challenge may represent the Taylor serie before it converges.

raypendergraph + 2 comments I seem to be having some sort of issue with stdout on this problem. I can copy the (Clojure) function and run it locally in LightTable or in lein repl and it runs great. I get the answers I expect printed but running here it always gives me "~ no response on stdout ~". Does anyone know what I might be doing wrong? My last submission is here

abhiranjan + 1 comment Hi, you have to read input by yourself from now on. I/O portion was provided only for introductory level challenges.

phil_hord + 0 comments The Scala template provided just a function definition, but the problem indicated we would need to provide our own IO. It was confusing. I tried the template as-is (without adding my own IO) and it worked. But here you reiterate the problem statement's requirement about IO. Who is correct?

dbudashny + 0 comments Got the same issue. Mine was related to (map println) not printing anything. It's caused by lazy nature of map, so I just constructed output string by using join and then printed it.

Sort 88 Discussions, By:

Please Login in order to post a comment