-
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
-
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
-
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
-
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|