locked
Logic For Addtion RRS feed

  • Question

  • User1996548659 posted

     Dear All,

    I want code for following logic,

    1+2+3+5+8+13 =  32

    thnks

    Thursday, August 6, 2009 1:58 AM

Answers

  • User1006193418 posted

     Dear All,

    I want code for following logic,

    1+2+3+5+8+13 =  32

    thnks

    Hi,

    Please refer to this function that returns the sum of this sequence with the only input as the length of the sequence.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.Write(SumOfFibonacci(6))
    
    End Sub
    
    Protected Function SumOfFibonacci(ByVal n As Integer) As Integer
        If n <= 0 Then Throw New Exception("n must be a positive number")
        Dim f1 As Long = 1
        Dim f2 As Long = 2
        Dim temp As Long
    
        If n = 1 Then Return f1
    
        Dim sum As Long = 0
        For i As Integer = 1 To n
            sum = sum + f1
            temp = f2 : f2 = f2 + f1 : f1 = temp
        Next
        Return sum
    
    End Function

    Since this is the very famous Fibonacci Number, which you can refer to this link for more information: http://en.wikipedia.org/wiki/Fibonacci_number, thousands of functions doing the same task can be found from Google. You may have a try yourself with this search link: http://www.google.com/search?hl=en&newwindow=1&q=fibonacci+sum+code&aq=f&oq=&aqi.

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 7, 2009 6:11 AM

All replies

  • User384031199 posted

    Hi,

    Can you provide more details on your requirement? This looks like a simple addition.


    Thursday, August 6, 2009 4:52 AM
  • User1996548659 posted

     Hi

    I want code for this in vb.net.

    I just want code for that how to do ?

    thnks

    Thursday, August 6, 2009 5:11 AM
  • User1006193418 posted

     Dear All,

    I want code for following logic,

    1+2+3+5+8+13 =  32

    thnks

    Hi,

    Please refer to this function that returns the sum of this sequence with the only input as the length of the sequence.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.Write(SumOfFibonacci(6))
    
    End Sub
    
    Protected Function SumOfFibonacci(ByVal n As Integer) As Integer
        If n <= 0 Then Throw New Exception("n must be a positive number")
        Dim f1 As Long = 1
        Dim f2 As Long = 2
        Dim temp As Long
    
        If n = 1 Then Return f1
    
        Dim sum As Long = 0
        For i As Integer = 1 To n
            sum = sum + f1
            temp = f2 : f2 = f2 + f1 : f1 = temp
        Next
        Return sum
    
    End Function

    Since this is the very famous Fibonacci Number, which you can refer to this link for more information: http://en.wikipedia.org/wiki/Fibonacci_number, thousands of functions doing the same task can be found from Google. You may have a try yourself with this search link: http://www.google.com/search?hl=en&newwindow=1&q=fibonacci+sum+code&aq=f&oq=&aqi.

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 7, 2009 6:11 AM
  • User1918990383 posted

    Refer this for more info on fibonacci series :

    http://goldennumber.net/fibonser.htm

    This code will help you :

    Dim i As Integer = 0
    Dim j As Integer = 1
    Dim k, l As Integer

    Dim str As String
    str = i & "," & j
    For l = 0 To 10
    k = i + j
    i = j
    j = k
    str = str & "," & k
    Next
    MsgBox(str)

    If u want to add u can add to array.

    Friday, August 7, 2009 6:31 AM