|
-
Jan 27th, 2008, 01:35 PM
#1
Thread Starter
Addicted Member
[2008] Arithmetic operation resulted in an overflow
When the loop is
For x = 3 to 10
the codes work fine
when i change it to 100, the code gives an eroor
"Arithmetic operation resulted in an overflow."
on the line "If (Result + PreResult) Mod 2 = 0 Then y = y + Result + PreResult"
WHY????
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As Integer, Result As Long, PreResult As Long, oldresult As Long
Dim y As Long
Result = 2
PreResult = 1
y = 3
For x = 3 To 100
If (Result + PreResult) Mod 2 = 0 Then y = y + Result + PreResult
oldresult = PreResult
PreResult = Result
Result = Result + oldresult
Next
TextBox1.Text = y
End Sub
Im using Visual Studio 2008 Professional Edition
.Net Framework 3.5
-
Jan 27th, 2008, 01:49 PM
#2
Re: [2008] Arithmetic operation resulted in an overflow
 Originally Posted by perito
WHY????
Because 'Result' will hold 7540113804746346429, and 'PreResult' will hold 4660046610375530309. And you're trying to add them together. Thats a pretty big number.
Besides, the logic in that loop is pretty..odd, what are you trying to accomplish?
-
Jan 27th, 2008, 01:52 PM
#3
Re: [2008] Arithmetic operation resulted in an overflow
Put a breakpoint on the line Result = result + oldresult, then step through the code watching the size of result balloon. That math is not quite a binary expansion, but it is close, because it is similar to n += 2n, which, for any n>1 is a binary sequence, which means that on the 100th iteration, n holds 2^99, but a long integer can only hold about 2^63, so it overflows long before you reach the 100th iteration.
My usual boring signature: Nothing
 
-
Jan 27th, 2008, 01:52 PM
#4
Re: [2008] Arithmetic operation resulted in an overflow
My usual boring signature: Nothing
 
-
Jan 27th, 2008, 01:55 PM
#5
Thread Starter
Addicted Member
Re: [2008] Arithmetic operation resulted in an overflow
The question is this
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Find the sum of all the even-valued terms in the sequence which do not exceed one million.
i cant think of a better way to do this...
please give me new ideas
Im using Visual Studio 2008 Professional Edition
.Net Framework 3.5
-
Jan 27th, 2008, 02:00 PM
#6
Re: [2008] Arithmetic operation resulted in an overflow
But you don't need to go past 1,000,000, and you are currently going FAR beyond that.
Just add a condition into your loop to see if the value has gone over one million, and exit for if it has. Alternatively, rather than using a For Next loop, since you don't really know how many times you are going to loop, use a Do...Loop. Have x just be a counter in there that gets incremented each time through the loop, and loop until the value exceeds one million. That's a better solution than using the Exit For, though they will do about the same thing.
My usual boring signature: Nothing
 
-
Jan 27th, 2008, 02:09 PM
#7
Thread Starter
Addicted Member
Re: [2008] Arithmetic operation resulted in an overflow
hmmm....
i need to loop a million times right? because in the example there are 10 numbers, the sum of the even is 44
to do that, we loop 10 times, check if the number is even, add it.
I didnt understand what u mean...
Do Until y>= 1000000 will dont work...
please can u explain more
Im using Visual Studio 2008 Professional Edition
.Net Framework 3.5
-
Jan 27th, 2008, 02:24 PM
#8
Re: [2008] Arithmetic operation resulted in an overflow
I read your last post to suggest mean that you are looking for all numbers less than one million in the sequence, rather than looking for the first million numbers in the sequence.
Do While Y<= 1000000
would get you pretty close to the result, however it will end with the first value of y that is greater than 1000000, and it looks like you want the result just before that one. Therefore, it seems like a safer loop would be:
DO
Loop While (y + result + oldresult <= 1000000)
Since it appears that as soon as that becomes false, then the next value of y will be greater than 1,000,000, so the current value of y will be the largest one that is less than 1,000,000.
My usual boring signature: Nothing
 
-
Jan 27th, 2008, 02:31 PM
#9
Thread Starter
Addicted Member
Re: [2008] Arithmetic operation resulted in an overflow
the answer is still wrong :S
i even tried
Loop While (y + Result + oldresult <= 1000000)
y = y - oldresult - PreResult
and its still wrong.
Im wondering, the question might be different
Find the sum of all the even-valued terms in the sequence which do not exceed one million.
they might mean that the sequence doesnt exceed one million, not the sum. that will result in a different code.
ideas?
Im using Visual Studio 2008 Professional Edition
.Net Framework 3.5
-
Jan 27th, 2008, 03:17 PM
#10
Re: [2008] Arithmetic operation resulted in an overflow
Oh, I hadn't seen that. Now I get it.
In y, you are creating the sum of all the values. In Result you are getting the next item in the sequence. At the end of the loop:
Result: Holds the next item in the sequence (item N).
PreResult: Holds the last item in the sequence (item N-1).
OldResult: Holds the second to last item (item N-2).
So your sequence is correct, but what about your If statement?
Why are you checking Result + PreResult Mod 2? Result is the next item in the sequence, so shouldn't the if be:
If Result Mod 2 = 0 Then Y += Result
Then the loop would be:
Do
Loop while Result <= 1000000
My usual boring signature: Nothing
 
-
Jan 27th, 2008, 03:40 PM
#11
Thread Starter
Addicted Member
Re: [2008] Arithmetic operation resulted in an overflow
no the final result is (Result + PreResult)
make the loop until 10 and u will see that
Result + PreResult = 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Result alone is meaningless
i made the changes and checked anyway and the answer is still wrong...
Im using Visual Studio 2008 Professional Edition
.Net Framework 3.5
-
Jan 27th, 2008, 04:31 PM
#12
Re: [2008] Arithmetic operation resulted in an overflow
It works as it does because you are "looking ahead" at the next result, and adding it in if it belongs, whereas I was suggesting altering it around such that you are looking back. I see that I was out of phase a little, so I wrote it myself:
vb Code:
Dim Result As Long, PreResult As Long, oldresult As Long
Dim y As Long
Me.lb1.Items.Clear()
Me.lbAll.Items.Clear()
Result = 2
PreResult = 1
y = 3
Do
oldresult = PreResult
PreResult = Result
Result = Result + oldresult
Me.lbAll.Items.Add(Result)
If Result Mod 2 = 0 Then
y += Result
Me.lb1.Items.Add(Result)
End If
Loop While Result <= 1000000
lMess.Text = y.ToString
I put two listboxes on the form, one shows the even results, the other shows ALL the results. The sequence is correct.
The value of y is: 1,089,155
The highest result is: 832,040
My usual boring signature: Nothing
 
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
|
Click Here to Expand Forum to Full Width
|