Fibonacci

  • Thread starter Thread starter J Ivan P Silvestre
  • Start date Start date
J

J Ivan P Silvestre

Hi group:
Iterative method for Fibonacci in bath:
----------------------------------------------------------------------------
:Fibonacci
if %1 leq 0 (echo Invalid Operation Exception&goto:END)
if %1 lss 3 (echo 1&goto:END) else (call :major %1&goto:END )
:major
set minusone=1
set minustwo=1
FOR /L %%a IN (3, 1, %1) do call :sum
echo %fib%
:sum
set /A fib=%minusone% + %minustwo%
set minustwo=%minusone%
set minusone=%fib%
goto:END
if you want see the 10 first elements you can do it just like in this
code:
@echo off
FOR /L %%a IN (0, 1, 10) do Call :Fibonacci %%a
pause
---------------------------------------------
maibe you need a method so mush fast so, luck now what can i do:
-----------------------------
:Fibonacci
set /a m00=0
set /a m01=1
set /a m11=1
call:dvdv %1
echo (%1) = %m01%
goto:eof

:dvdv
set /a n=%1
set /a par=%n%%%2
set /a div1=%n%/2
set /a div2=(%n%-1)/2

if NOT %1 EQU 1 (
if %par% EQU 0 (
call:dvdv %div1%
call:mult
) else (
call:dvdv %div2%
call:mult impar
)
)
goto:EOF

:mult
set /a p00=%m00%*%m00%+%m01%*%m01%
set /a p01=%m00%*%m01%+%m01%*%m11%
set /a p11=%m01%*%m01%+%m11%*%m11%

if "%1"=="impar" (
set /a m00=%p01%
set /a m01=%p00%+%p01%
set /a m11=%p01%+%p11%
) else (
set /a m00=%p00%
set /a m01=%p01%
set /a m11=%p11%
)
goto:EOF
-----------------------------------------------------
Again if you want see the 10 first elements you can do it just like in
this code:
@echo off
FOR /L %%a IN (0, 1, 10) do Call :Fibonacci %%a
pause
exit
 
J Ivan P Silvestre said:
Hi group:
Iterative method for Fibonacci in bath:
-------------------------------------------------------------------------- --
:Fibonacci
if %1 leq 0 (echo Invalid Operation Exception&goto:END)
if %1 lss 3 (echo 1&goto:END) else (call :major %1&goto:END )
:major
set minusone=1
set minustwo=1
FOR /L %%a IN (3, 1, %1) do call :sum
echo %fib%
:sum
set /A fib=%minusone% + %minustwo%
set minustwo=%minusone%
set minusone=%fib%
goto:END
if you want see the 10 first elements you can do it just like in this
code:
@echo off
FOR /L %%a IN (0, 1, 10) do Call :Fibonacci %%a
pause
---------------------------------------------
maibe you need a method so mush fast so, luck now what can i do:
-----------------------------
:Fibonacci
set /a m00=0
set /a m01=1
set /a m11=1
call:dvdv %1
echo (%1) = %m01%
goto:eof

:dvdv
set /a n=%1
set /a par=%n%%%2
set /a div1=%n%/2
set /a div2=(%n%-1)/2

if NOT %1 EQU 1 (
if %par% EQU 0 (
call:dvdv %div1%
call:mult
) else (
call:dvdv %div2%
call:mult impar
)
)
goto:EOF

:mult
set /a p00=%m00%*%m00%+%m01%*%m01%
set /a p01=%m00%*%m01%+%m01%*%m11%
set /a p11=%m01%*%m01%+%m11%*%m11%

if "%1"=="impar" (
set /a m00=%p01%
set /a m01=%p00%+%p01%
set /a m11=%p01%+%p11%
) else (
set /a m00=%p00%
set /a m01=%p01%
set /a m11=%p11%
)
goto:EOF
-----------------------------------------------------
Again if you want see the 10 first elements you can do it just like in
this code:
@echo off
FOR /L %%a IN (0, 1, 10) do Call :Fibonacci %%a
pause
exit

Why not keep it simple?
@echo off
set a=0
set b=1
set count=0
set max=10

:Loop
set /a c=%a% + %b%
:Loop
set a=%b%
set b=%c%
set /a count +=1
if %count% LSS %max% goto Loop
 
Back
Top