brewersfan1976

fibo_sum.rb

May 27th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.67 KB | None | 0 0
  1. def fibonacciSum(n)
  2.     fibo = Array.new
  3.     x = 2
  4.     sum = 0
  5.    
  6.     fibo[0] = 0
  7.     fibo[1] = 1
  8.    
  9.     while (sum <= n)
  10.           sum = fibo[x - 1] + fibo[x - 2]
  11.           fibo[x] = sum
  12.           x = x + 1
  13.     end
  14.    
  15.     array_length = fibo.length
  16.     x = array_length - 1
  17.     sum = 0
  18.     fibo_sum = Array.new
  19.    
  20.     while (x >= 0)
  21.         if fibo[x] <= n and fibo[x] + sum <= n then
  22.            sum = sum + fibo[x]
  23.            fibo_sum[x] = fibo[x]
  24.         end
  25.            
  26.         if sum == n then
  27.            break
  28.         end
  29.        
  30.         x = x - 1
  31.     end
  32.        
  33.     fibo_sum = fibo_sum - ["", nil]
  34.            
  35.     return fibo_sum
  36. end
Advertisement
Add Comment
Please, Sign In to add comment