--[[
---------------------------------------------------------------------------
Remote Train Control Program for Windows

© Copyright 2019 by Mark DiVecchio

	This file is part of Remote Train Control.

	Remote Train Control is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Remote Train Control is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Remote Train Control.  If not, see <http://www.gnu.org/licenses/>.

 Home Page : http://www.silogic.com/trains/RTC_Running.html
---------------------------------------------------------------------------]]
-- Semicolons are not required in Lua code
-- Time two factorial alogorithums - classic and tail recursive
title = "Factorial Test"

-- from   http://lua-users.org/wiki/DemoScripts


--- the classic recursive example:
function factorial(n)
    if n == 0 then
        return 1
    else
        return n * factorial(n-1)
    end
end

-- and its tail recursive variant
fact = function (n)
       local f
       f = function (m,a)
           if m == 0 then return a end -- if
           return f(m-1,m*a) end -- function
       return f(n,1) end -- function

function setup()
print("Start");
valx = 10;
print("factorial (classic) of ", valx, " is ", factorial(valx))
valx = 20;
N1 = MicroSeconds();
print("factorial (classic) of ", valx, " is ", factorial(valx))
N2 = MicroSeconds();
print("factorial (tail recursive) of ", valx, " is ", fact(valx))
N3 = MicroSeconds();

print("Classic Algorithm " .. N2 - N1 .. " us");
print("Tail Recursive Variant " .. N3 - N2 .. " us");

return Stop("Done");
end

