Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Fibonacci sequence in STATA

    Hello

    I am trying to write a loop in STATA which would sum up all even numbers of the Fibonacci sequence that are smaller or equal to 1000.

    My understanding is that even numbers in this sequence are given by 4 * evennumber2 + evennumber1
    So, for example, the 3rd even number in the sequence is given by 4*2+0=8
    the 4th is given by 4*8+2=34 and so on and so forth

    a very rough idea of what my loop would look like is

    generate ef1 = 0
    generate ef2 = 2
    generate sum = ef1 + ef2

    while ef2 <= 1000
    generate ef3 = 4 * ef2 + ef1

    if ef3 > 1000
    exit
    display ef3

    else
    replace ef1 = ef2
    replace ef2 = ef3

    I am unsure of how to write it with the correct syntax and unsure whether to use a forvalues of foreach loop. I ask for some direction.
    Thank you in advance

  • #2
    No loop is needed. The following code uses your formula for even values of the Fibonacci sequence.
    Code:
    set obs 10
    generate ef = .
    replace ef = 0 in 1
    replace ef = 2 in 2
    replace ef = 4*ef[_n-1] + ef[_n-2] in 3/L
    drop if ef>1000
    generate sef = sum(ef)
    Code:
    . list, clean 
    
            ef   sef  
      1.     0     0  
      2.     2     2  
      3.     8    10  
      4.    34    44  
      5.   144   188  
      6.   610   798
    The following code starts with the definition of the Fibonacci sequence and discards the odd values.
    Code:
    set obs 20
    generate ef = .
    replace ef = 0 in 1
    replace ef = 1 in 2
    replace ef = ef[_n-1] + ef[_n-2] in 3/L
    drop if mod(ef,2)!=0
    drop if ef>1000
    generate sef = sum(ef)
    The results are the same as the first example.

    Comment


    • #3
      Ah! this is already very helpful, thank you!
      But if I had to use a loop... would it be

      generate ef = .
      replace ef = 0 in 1
      replace ef = 2 in 2

      forvalues 1/10 {

      replace ef = 4*ef[_n-1] + ef[_n-2] in 3/L

      generate sef = sum(ef)
      if sef >= 1000
      exit
      display sef
      if else....

      ?? I'm not sure...

      Comment


      • #4
        Perhaps
        Code:
        set obs 10
        generate ef = .
        replace ef = 0 in 1
        replace ef = 2 in 2
        forvalues i = 3/10 {
            local eftemp = 4*ef[`i'-1] + ef[`i'-2]
            if `eftemp'>1000 continue, break
            replace ef = `eftemp' in `i'
            }
        drop if ef==.
        generate sef = sum(ef)
        or
        Code:
        set obs 10
        generate ef = .
        replace ef = 0 in 1
        replace ef = 2 in 2
        forvalues i = 3/10 {
            replace ef = 4*ef[`i'-1] + ef[`i'-2] in `i'
            if ef[`i']>1000 continue, break
            }
        drop if ef>1000
        generate sef = sum(ef)
        or
        Code:
        set obs 10
        generate ef = .
        replace ef = 0 in 1
        replace ef = 2 in 2
        local i 2
        while ef[`i']<1000 {
            local i = `i'+1
            replace ef = 4*ef[`i'-1] + ef[`i'-2] in `i'
            }
        drop if ef>1000
        generate sef = sum(ef)
        Last edited by William Lisowski; 13 Aug 2022, 07:15.

        Comment


        • #5
          If we are just using Stata as a calculator, why even disturb the dataset in memory? Consider:

          Code:
          local max 1000
          local eF1 = 0
          local eF2 = 2
          local n = 2
          local sum = 0
          
          while `eF`n'' < `max' {
              local sum = `sum' + `eF`n''
              local ++n
              local eF`n' = 4*`eF`=`n'-1'' + `eF`=`n'-2''
          }
          
          dis "The sum of the even Fibonacci numbers below `max' is `sum'."

          Comment

          Working...
          X