Shamil Salakhetdinov
shamil at smsconsulting.spb.ru
Sat Apr 24 07:28:47 CDT 2010
Thank you, Susan and Drew - your lovely chat (and Drew's original code) forced me to try to find a math based solution ;) - here it's (C#) (please corrent me if this solution will happen to be wrong): 1) Using lambda expressions: public static int GetSurvivorNumberUsingFunctionalProgramming(int arraySize) { Func<int, int, int> survivorNumber = null; survivorNumber = (lineSize, power) => Math.Pow(2, power) > lineSize / 2 ? (int)Math.Pow(2, power) : survivorNumber(lineSize, power + 1); return survivorNumber(arraySize, 1); } 2) Using iteration: public static int GetSurvivorNumberUsingIteration(int arraySize) { int power = 0; while (Math.Pow(2, ++power) <= arraySize / 2) ; return (int)Math.Pow(2, power); } 3) Using recursion: public static int GetSurvivorNumberUsingRecursion(int arraySize) { return GetSurvivorNumberUsingRecursion(arraySize, 1); } public static int GetSurvivorNumberUsingRecursion(int arraySize, int power) { if (Math.Pow(2, power) > arraySize / 2) return (int)Math.Pow(2, power); return GetSurvivorNumberUsingRecursion(arraySize, power + 1); } One can make similar iterative and recursive coding solutions using VBA. Although C#'s/VB.NET's functional programming approach is more powerful/scalable as modern compilers can compile, build and "automagically" distribute execution of functional code blocks on several processor cores... Thank you Arthur for this puzzle - it forced me to start learning C# functional programming... --Shamil P.S. Here is how folks cancluate factorials using c# functional programming (look at the bottom of this topic as it has really simple recursive solution): http://blogs.msdn.com/madst/archive/2007/05/11/recursive-lambda-expressions. aspx -----Original Message----- From: accessd-bounces at databaseadvisors.com [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Susan Harkins Sent: Saturday, April 24, 2010 1:57 AM To: Access Developers discussion and problem solving Subject: Re: [AccessD] OT: Friday Puzzles ==========Drew, can you GET any geekier??????????? ;) Susan H. --