Fibonacci Numbers, Windows Batch Programming Revisited


I have used a lot the Fibonacci numbers [previous posts] as an example to illustrate the programming concepts and ideas. Today, I still use this to revisit the concepts of programming in Windows batch, via the three different versions of implementation.

The Fib numbers are infinite sequences (non-negative integers) which are defined as follows.

Fib(n) = Fib(n - 1) + Fib(n - 2)
Fib(0) = 1
Fib(1) = 1

We all know there are two methods of computing the Fib numbers, the iterative one and the recursive one.

The first implementation would be straightforward, using the iterative approach. Using for /l %%g to generate a loop that iterates the computation.

@echo off
:: Print Fibonacci Numbers
:: https://helloacm.com

title Fibonacci Numbers

setlocal enableextensions enabledelayedexpansion
set n=15

set a=1
set b=1

set s= %a% %b%
for /l %%g in (1,1,%n%) do (
	set /a c=a+b
	set s=!s! !c!
	set /a a=b
	set /a b=c
)

echo !s!
endlocal

By replacing for using simple loop by goto and label we have slightly a different implementation, but the programming algorithm is the same.

@echo off
:: Print Fibonacci Numbers
:: https://helloacm.com

title Fibonacci Numbers

setlocal enableextensions enabledelayedexpansion

set n=15
set a=1
set b=1

set s= %a% %b%

:loop
	if !n! equ 0 goto print
	set /a n-=1
	set /a c=a+b
	set s=!s! !c!
	set /a a=b
	set /a b=c
	goto :loop

:print
	echo !s!
	echo.

endlocal

The second is not so recommended because of goto which is arguably depreciated. This approach is more BASIC-style. The simple recursive approach, however, not exactly the full recursive approach, as defined in mathematics expressions given above. But this shows the idea of calling recursive functions in Windows Batch script [see also this post on function definition in batch].

@echo off
:: Print Fibonacci Numbers
:: https://helloacm.com

title Fibonacci Numbers
setlocal enableextensions 
setlocal enabledelayedexpansion

set /a a=1
set /a maxn=17
(set s=)
for /l %%g in (1,1,%maxn%) do (
	call :fib a 1 %%g
	set s=!s! !a!
	set /a a=1
)
echo !s!
goto eof

:fib
set /a n1=%1
set /a n2=%2
set /a n3=n1+n2
set /a nn=%3-1

if %nn% gtr 0 (
	:: recursive call
	call :fib !n2! !n3! !nn!
)
:: return value to the first parameter
set %1=%n1%

:eof

endlocal

The recursive function is defined using label :fib and it takes three parameters, the first two are the iterated Fib numbers, and the third one is the depth. On every call to this function, the depth is checked. If it is zero, then no more action is required, otherwise, a single recursive call (with depth minus one) and the updated Fib numbers are carried out. The return value is stored in the %1, using set. The main processing script uses the for statement, which prints the first 17 Fib numbers. However, comparing to the first two versions, this implementation is slow, you might experience a delay when the maxn is large.

All three versions print the same results.

fib.bat Fibonacci Numbers, Windows Batch Programming Revisited algorithms batch script beginner brute force github implementation programming languages recursive tools / utilities tricks

The github link is [here].

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
627 words
Last Post: Group, Label and Functions in Windows Batch
Next Post: Make Lots of Directories with SMD.bat using Windows Batch

The Permanent URL is: Fibonacci Numbers, Windows Batch Programming Revisited

Leave a Reply