 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Senior Member
Join Date
Aug 2004
Posts
43,023

## buffer overflow

[Originally posted by drop]

I am using a function for calculate a factorial of a number but when I write a high number (ex.: 9999999) it produce an error of buffer overflow, how can I resolve this?
The function is :

Function factorial(ByVal factor As Long)
factor = factor - 1
If factor = 0 Then
factorial = 1
Exit Function
End If
factorial = factorial(factor) + (factor + 1)
End Function  Reply With Quote

2. Senior Member
Join Date
Aug 2004
Posts
43,023

## Re:buffer overflow

[Originally posted by Mike Lyons]

You're probably exceeding the capacity of the Long data type.

Try a Double and ignore the fractional portion.

Mike  Reply With Quote

3. Senior Member
Join Date
Aug 2004
Posts
43,023

## Re:buffer overflow

[Originally posted by Ed Ardzinski]

Even using long data you can only calculate the factorial up to about n=171 or so...

I wrote a code snippet that calculates it up to about n=680, but the return is a string representing the result in scientific notation.

Check out

http://www.freevbcode.com/ShowCode.Asp?ID=3530  Reply With Quote

4. Senior Member
Join Date
Aug 2004
Posts
43,023

## Re:buffer overflow

[Originally posted by Mikekay2]

You are calculating factorial incorrectly, factorial is found by multiplying not summing.
e.g. 4! = 1*2*3*4 = 24
not 1+1+2+3+4 = 11 which your code will give

The other problem apart from the number been too large is that you are recursively calling the function many times, you will have x instances of the function on the stack if x is the number to get factorial of. As the stack is only able to hold a limited number of calls before you run out of memory this may cause you code to crash.

Try using a non-recursive approach e.g:

Dim I As Long
Dim Result As Double
' if x is number to get factorial of

Result=1
For I = 2 To X
ÿ Result=Result*I
Next  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•