Newbie questions

Nick Linker xlcr@REDACTED
Mon Mar 27 06:45:06 CEST 2006

Kostis Sagonas wrote:

>Nick Linter wrote:
>An example would have helped us understand better what the issue is.
>Currently, I get:
>Eshell V5.5  (abort with ^G)
>1> math:log(...).
Well, I get the following result:

    43> math:log10(test:fib(1476)).
    44> math:log10(test:fib(1477)).
    =ERROR REPORT==== 27-Mar-2006::10:57:47 ===
    Error in process <0.181.0> with exit value:




    ** exited: {badarith,[{math,log10,



                          {shell,eval_loop,3}]} **

My system is Windows XP, Erlang R10B.

>fib(N) ->
>  trunc((1/sqrt(5)) * (pow(((1+sqrt(5))/2),N) - pow(((1-sqrt(5))/2),N))).
Good solution :-)
Now I also have different idea without using recursion. It is based on 
the following equation
[F_n  ]   [1  1]   [F_n-1]
[     ] = [    ] * [     ]
[F_n-1]   [1  0]   [F_n-2]
And we just have to calculate k-th power of the matrix [[1,1],[1,0]]. It 
is possible to do within O(log(k)).

>PS. The performance you are experiencing in your version of fib/1
>    has nothing to do with tail recursion...
Yes, exponential number of recursive calls... Thank you.

I'm sorry, but most interesting question (2nd, about number of digits in 
an integer) remains unanswered. But I guess it is very rare problem with 
numbers, so if there is no answer, I will understand.

Best regards,
Linker Nick.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the erlang-questions mailing list