Newbie questions

Kostis Sagonas kostis@REDACTED
Sat Mar 25 09:47:11 CET 2006

Nick Linter wrote:
 > I was playing with Erlang for calculation over large numbers and 
 > discovered some issues:
 > 1. math:log fails with "badarg" for big integers.

An example would have helped us understand better what the issue is.
Currently, I get:

Eshell V5.5  (abort with ^G)
1> math:log(43466557686938914862637500386755014010958388901725051132915256476112292920052539720295234060457458057800732025086130975998716977051839168242483814062805283311821051327273518050882075662659534523370463746326528).

 > 3. Let me define the following function:
 >   fib(0) -> 0;
 >   fib(1) -> 1;
 >   fib(N) -> fib(N-1) + fib(N-2).
 > I have failed to convert this function _without_ put/get to be able to 
 > compute even fib(100) within reasonable period of time (I guess I did it 
 > wrong so that tail recursion was not here). Is there a way to compute 
 > fib(N), N>=100 without side effects?

Yes. Try:

-import(math, [pow/3, sqrt/1]).

fib(N) ->
  trunc((1/sqrt(5)) * (pow(((1+sqrt(5))/2),N) - pow(((1-sqrt(5))/2),N))).


PS. The performance you are experiencing in your version of fib/1
    has nothing to do with tail recursion...

More information about the erlang-questions mailing list