def tak(x, y, z)unless y < xzelsetak(tak(x-1, y, z),tak(y-1, z, x),tak(z-1, x, y))endend
It is often used as a benchmark for languages with optimization for recursion.
Its original definition by Ikuo Takeuchi (竹内郁雄), however was as follows;
def tarai(x, y, z)unless y > xy # not z!elsetarai(tarai(x-1, y, z),tarai(y-1, z, x),tarai(z-1, x, y))endend
tarai is short for tarai mawashi, "to pass around" in Japanese.
John McCarthy attributed this function as tak() to commemorate Takeuchi but the y somehow got turned into the z.
This is a small, but significant difference because the original version benefits significantly by lazy evaluation.
Though written in exactly the same manner as others, the Haskell code below runs much faster.
tarai :: Int -> Int -> Int -> Int tarai x y z| x <= y = y| otherwise = tarai(tarai (x-1) y z)(tarai (y-1) z x)(tarai (z-1) x y)
You can easily accelerate this function via memoization yet lazy evaluation still wins.
The best known way to optimize tarai is to use mutually recursive helper function as follows.
def laziest_tarai(x, y, zx, zy, zz)unless y < xyelselaziest_tarai(tarai(x-1, y, z),tarai(y-1, z, x),tarai(zx, zy, zz)-1, x, y)endend
def tarai(x, y, z)unless y < xyelselaziest_tarai(tarai(x-1, y, z),tarai(y-1, z, x),z-1, x, y)endend
Here is an efficient implementation of tarai() in C: Note the an additional check for (x <= y) before z (the third argument) is evaluated, avoiding unnecessary recursive evaluation.
Patent Issued for System, Method, and Computer Program Product for Determining a Plurality of Application Settings Utilizing a Mathematical Function
Oct 11, 2012; By a News Reporter-Staff News Editor at Computer Weekly News -- NVIDIA Corporation (Santa Clara, CA) has been issued patent...
US Patent Issued to NVIDIA on Sept. 25 for "System, Method, and Computer Program Product for Determining a Plurality of Application Settings Utilizing a Mathematical Function" (American, Russian Inventors)
Sep 28, 2012; ALEXANDRIA, Va., Sept. 28 -- United States Patent no. 8,276,133, issued on Sept. 25, was assigned to NVIDIA Corp. (Santa Clara,...
US Patent Issued to SAS Institute on Nov. 13 for "Dynamic Analytical Differentiator for Obfuscated Functions in Complex Models" (North Carolina Inventors)
Nov 17, 2012; ALEXANDRIA, Va., Nov. 17 -- United States Patent no. 8,312,260, issued on Nov. 13, was assigned to SAS Institute Inc. (Cary, N. C...
"Methods for Determining Optical Power, for Power-Normalizing Laser Measurements, and for Stabilizing Power of Lasers Via Compliance Voltage Sensing" in Patent Application Approval Process
Aug 22, 2013; By a News Reporter-Staff News Editor at Politics & Government Week -- A patent application by the inventors Taubman, Matthew S....