<div dir="ltr">I would change <div><br><div><div style="font-size:16px">fact(N) -></div><div style="font-size:16px">  case N of</div><div style="font-size:16px">    0 -> 1;</div><div style="font-size:16px">    1 -> 1;</div><div style="font-size:16px">    _ -> N * fact(N - 1)</div><div style="font-size:16px">  end.</div></div></div><div style="font-size:16px"><br></div><div style="font-size:16px">to</div><div style="font-size:16px"><br></div><div style><div style><span style="font-size:16px">fact(N) -></span></div><div style><span style="font-size:16px">  fact_tr(N, 1).</span></div><div style><span style="font-size:16px"><br></span></div><div style><span style="font-size:16px">fact_tr(N, Acc) -></span></div><div style><span style="font-size:16px">  case N of</span></div><div style><span style="font-size:16px">    0 -> Acc;</span></div><div style><span style="font-size:16px">    1 -> Acc;</span></div><div style><span style="font-size:16px">    _ -> fact_tr   (N - 1, N * Acc)</span></div><div style><span style="font-size:16px">  end.</span></div><div style><span style="font-size:16px"><br></span></div><div style><span style="font-size:16px">To make it more efficient</span></div><div style><span style="font-size:16px"><a href="http://stackoverflow.com/questions/33923/what-is-tail-recursion">http://stackoverflow.com/questions/33923/what-is-tail-recursion</a></span><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Mar 3, 2015 at 6:32 PM, Harit Himanshu <span dir="ltr"><<a href="mailto:harit.subscriptions@gmail.com" target="_blank">harit.subscriptions@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Richard and Alex<div><br></div><div>Thanks a lot for your help. I got help up with my day job and could not try it out. Thanks a lot for your help on this problem.  </div><div>I tried a different version and it looks like following. Let me know if you see any issues with that</div><div><br></div><div><div>-module(solution).</div><div>-export([main/0]).</div><div><br></div><div>main() -></div><div>  {ok, [N]} = io:fread("", "~d"),</div><div>  Data = read_input([], N),</div><div>  [io:format("~p~n", [e_x(X)]) || X <- Data].</div><div><br></div><div>read_input(Input, N) -></div><div>  case N of</div><div>    0 -> lists:reverse(Input);</div><div>    _ -> {ok, [Num]} = io:fread("", "~s"),</div><div>      {NumFloat, _} = string:to_float(Num),</div><div>      read_input([NumFloat | Input], N - 1)</div><div>  end.</div><div><br></div><div>e_x(X) -></div><div>  1 + lists:sum([math:pow(X, N) / fact(N) || N <- lists:seq(1, 9)]).</div><div><br></div><div>fact(N) -></div><div>  case N of</div><div>    0 -> 1;</div><div>    1 -> 1;</div><div>    _ -> N * fact(N - 1)</div><div>  end.</div></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Feb 25, 2015 at 4:38 PM, Alex Alvarez <span dir="ltr"><<a href="mailto:eajam@hotmail.com" target="_blank">eajam@hotmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
  
    
  
  <div text="#000000" bgcolor="#C0C0C0">
    <div>Or just simply...<br>
      <br>
      <br>
      -module(e_fun).<br>
      <br>
      -export ([start/2]).<br>
      <br>
      start (N, X) -><br>
        1 + step (N - 1, 1, X, 1.0, 0).<br>
      <br>
      step (0, _, _, _, S) -> S;<br>
      step (N, NN, X, Z, S) -><br>
          ZZ = Z * X/NN,<br>
        step (N - 1, NN + 1, X, ZZ, S + ZZ).<br>
      <br>
      <br>
      Here I'm just reusing the each previous term (ZZ) of the Taylor
      series to compute the next term and then add it up to the sum (S).<br>
      <br>
      Cheers,<br>
      Alex<div><div><br>
      <br>
      <br>
      On 02/24/2015 10:27 PM, Richard A. O'Keefe wrote:<br>
    </div></div></div>
    <blockquote type="cite"><div><div>
      <pre>On 25/02/2015, at 11:00 am, Harit Himanshu <a href="mailto:harit.subscriptions@gmail.com" target="_blank"><harit.subscriptions@gmail.com></a> wrote:

</pre>
      <blockquote type="cite">
        <pre>I am trying to learn Functional Programming and Erlang by practicing problems available online. 

One question where I don't know about solving it is  

The series expansion of ex is given by:

1 + x + x2/2! + x3/3! + x4/4! + .......

Evaluate e^x for given values of x, by using the above expansion for the first 10 terms.


The problem statement could be found here

Can someone guide me how this could be achieved in Erlang?
</pre>
      </blockquote>
      </div></div><pre>The first 10 terms of exp(X) are
     X    X^2   X^3         X^9
1 + --- + --- + --- + ... + ---
     1     2     6           9!
This is a polynomial.  We can evaluate it cheaply using
Horner's Rule.  Any time you have a polynomial to
evaluate, you should think about using Horner's Rule.

     X     /     X    /      X        /      X  \   \ \
1 + --- * | 1 + --- * | 1 + --- * ... | 1 + --- | ...| |
     1     \     2    \      3        \       9 /   / /

We can see a building block here: 1 + (X/n)*Rest.

So let's make a function for the building block:

step(X, N, Rest)
  when is_float(X), is_float(N), is_float(Rest) ->
    1.0 + (X/N)*Rest.

There are 9 steps, which is small enough to do by hand:
(NINE steps to get TEN terms.  I hope that's clear.)

exp(X)
  when is_float(X) ->
    step(X, 1.0, step(X, 2.0, step(X, 3.0,
    step(X, 4.0, step(X, 5.0, step(X, 6.0,
    step(X, 7.0, step(X, 8.0, step(X, 9.0, 1.0))))))))).


I should point out that this is a lousy way to compute
exp(X) for abs(X) "large".  Waving hands vaguely, you
want X^10/10! "small".  The relative error for X = 1.0
is -1.1e-7, for X = 2.0 is -4.6e-5, and for X = 4.0 is
a scary -0.0081.

One technique that's used in cases like this is
RANGE REDUCTION.  e^x = 2^(1.442695... * x).
To begin, scale x by log of e to the base 2,
and separate that into an integer part N and a fraction
part F.  e^x is then 2^(N+F) = 2^N*2^F.  Since F is
"small", we can use something like this polynomial.
And then we can use the equivalent of C's ldexp() to
handle the 2^N* part.  Or we could if Erlang _had_ an
equivalent of ldexp().

You don't really need the is_float/1 tests; I put them in
so that the compiler could generate better native code.

_______________________________________________
erlang-questions mailing list
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a>


</pre>
    </blockquote>
    <br>
  </div>

<br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div>