randomfox (randomfox) wrote,
randomfox
randomfox

Calculate bills and hits needed to add 1 to the George Score (Lua version)

Same as the Perl version of the program, but rewritten in Lua.


#!lua

function gs(be, th, di)
    return 100 * (math.sqrt(math.log(be)) + math.log(th + 1)) * (1 - di / 90)
end

function solve1gs(fn)
    local curgs = fn(0)
    local upper = 1
    while upper < 1e9 do
	if fn(upper) - curgs >= 1 then
	    break
	end
	upper = upper * 2
    end

    local a = 0
    local b = upper
    local itercount = 0
    local xold = a

    while true do
	local fa = fn(a) - curgs - 1
	local fb = fn(b) - curgs - 1

	local newx = a - fa * (b - a) / (fb - fa)
	local newfx = fn(newx) - curgs - 1

	itercount = itercount + 1
	print(itercount .. ": " .. newx .. " " .. newfx)

	if math.abs(newx - xold) < 1e-10 * math.abs(newx) then
	    return newx
	end
	xold = newx

	if fa * newfx > 0 then
	    a = newx
	else
	    b = newx
	end
    end
end

function showneeded(hitpred, billpred)
    print("Hits needed for 1GS  = " .. string.format("%.2f", hitpred))
    print("Bills needed for 1GS = " .. string.format("%.2f", billpred))
    print("Bill/Hit ratio       = " .. string.format("%.3f", billpred / hitpred))
    print()
end

if not arg[2] then
    error("Usage: " .. arg[0] .. " bills-entered total-hits [days-inactive]")
end

billsentered = tonumber(arg[1])
if not billsentered then
    error("bills-entered argument must be a number")
end

totalhits = tonumber(arg[2])
if not billsentered then
    error("total-hits argument must be a number")
end

daysinactive = 0
if arg[3] then
    daysinactive = tonumber(arg[3])
    if not daysinactive then
	error("days-inactive argument must be a number")
    end
end

print("Bills Entered        = " .. billsentered)
print("Total Hits           = " .. totalhits)
print("Days Inactive        = " .. daysinactive)
print("George Score         = " .. string.format("%.3f", gs(billsentered, totalhits, daysinactive)))
print()

showneeded(
	(totalhits + 1) / 100, 
	billsentered * math.sqrt(math.log(billsentered)) / 50)

showneeded(
	solve1gs(function(incrhits) return gs(billsentered, totalhits + incrhits, daysinactive) end),
	solve1gs(function(incrbills) return gs(billsentered + incrbills, totalhits, daysinactive) end))

Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments