# functional math solving 15! produces overflow error

Page 1 of 1

## 9 Replies - 1535 Views - Last Post: 03 June 2009 - 10:59 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=107670&amp;s=810d0d055755931290debca3d08653e2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 icomeinpieces

Reputation: 0
• Posts: 5
• Joined: 29-May 09

# functional math solving 15! produces overflow error

Posted 29 May 2009 - 05:44 PM

i'm making a program to do binomial expansion math for me to check my math work to compare answers with

in this type of math you need to get answers for ncr equations which in involve finding out the answer to say 15! (15 * 14 * 13.... and so on)

this code here will do fine it will get my ncr math me like it will provide the answer to 3c2 which is 10 using the n! / (R1 * (n-r)!) formula but when ever i use something like 15c15 for example or anything for a r variable the "i" variable in my code which essentially represents n! will produce a overflow error. as you can see my method take 1 * 1 at first which is fine then 1* 2 *3* 4 and so on till it hits whatever value i put in the text box for it x reaches 13 and crashes with said error. now assuming i have the variables declared right and i do have code to produce the result in another text box i just didn't include it here.

so my question here is why can say a calculator like say speed crunch can handle huge numbers like that and this code can't?

``` For x = 1 To Val(power.Text)
i = i * Val(x)
Next
For z = 0 To Val(power.Text)
nr = Val(power.Text) - Val(z)
ii = 1
ii2 = 1
For w = 0 To Val(z)
ii = ii * Val(w)
If ii = 0 Then
ii = 1
End If
Next
For v = 0 To Val(nr)
ii2 = ii2 * Val(v)
If ii2 = 0 Then
ii2 = 1
End If
Next
p = Val(i) / (Val(ii) * Val(ii2))
For x = 1 To Val(z)
u = Val(power.Text) - x
d = x
Next
next
```

Is This A Good Question/Topic? 0

## Replies To: functional math solving 15! produces overflow error

### #2 thava

• D.I.C Lover

Reputation: 180
• Posts: 1,607
• Joined: 17-April 07

## Re: functional math solving 15! produces overflow error

Posted 29 May 2009 - 06:02 PM

first thing is for next is not completed corrrectly
then you have to show your declaration because this will also lead some cause like this

### #3 icomeinpieces

Reputation: 0
• Posts: 5
• Joined: 29-May 09

## Re: functional math solving 15! produces overflow error

Posted 30 May 2009 - 12:16 AM

thava, on 29 May, 2009 - 05:02 PM, said:

first thing is for next is not completed corrrectly
then you have to show your declaration because this will also lead some cause like this

sorry i just took this part of the code there is a proper next code but it was further down and it didn't get included and i stated that you could assume there was proper declarations for the variables.

but here is the code
it's a bit messy and not commented on yet still testing it to make sure everything works right but if i put in 34 in the power.text field this specific code located about halfway down
```vf(cvf) = va(cvf) + vb(cvf)
```
will come up overflow error while cvf equal 17 and the actual va and vb variables are very huge numbers at this point

```Public Class Form1
Dim tb As String
Dim tm As String
Dim a As String
Dim b As String
Dim ai As Integer
Dim bi As Integer
Dim aih As String
Dim bih As String
Dim containa As String
Dim containb As String
Dim contain As String
Dim nth As String
Dim an As String
Dim bn As String
Dim bd As Integer
Dim bds As String
Dim i As Integer
Dim ii As Integer
Dim ii2 As Integer
Dim nr As Integer
Dim p As Integer
Dim u As Integer
Dim d As Integer
Dim abd As Integer
Dim abn As Integer
Dim abdf As Integer
Dim deno As Integer
Dim nume As Integer
Dim rgxExp As New System.Text.RegularExpressions.Regex(" ")

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
If F_R.Checked = True Then
nume = Val(numerator.Text)
deno = Val(denominator.Text)
For r = nume To 1 Step -1
If (nume Mod r) + (deno Mod r) = 0 Then
nume = nume / r
deno = deno / r
r = 1
End If
numerator.Text = nume
denominator.Text = deno
Next
End If

'------------------------------------------------------

If b_e.Checked = True Then
TextBox6.Text = ""
term.Text = ""
Dim x As Integer
x = Val(power.Text) + 2
Dim f(x) As String
Dim va(x) As Integer
Dim vb(x) As Integer
Dim vf(x) As Integer
If aend.Text <> "" And bend.Text <> "" Then
nth = 1
i = 1
For x = 1 To Val(power.Text)
i = i * Val(x)
Next
For z = 0 To Val(power.Text)
nr = Val(power.Text) - Val(z)
ii = 1
ii2 = 1
For w = 0 To Val(z)
ii = ii * Val(w)
If ii = 0 Then
ii = 1
End If
Next
For v = 0 To Val(nr)
ii2 = ii2 * Val(v)
If ii2 = 0 Then
ii2 = 1
End If
Next
p = Val(i) / (Val(ii) * Val(ii2))
For x = 1 To Val(z)
u = Val(power.Text) - x
d = x
Next
For cv = 1 To (z + 1)
ai = (Val(power.Text) - (cv - 1)) * Val(apower.Text)
bi = (cv - 1) * Val(bpower.Text)
If Val(ai) = 1 Then
aih = ""
Else
aih = "^" + Str(ai)
End If
If Val(bi) = 1 Then
bih = ""
Else
bih = "^" + Str(bi)
End If
If Val(ai) = 0 Then
a = ""
Else
a = Ae.Text + aih
End If
If Val(bi) = 0 Then
b = ""
Else
b = Be.Text + bih
End If
abd = Val(aend.Text) ^ ai * Val(bend.Text) ^ bi
Next
containa = a
containb = b
If abd Mod p = 0 Then
abn = 1
abdf = abd / p
Else
For r = p To 1 Step -1
If (abd Mod r) + (p Mod r) = 0 Then
abn = p / r
abdf = abd / r
r = 1
End If
Next
End If
abn = abn * Val(Aen.Text) ^ bi
abn = abn * Val(Ben.Text) ^ bi
If abn < 0 Then
Else
End If
If z = 0 Then
TextBox6.Text = TextBox6.Text + Str(abn) + "/" + Str(abdf) + a + b
Else
TextBox6.Text = TextBox6.Text + add + Str(abn) + "/" + Str(abdf) + a + b
End If
If contains.Checked = False Then
If Val(nthterm.Text) > 0 Then
If Val(nthterm.Text) = (z + 1) Then
term.Text = Str(abn) + "/" + Str(abdf) + a + b
End If
End If
If Val(pt.Text) > 0 Then
If z = Val(pt.Text) Then
z = (p + 1)
End If
End If
nth = 0
Else
If nth = 1 Then
contain = containa
containa = rgxExp.Replace(contain, "")
If containa = nthterm.Text Then
term.Text = Str(abn) + "/" + Str(abdf) + a + b
nth = 0
nthterm.Text = (z + 1)
contains.Checked = False
Else
contain = containb
containb = rgxExp.Replace(contain, "")
If containb = nthterm.Text Then
term.Text = Str(abn) + "/" + Str(abdf) + a + b
nth = 0
nthterm.Text = (z + 1)
contains.Checked = False
End If
End If
End If
End If
Next
tb = TextBox6.Text
TextBox6.Text = rgxExp.Replace(tb, "")
tm = term.Text
term.Text = rgxExp.Replace(tm, "")

Else

If aend.Text = Aen.Text Then
Aen.Text = 1
aend.Text = ""
End If
If bend.Text = Ben.Text Then
Ben.Text = 1
bend.Text = ""
End If
bds = ""
nth = 1
p = Val(power.Text)
vf(1) = Val(Aen.Text)
vf(2) = Val(Ben.Text)

For CV = 1 To (p - 1)
For cva = 0 To (CV + 1)
vf(0) = 0
va(cva + 1) = Val(Ben.Text) * vf(cva)
If Val(pt.Text) > 0 Then
If cva = Val(pt.Text) Then
cva = CV + 2
End If
End If
Next
For cvb = 1 To (CV + 2)
vf(CV + 2) = 0
vb(cvb) = Val(Aen.Text) * vf(cvb)
If Val(pt.Text) > 0 Then
If cvb = Val(pt.Text) Then
cvb = CV + 2
End If
End If
Next
For cvf = 1 To (CV + 2)
vf(cvf) = va(cvf) + vb(cvf)
f(cvf) = Str(vf(cvf))
If Val(pt.Text) > 0 Then
If cvf = Val(pt.Text) Then
cvf = CV + 2
End If
End If
Next
Next
For cv = 1 To Val(power.Text) + 1
ai = (Val(power.Text) - (cv - 1)) * Val(apower.Text)
bi = (cv - 1) * Val(bpower.Text)
If aend.Text <> "" Then
End If
If bend.Text <> "" Then
bd = Val(bend.Text) ^ bi
bds = "/" + Str(bd)
End If
a = ads + Ae.Text + "^" + Str(ai)
b = bds + Be.Text + "^" + Str(bi)
If Ae.Text = "" Then
a = ""
If f(cv) = 1 Then
a = f(cv)
End If
Else
If ai = 1 Then
a = Ae.Text
Else
If ai = 0 Then
a = ""
End If
End If
End If
If Be.Text = "" Then
b = ""
Else
If bi = 1 Then
b = Be.Text
Else
If bi = 0 Then
b = ""
End If
End If
End If
If vf(cv) > 0 Then
If cv > 1 Then
Else
End If
Else
End If
If Be.Text = "" Then
If cv = p + 1 Then
Else
End If
End If
an = f(cv)
bn = f(cv)
If aend.Text = "" Then
If an = 1 Then
If Be.Text = "" Then
If cv = 1 Then
an = ""
End If
Else
an = ""
End If
End If
End If
If cv = (Val(power.Text) + 1) Then
If Ae.Text = "" Then
a = ""
End If
End If
If bend.Text = "" Then
If bn = 1 Then
bn = ""
End If
End If
If cv = Val(power.Text) + 1 Then
If an <> "" Then
End If
End If
If Ben.Text = 1 Then
If Be.Text = "" Then

End If
End If
If cv = 1 Then
TextBox6.Text = TextBox6.Text + an + a + b
Else
TextBox6.Text = TextBox6.Text + add + an + a + b
End If
If aend.Text = "" And aend.Text = "" Then
Else
End If
containa = a
containb = b
If contains.Checked = False Then
If Val(nthterm.Text) > 0 Then
If Val(nthterm.Text) = cv Then
term.Text = an + a + b
End If
End If
If Val(pt.Text) > 0 Then
If cv = Val(pt.Text) Then
cv = (p + 1)
End If
End If
nth = 0
Else

If nth = 1 Then
contain = containa
containa = rgxExp.Replace(contain, "")
If containa = nthterm.Text Then
term.Text = f(cv) + a + b
nth = 0
nthterm.Text = cv
Else
contain = containb
containb = rgxExp.Replace(contain, "")
If containb = nthterm.Text Then
term.Text = f(cv) + a + b
nth = 0
nthterm.Text = cv
End If
End If
End If
End If
Next
If nth = 0 Then
contains.Checked = False
End If

tb = TextBox6.Text
TextBox6.Text = rgxExp.Replace(tb, "")
If Val(nthterm.Text) <= p + 1 Then
tm = term.Text
term.Text = rgxExp.Replace(tm, "")
Else
MsgBox("put in a nth term that is equal to or less then the power of the equation +1")
TextBox6.Text = ""
nthterm.Text = 0
End If
End If
End If
End Sub
```

### #4 June7

Reputation: 104
• Posts: 904
• Joined: 09-December 08

## Re: functional math solving 15! produces overflow error

Posted 30 May 2009 - 12:19 AM

What is this supposed to do?
```For x = 1 To Val(power.Text)
i = i * Val(x)
Next
```
Unless you have previously set the value of i to something, it starts here as 0 and this loop will produce nothing but 0. I didn't look at how you use this result in the following loops because right off this doesn't make sense. Have you step debugged?

Edit: was composing this post just as you posted the expanded code. Now I can see how i value is set. Still suggest step debugging.

This post has been edited by June7: 30 May 2009 - 12:28 AM

### #5 born2c0de

• printf("I'm a %XR",195936478);

Reputation: 187
• Posts: 4,673
• Joined: 26-November 04

## Re: functional math solving 15! produces overflow error

Posted 30 May 2009 - 01:17 AM

Here's some tips:
• To ensure that all nCn combinations work:
Before you calcutate the factorial, check if both paramaters contain the same value. If they do, return 1 or else continue finding the factorial.
• Increasing the computable range of numbers:
Remember that nCr = nC(n-r)
So if you're supposed to calculate 15C13, convert the expression to 15C2 (since 15 - 13 = 2) and calculate the factorials.
If you reduce your expressions, the loops will run faster and will also increase your range of computable numbers.

Hope this helps.

### #6 icomeinpieces

Reputation: 0
• Posts: 5
• Joined: 29-May 09

## Re: functional math solving 15! produces overflow error

Posted 30 May 2009 - 07:46 PM

born2c0de, on 30 May, 2009 - 12:17 AM, said:

Here's some tips:
• To ensure that all nCn combinations work:
Before you calcutate the factorial, check if both paramaters contain the same value. If they do, return 1 or else continue finding the factorial.
• Increasing the computable range of numbers:
Remember that nCr = nC(n-r)
So if you're supposed to calculate 15C13, convert the expression to 15C2 (since 15 - 13 = 2) and calculate the factorials.
If you reduce your expressions, the loops will run faster and will also increase your range of computable numbers.
Hope this helps.

hmmm never thought of that it has to compute the numbers down from 15n0 to 15n15 so if i stop it halfway roughly then make it work backwards it should be able to continue to work with numbers nearly double what it can handle right now.

but is there a better way to handle the overflow i mean a calculator program i have can handle much larger numbers that what was coming up in my step debugging before crashing

### #7 Lithium

Reputation: 0
• Posts: 15
• Joined: 28-May 09

## Re: functional math solving 15! produces overflow error

Posted 31 May 2009 - 06:40 AM

Works for me.

### #8 thava

• D.I.C Lover

Reputation: 180
• Posts: 1,607
• Joined: 17-April 07

## Re: functional math solving 15! produces overflow error

Posted 31 May 2009 - 06:39 PM

nearly 25 textboxes and 3 radio buttons it's quite hard to found what you want to do in that 25 textboxes where should i enter the input

### #9 icomeinpieces

Reputation: 0
• Posts: 5
• Joined: 29-May 09

## Re: functional math solving 15! produces overflow error

Posted 31 May 2009 - 08:53 PM

would text boxes be able to handle larger numbers then regular variables?

### #10 born2c0de

• printf("I'm a %XR",195936478);

Reputation: 187
• Posts: 4,673
• Joined: 26-November 04

## Re: functional math solving 15! produces overflow error

Posted 03 June 2009 - 10:59 AM

Textboxes stores data as strings and not numbers. Even if a textbox is set to accept only numeric data, it still stores it in a string.