Scripting competition

J. Landman Gay jacque at hyperactivesw.com
Mon May 11 23:09:31 EDT 2009


Brian Yennie wrote:

> function fib n
>   put 0 into thisNum
>   put 1 into nextNum
>   repeat n-1
>      put 0 into tCarry
>      put empty into tSum
>      if (length(nextNum) > length(thisNum)) then put "0" before thisNum
>      repeat with i=length(nextNum) down to 1
>         get ((char i of thisNum) + (char i of nextNum) + tCarry)
>         if (it <10) then
>            put 0 into tCarry
>            put it before tSum
>         else
>            put 1 into tCarry
>            put (it - 10) before tSum
>         end if
>      end repeat
>      if (tCarry= 1) then put "1" before tSum
>      put nextNum into thisNum
>      put tSum into nextNum
>   end repeat
>   return tSum
> end fib

Works, so I condensed a few lines, and added the ability to return the 
whole list:

function fib n
   put 0 into thisNum
   put 1 into nextNum
   repeat n-1
     put 0 into tCarry
     put empty into tSum
     if (length(nextNum) > length(thisNum)) then put "0" before thisNum
     repeat with i=length(nextNum) down to 1
       get (char i of thisNum) + (char i of nextNum) + tCarry
       put it div 10 into tCarry
       put it mod 10 before tSum
     end repeat
     if (tCarry= 1) then put "1" before tSum
     put nextNum into thisNum
     put tSum into nextNum
     put tSum & cr after tList
   end repeat
   return tList
end fib


-- 
Jacqueline Landman Gay         |     jacque at hyperactivesw.com
HyperActive Software           |     http://www.hyperactivesw.com



More information about the use-livecode mailing list