अनुखंड:घातांकीय खोज
"इस मॉड्यूल हेतु प्रलेख अनुखंड:घातांकीय खोज/doc पर बनाया जा सकता है"
-- यह अनुखंड एक सामान्य घातांकीय खोज एल्गोरिथम प्रदान करता है। require[[strict]] local checkType = require('libraryUtil').checkType local function middlePoint(lower, upper) return math.floor(lower + (upper - lower) / 2) end local function search(testFunction, i, lower, upper) if testFunction(i) then if i + 1 == upper then return i end lower = i if upper then i = middlePoint(lower, upper) else i = i * 2 end return search(testFunction, i, lower, upper) else upper = i i = middlePoint(lower, upper) return search(testFunction, i, lower, upper) end end return function(testFunction, initialValue) checkType('घातांकीय खोज', 1, testFunction, 'function') checkType('घातांकीय खोज', 2, initialValue, 'number', true) if initialValue and (initialValue < 1 or initialValue ~= math.floor(initialValue) or initialValue == math.huge) then error( string.format( "'घातांकीय खोज' आर्गुमेंट #२ में अमान्य initialValue मान '%s' पाया गया " .. "(initialValue मान एक धनात्मक पूर्णांक होना चाहिए)", tostring(initialValue) ), 2 ) end initialValue = initialValue or 2 if not testFunction(1) then return nil end return search(testFunction, initialValue, 1, nil) end