You are viewing our Forum Archives. To view or take place in current topics click here.
Fibonacci number problem
Posted:
Fibonacci number problemPosted:
Status: Offline
Joined: Aug 28, 201311Year Member
Posts: 8
Reputation Power: 0
Status: Offline
Joined: Aug 28, 201311Year Member
Posts: 8
Reputation Power: 0
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, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
Post your source code in a code box for how you got the answer, here is mine.
Sub Main()
Dim A As Integer = 1
Dim B As Integer = A + A
Dim C As Integer = A + B
Dim D As Integer = B + C
Dim E As Integer = C + D
Dim F As Integer = D + E
Dim G As Integer = E + F
Dim H As Integer = F + G
Dim I As Integer = G + H
Dim J As Integer = H + I
Dim K As Integer = I + J
Dim L As Integer = J + K
Dim M As Integer = K + L
Dim N As Integer = L + M
Dim O As Integer = M + N
Dim P As Integer = N + O
Dim Q As Integer = O + P
Dim R As Integer = P + Q
Dim S As Integer = Q + R
Dim T As Integer = R + S
Dim U As Integer = S + T
Dim V As Integer = T + U
Dim W As Integer = U + V
Dim X As Integer = V + W
Dim Y As Integer = W + X
Dim Z As Integer = X + Y
Dim AA As Integer = Y + Z
Dim AB As Integer = Z + AA
Dim AC As Integer = AA + AB
Dim AD As Integer = AB + AC
Dim AE As Integer = AC + AD
Dim AF As Integer = AD + AE
Dim Answer As Integer = B + E + H + K + N + Q + T + W + Z + AC + AF
Console.WriteLine(A & "A")
Console.WriteLine(B & "B")
Console.WriteLine(C & "C")
Console.WriteLine(D & "D")
Console.WriteLine(E & "E")
Console.WriteLine(F & "F")
Console.WriteLine(G & "G")
Console.WriteLine(H & "H")
Console.WriteLine(I & "I")
Console.WriteLine(J & "J")
Console.WriteLine(K & "K")
Console.WriteLine(L & "L")
Console.WriteLine(M & "M")
Console.WriteLine(N & "N")
Console.WriteLine(O & "O")
Console.WriteLine(P & "P")
Console.WriteLine(Q & "Q")
Console.WriteLine(R & "R")
Console.WriteLine(S & "S")
Console.WriteLine(T & "T")
Console.WriteLine(U & "U")
Console.WriteLine(V & "V")
Console.WriteLine(W & "W")
Console.WriteLine(X & "X")
Console.WriteLine(Y & "Y")
Console.WriteLine(Z & "Z")
Console.WriteLine(AA & "AA")
Console.WriteLine(AB & "AB")
Console.WriteLine(AC & "AC")
Console.WriteLine(AD & "AD")
Console.WriteLine(AE & "AE")
Console.WriteLine(AF & "AF")
Console.WriteLine("the answer is " & Answer)
Console.ReadLine()
End Sub
[code]
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
Post your source code in a code box for how you got the answer, here is mine.
Sub Main()
Dim A As Integer = 1
Dim B As Integer = A + A
Dim C As Integer = A + B
Dim D As Integer = B + C
Dim E As Integer = C + D
Dim F As Integer = D + E
Dim G As Integer = E + F
Dim H As Integer = F + G
Dim I As Integer = G + H
Dim J As Integer = H + I
Dim K As Integer = I + J
Dim L As Integer = J + K
Dim M As Integer = K + L
Dim N As Integer = L + M
Dim O As Integer = M + N
Dim P As Integer = N + O
Dim Q As Integer = O + P
Dim R As Integer = P + Q
Dim S As Integer = Q + R
Dim T As Integer = R + S
Dim U As Integer = S + T
Dim V As Integer = T + U
Dim W As Integer = U + V
Dim X As Integer = V + W
Dim Y As Integer = W + X
Dim Z As Integer = X + Y
Dim AA As Integer = Y + Z
Dim AB As Integer = Z + AA
Dim AC As Integer = AA + AB
Dim AD As Integer = AB + AC
Dim AE As Integer = AC + AD
Dim AF As Integer = AD + AE
Dim Answer As Integer = B + E + H + K + N + Q + T + W + Z + AC + AF
Console.WriteLine(A & "A")
Console.WriteLine(B & "B")
Console.WriteLine(C & "C")
Console.WriteLine(D & "D")
Console.WriteLine(E & "E")
Console.WriteLine(F & "F")
Console.WriteLine(G & "G")
Console.WriteLine(H & "H")
Console.WriteLine(I & "I")
Console.WriteLine(J & "J")
Console.WriteLine(K & "K")
Console.WriteLine(L & "L")
Console.WriteLine(M & "M")
Console.WriteLine(N & "N")
Console.WriteLine(O & "O")
Console.WriteLine(P & "P")
Console.WriteLine(Q & "Q")
Console.WriteLine(R & "R")
Console.WriteLine(S & "S")
Console.WriteLine(T & "T")
Console.WriteLine(U & "U")
Console.WriteLine(V & "V")
Console.WriteLine(W & "W")
Console.WriteLine(X & "X")
Console.WriteLine(Y & "Y")
Console.WriteLine(Z & "Z")
Console.WriteLine(AA & "AA")
Console.WriteLine(AB & "AB")
Console.WriteLine(AC & "AC")
Console.WriteLine(AD & "AD")
Console.WriteLine(AE & "AE")
Console.WriteLine(AF & "AF")
Console.WriteLine("the answer is " & Answer)
Console.ReadLine()
End Sub
[code]
#2. Posted:
Status: Offline
Joined: Oct 10, 201113Year Member
Posts: 2,175
Reputation Power: 94
Status: Offline
Joined: Oct 10, 201113Year Member
Posts: 2,175
Reputation Power: 94
You don't need so many variables I mean I dont even get what you tried to do here.
All you need is to create the first two variables call them a and b. a is set to 0 and b is set to 1, this is to make incrementation easier.Or you can even use 1 main variable and one limit variable. Then you would use a loop to loop from 0 - 4000000.
Here is some pseudo code:
int a = 0;
int limit = 4000000;
for(int i = 0; i< limit; i = a + (a = i))
return a.
There are many ways of doing this, you got this from Euler as I did these before quite fun.
All you need is to create the first two variables call them a and b. a is set to 0 and b is set to 1, this is to make incrementation easier.Or you can even use 1 main variable and one limit variable. Then you would use a loop to loop from 0 - 4000000.
Here is some pseudo code:
int a = 0;
int limit = 4000000;
for(int i = 0; i< limit; i = a + (a = i))
return a.
There are many ways of doing this, you got this from Euler as I did these before quite fun.
- 0useful
- 0not useful
#3. Posted:
Status: Offline
Joined: Aug 28, 201311Year Member
Posts: 8
Reputation Power: 0
Status: Offline
Joined: Aug 28, 201311Year Member
Posts: 8
Reputation Power: 0
Tolerated wrote You don't need so many variables I mean I dont even get what you tried to do here.
All you need is to create the first two variables call them a and b. a is set to 0 and b is set to 1, this is to make incrementation easier.Or you can even use 1 main variable and one limit variable. Then you would use a loop to loop from 0 - 4000000.
Here is some pseudo code:
int a = 0;
int limit = 4000000;
for(int i = 0; i< limit; i = a + (a = i))
return a.
There are many ways of doing this, you got this from Euler as I did these before quite fun.
I did, I'm only on number 3 and stumped, for now, anyways I am trying to do as much as I can by only using visual basic, do you have a vb snippet.
- 0useful
- 0not useful
#4. Posted:
Status: Offline
Joined: Oct 10, 201113Year Member
Posts: 2,175
Reputation Power: 94
Status: Offline
Joined: Oct 10, 201113Year Member
Posts: 2,175
Reputation Power: 94
I don't remember the exact structure for vb but I imagine it could be something like so:
Dim a as Integer;
a = 0;
Dim limit as Integer;
limit = 4000000;
Dim counter as integer;
For counter = 0 to limit
MessageBox.Show(a);
Next counter = a + (a = counter)
Thats my best shot I have not done vb in a long time.
Dim a as Integer;
a = 0;
Dim limit as Integer;
limit = 4000000;
Dim counter as integer;
For counter = 0 to limit
MessageBox.Show(a);
Next counter = a + (a = counter)
Thats my best shot I have not done vb in a long time.
- 1useful
- 0not useful
#5. Posted:
Status: Offline
Joined: Aug 28, 201311Year Member
Posts: 8
Reputation Power: 0
Status: Offline
Joined: Aug 28, 201311Year Member
Posts: 8
Reputation Power: 0
Tolerated wrote I don't remember the exact structure for vb but I imagine it could be something like so:
Thats my best shot I have not done vb in a long time.
Thanks, I will try that out later
- 0useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.