[erlang-questions] Fibonacci with escript (more fun...) [with CRASH]
Roger Larsson
roger.larsson@REDACTED
Fri Apr 13 19:15:35 CEST 2007
Found this
http://www.math.utah.edu/~beebe/software/java/fibonacci/
so I thought - what about Erlang? My attempt in escript is attached
# chmod +x fibmini.erl
this runs well for a lot higher inputs than most other languages
# ./fibmini.erl 100
and even
# ./fibmini.erl 1000
produces some output (you need to modify format)
But
# ./fibmini.erl 2000
ends with
"- - -
1475 ****************************** 1.61803398874989
0.000000000000000000
escript: script failed with error reason badarith
"
Why?
/RogerL
-------------- next part --------------
#!/usr/bin/env escript
%% -*- erlang -*-
%% As all ints in Erlang are bigints which simplifies the code alot.
%% Division / gives float result (there are 'div' and 'mod' operators)
%% This sample is written using the new escript (Erlang script)
%% # chmod +x fibomini.erl
%% # ./fibomini.erl 100
calc(Max, _, _, _, Max) -> ok; % end at generation
calc(Max, Golden, Lo, Hi, N) ->
Ratio = Hi/Lo, Err = Ratio - Golden,
io:format("~4w ~30w ~19.15g ~23.18f~n",[N, Hi, Ratio, Err]),
calc(Max,Golden, Hi, Lo+Hi, N+1).
calc(Max) ->
Golden=(1 + math:sqrt(5)) / 2, N=1,
calc(Max+1, Golden, 1, 1, N).
main([String]) -> calc(list_to_integer(String)).
More information about the erlang-questions
mailing list