Wednesday, May 20, 2009

Programming Quiz of the Day/Week/Month.


Question 1: What is the value of x after this code executes?

dim j as integer, x as integer
j = 5
x = (-1 ^ (j - 1))

Question 2: What mathematical law does the following code implement?

'Globals
dim i as integer, j as integer
dim dValues() as double

Private Function DoEet() As Double

    i = UBound(dValues)
    For j = 1 To i + 1
        DoEet = DoEet + RecursiveHell(-1, 0, -1, 1)
    Next j

End Function

Private Function RecursiveHell(k As Integer, l As Integer, _
    m As Integer, x As Double) As Double
Dim temp As Double

    If k = -1 Then
        k = 0
        RecursiveHell = RecursiveHell + RecursiveHell(k, 1, k, 1)
    Else
        If l < j
            Do While m < i - (j - 1)
                temp = x * dValues(m)
                RecursiveHell = RecursiveHell + RecursiveHell(k, l + 1, m + 1, temp)
                m = m + 1
            Loop
        Else    'l = j
            Do While m < i
                temp  = x * dValues(m) * ((-1) ^ (j + 1))
                RecursiveHell = RecursiveHell + temp 
                m = m + 1
            Loop
        End If
    End If

End Function

(Might want to resize your browser so that this fits on one line:)
-----------------------------------------------------------------------------------

(Don't know what recursion is? Click here to find out.)

19 comments:

  1. Well duh! Isn't the answer 14? :)

    ReplyDelete
  2. ah crap, I fell for it again. I always think of -1 as a number just like any other and so mentally I'm alsways doing (-1) followed by everything else. Stupid order of opertations.

    ReplyDelete
  3. is the answer to #2 the law that states you must assign something to your array before you can do anything with it let alone get the Ubound of it?

    ReplyDelete
  4. We're assuming that the array is populated by the subroutine that calls this function. I didn't feel like adding error checks.

    ReplyDelete
  5. were the RecursiveHell arguments supposed to be ByVal or ByRef? Because k gets set to i+1 immediately after "RecursiveHell = RecursiveHell + RecursiveHell(k, 1, k, 1)" in the "For k = 0 To i" loop...

    I'll assume Byval. Let me know if I assumed wrong.

    ReplyDelete
  6. My understanding is that in Visual Basic, unless otherwise specified, integers, longs, singles, doubles, and strings are always passed by value. I thought only classes and the like defaulted to ByRef.

    Either way, it doesn't matter. When RecursiveHell calls itself, it always passes its own k, which is never -1, so the For loop is only run in the first call made to the function.

    ReplyDelete
  7. All function parameters in VB6 are ByRef unless otherwise specified.

    My guess is the mathematical law is the one that weeds out programmers that declare single-letter loop index variables at the module level.

    ReplyDelete
  8. Since k is passed byref in the first loop as the third parameter, any references to m in that instance of the function will modify k and change the behavior of the loop in the caller. Should we assume the function parameters should be ByRef?

    ReplyDelete
  9. Removed redundant "k" loop.

    Parameters are ByRef.

    ReplyDelete
  10. dValues = {1, 1.1, 1.2, 1.3, 1.4, 1.5}
    RH(-1, 0, -1, 1)
    RH(0, 1, 0, 1)
    RH Returning 6
    RH Returning 6
    RH(-1, 0, -1, 1)
    RH(0, 1, 0, 1)
    RH(0, 2, 1, 1)
    RH Returning -5
    RH(1, 2, 2, 1.1)
    RH Returning -4.29
    RH(2, 2, 3, 1.2)
    RH Returning -3.24
    RH(3, 2, 4, 1.3)
    RH Returning -1.82
    RH Returning -14.35
    RH Returning -14.35
    RH(-1, 0, -1, 1)
    RH(0, 1, 0, 1)
    RH(0, 2, 1, 1)
    RH(0, 3, 2, 1.1)
    RH Returning 4.29
    RH(0, 3, 3, 1.2)
    RH Returning 3.24
    RH Returning 7.53
    RH(1, 2, 2, 1.1)
    RH(1, 3, 3, 1.32)
    RH Returning 3.564
    RH Returning 3.564
    RH(2, 2, 3, 1.2)
    RH Returning 0
    RH Returning 11.094
    RH Returning 11.094
    RH(-1, 0, -1, 1)
    RH(0, 1, 0, 1)
    RH(0, 2, 1, 1)
    RH(0, 3, 2, 1.1)
    RH Returning 0
    RH Returning 0
    RH(1, 2, 2, 1.1)
    RH Returning 0
    RH Returning 0
    RH Returning 0
    RH(-1, 0, -1, 1)
    RH(0, 1, 0, 1)
    RH(0, 2, 1, 1)
    RH Returning 0
    RH Returning 0
    RH Returning 0
    RH(-1, 0, -1, 1)
    RH(0, 1, 0, 1)
    RH Returning 0
    RH Returning 0
    DoEet() returned 2.744

    No idea.

    ReplyDelete
  11. Hint: the law is question would have the values in dValue be between 0 and 1.

    ReplyDelete
  12. I'll post the answer tomorrow, if no one has figured it out.

    ReplyDelete
  13. Running it with 20 values takes a while.

    ReplyDelete
  14. I ran with 10 groups of random numbers of random values, resulting in apparently random results: View results. I can discern no obvious pattern.

    ReplyDelete
  15. I don't know. If you through in a couple of Debug.Print lines around the end of the routines you do find some kind of pattern. For instance a 10 element array with each element = .5 gets you:

    ReplyDelete
  16. RecursiveHell = 4.5
    RecursiveHell = 4.5
    DoEet + RecursiveHell(-1, 0, -1, 1) = 4.5
    RecursiveHell = -2
    RecursiveHell = -1.75
    RecursiveHell = -1.5
    RecursiveHell = -1.25
    RecursiveHell = -1
    RecursiveHell = -0.75
    RecursiveHell = -0.5
    RecursiveHell = -0.25
    RecursiveHell = -9
    RecursiveHell = -9
    DoEet + RecursiveHell(-1, 0, -1, 1) = -4.5
    RecursiveHell = 0.875
    RecursiveHell = 0.75
    RecursiveHell = 0.625
    RecursiveHell = 0.5
    RecursiveHell = 0.375
    RecursiveHell = 0.25
    RecursiveHell = 3.375
    RecursiveHell = 0.75
    RecursiveHell = 0.625
    RecursiveHell = 0.5
    RecursiveHell = 0.375
    RecursiveHell = 0.25
    RecursiveHell = 2.5
    RecursiveHell = 0.625
    RecursiveHell = 0.5
    RecursiveHell = 0.375
    RecursiveHell = 0.25
    RecursiveHell = 1.75
    RecursiveHell = 0.5
    RecursiveHell = 0.375
    RecursiveHell = 0.25
    RecursiveHell = 1.125
    RecursiveHell = 0.375
    RecursiveHell = 0.25
    RecursiveHell = 0.625
    RecursiveHell = 0.25
    RecursiveHell = 0.25
    RecursiveHell = 0
    RecursiveHell = 9.625
    RecursiveHell = 9.625
    DoEet + RecursiveHell(-1, 0, -1, 1) = 5.125
    RecursiveHell = -0.375
    RecursiveHell = -0.3125
    RecursiveHell = -0.25
    RecursiveHell = -0.1875
    RecursiveHell = -1.125
    RecursiveHell = -0.3125
    RecursiveHell = -0.25
    RecursiveHell = -0.1875
    RecursiveHell = -0.75
    RecursiveHell = -0.25
    RecursiveHell = -0.1875
    RecursiveHell = -0.4375
    RecursiveHell = -0.1875
    RecursiveHell = -0.1875
    RecursiveHell = 0
    RecursiveHell = -2.5
    RecursiveHell = -0.3125
    RecursiveHell = -0.25
    RecursiveHell = -0.1875
    RecursiveHell = -0.75
    RecursiveHell = -0.25
    RecursiveHell = -0.1875
    RecursiveHell = -0.4375
    RecursiveHell = -0.1875
    RecursiveHell = -0.1875
    RecursiveHell = 0
    RecursiveHell = -1.375
    RecursiveHell = -0.25
    RecursiveHell = -0.1875
    RecursiveHell = -0.4375
    RecursiveHell = -0.1875
    RecursiveHell = -0.1875
    RecursiveHell = 0
    RecursiveHell = -0.625
    RecursiveHell = -0.1875
    RecursiveHell = -0.1875
    RecursiveHell = 0
    RecursiveHell = -0.1875
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = -4.6875
    RecursiveHell = -4.6875
    DoEet + RecursiveHell(-1, 0, -1, 1) = 0.4375
    RecursiveHell = 0.15625
    RecursiveHell = 0.125
    RecursiveHell = 0.28125
    RecursiveHell = 0.125
    RecursiveHell = 0.125
    RecursiveHell = 0
    RecursiveHell = 0.40625
    RecursiveHell = 0.125
    RecursiveHell = 0.125
    RecursiveHell = 0
    RecursiveHell = 0.125
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0.53125
    RecursiveHell = 0.125
    RecursiveHell = 0.125
    RecursiveHell = 0
    RecursiveHell = 0.125
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0.125
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0.65625
    RecursiveHell = 0.65625
    DoEet + RecursiveHell(-1, 0, -1, 1) = 1.09375
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    DoEet + RecursiveHell(-1, 0, -1, 1) = 1.09375
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    DoEet + RecursiveHell(-1, 0, -1, 1) = 1.09375
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    DoEet + RecursiveHell(-1, 0, -1, 1) = 1.09375
    RecursiveHell = 0
    RecursiveHell = 0
    RecursiveHell = 0
    DoEet + RecursiveHell(-1, 0, -1, 1) = 1.09375
    RecursiveHell = 0
    RecursiveHell = 0
    DoEet + RecursiveHell(-1, 0, -1, 1) = 1.09375

    ReplyDelete
  17. It looks like something... but I'm too rusty to pick anything out. Also replacing each array element with .1 looks like some kind of pattern.

    ReplyDelete
  18. Well, it seems to take a set of real numbers and multiplies them against each other, alternately adding/subtracting the products, although with more than 3 numbers I can't keep track of the recursion in my head. It sort of reminds me of some of the approximations of PI as a sum of a series of fractions.

    ReplyDelete
  19. Rats, I thought I had something there. If you set dValues(n) = 1/(2n+3), so dvalues = {1/3, 1/5, 1/7, etc.}, it approaches pi/4 but exceeds it at 17 terms.

    At this point I'm assuming it's some mathematical law that predicts how much time people will waste because of the internet. Other than that, I can't remember anything it resembles (I took my last math class 9 years ago!).

    Tom: take out your recursive logging statements to speed things up. I can do 30 terms in ~30 sec.

    ReplyDelete

Note: Only a member of this blog may post a comment.

Current Earth-Destruction Status This page is Class A WWW Depricated

Lilypie Third Birthday tickers
Lilypie First Birthday tickers
All material on these pages is ©2003-2010 by Joe Belland, Dave Belland, Tom Adams, Cari Burud and/or Paul Harold except for the stuff that we blatantly stole from other sources. All rights reserved.