get all combination of a set of numbers

last example - here is code to calculate the fibonacci series

1,1,2,3,5,8,13,21 etc - where each term is the sum of the previous 2

Here is normal code, and recursive code - the normal code has a couple of variables to manage the calculations, and a for next loop to control the iteration, and is pretty easy to follow.

the recursive code has very little, and is surprisingly tricky to understand.
unlike other ones we looked at, where we had subs, here we have a function which returns a value - and little else! No looping - it's all smoke and mirrors .....


Code:
[COLOR="Red"]Function fibonacci_recursive(term As Long)
If term = 1 Or term = 2 Then
    fibonacci_recursive = 1
Else
    fibonacci_recursive = fibonacci_recursive(term - 1) + fibonacci_recursive(term - 2)
End If
End Function[/COLOR]


[COLOR="Blue"]Function fibonacci_normal(term As Long) As Long
Dim x As Long
Dim thisval As Long
Dim lastval As Long
Dim newval As Long

thisval = 1 'hard code the first 2 terms in the series
lastval = 1 

'so now we start the iteration at term 3
For x = 3 To term 
  newval = thisval + lastval
  lastval = thisval
  thisval = newval
Next

fibonacci_normal = newval
End Function[/COLOR]


[B][SIZE="3"]'{MAIN SUB to call both of the above methods}[/SIZE][/B]

Sub fib()
Dim term As Long
'this is the basic fbonacci series

'1,1,2,3,5,8,13,21,34,55,89,144
    

    term = 12

'evaluate the result by a normal   method, and then by a recursive method
    MsgBox ("Fibonacci series (normal code): Term " & term & " is " & fibonacci_normal(term))
    MsgBox ("Fibonacci series (recursive code): Term " & term & " is " & fibonacci_recursive(term))
End Sub
 
Last edited:
got it but in this case it permutes it, right? so 1234 and 4321 will be 2 different combinations. for my purposes i only needed each combination once.
Yes, sorry it was just an example of recursion.
 
Yes, sorry it was just an example of recursion.

just making sure i understand what's going on))))))))))
Gemma, i'm about to go to sleep and will study this later, but just wondering, what would this series be used for in real life?
 
just making sure i understand what's going on))))))))))
Gemma, i'm about to go to sleep and will study this later, but just wondering, what would this series be used for in real life?

the fibonacci series is everywhere in nature, as it relates to natural growth - eg ie each successive term grows in relation to the previous terms - so its seen in natural "growth", such as shell spirals, flower petal arrangements etc.

the ratio of successive terms converges to a ratio called the "golden mean" and amazingly successive terms are above then below the actually golden mean, differeing from it by increasingly small amounts.

just google either fibonacci or "golden mean"


the GM is about 1.6, and satisfies the equation GM -1 = 1 /GM

i'm sure it has relevance in many systems modelling chaos and natural processes, although I dont know for certain.

( not necessarily using recursion - that was just to demonstrate the principle )
 
here's an example of trying to use DIR() to recursively examine all the files in a tree....

are you saying there's no way in VBA to go through all files and folders in a folder? and this was the example of what happens if you try?
or there's a way just not by using recursion?
 
as far as fibonacci code - i can follow the normal one, as far as the recursive one - you're saying if the term is 1 or 2 then the result will be 1 in both cases, got that, so this is the one line that does all the work

fibonacci_recursive = fibonacci_recursive(term - 1) + fibonacci_recursive(term - 2)

and i understand what it does too, i think. it takes the last value and the value before last, so 3rd and 4th and sums them up, then on to 4th and 5th and sums them up, no?

i'd never be able to come up with this, but now that you wrote it - it looks very easy))))))))) seriously though, how come you said it's surprisignly tricky to understand?
 
also, to use Gemma's code my fields have to have numbers from 1 through 9. how do i assign autonumbers everytime and make sure that they start with 1 and don't skip?
 
never mind about autonumbers, solved that, i have another question about how deep is too deep for the combinations. for example, is 2 combinations out of 9 too deep? that's my worse case scenario, will i crash the program?
 
you wont crash the program with this recursion - it only has one thread - so if you are trying to get 10 items, it only pushes 10 items on to the stack - very "light"

the fibonacci one would though, for large numbers

say you are doing fibonacci number 50

it works this out by doing fib(49) + fib(48)

but it works out fib(49) by doing fib(48) and fib(47)
and it works out fib(48) by doing fib(47) and fib(46) and so on - so there are massive amounts of data being pushed on to the stack

(and takes proportionately longer to compute .... fib(35) took about 20 secs, and I crashed out of fib(50) because it was taking so long .... )
 
Last edited:
how come you said that the fibonacci recursive code is tricky to understand?
 

Users who are viewing this thread

Back
Top Bottom