ruby problem of bonacci series | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

ruby problem of bonacci series

code can be also found on this link https://code.sololearn.com/cva9U4w84M39/#rb #bonacci series 0 0 0 0 1 1 2 4 8... class Array_basic def bonacci(n,m) arr = Array.new y = n-1 #here is problem if y = n-2 given for i in 0..y #n... creates problem y = n-2 is also has problem arr[i] = 0 end arr << 1 #arr[3] = 1 #or use following #arr <<1 for j in 1...m-n arr[n+j] = 0 arr.values_at[n+j] #or it doest not work #arr[4..9] = 0 or arr[j] = 0 for k in 1...n # puts "-- #{arr[n+j]} --- #{arr[n+j-k]}" arr[n+j] = arr[n+j] + arr[n+j-k] end end puts "-- >#{arr}" end end obje = Array_basic.new puts "Input n and m. n must smaller then m" n = gets.to_i m= gets.to_i #bonacci series implemention call obje.bonacci(n,m) =begin problem: on line 4 if y = n-1 given it is fully functional. if y = n-2 given then it says following on Console--- `+': nil can't be coerced into Integer (TypeError) can anyone look that program where the actually problem is? =end

23rd Apr 2018, 3:16 PM
Sadik Sikder
Sadik Sikder - avatar
7 Answers
+ 6
You might want to save it as a code and give a link to it, just saying... ;) EDIT: Yup, that's more like it.
23rd Apr 2018, 3:38 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 2
The problem you have is that one of the values of your Array is an empty value (nil). For example, with the values n = 10 and m = 12 your array contains the following values: [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, nil, 0, 0] So when trying to execute the following line: arr [n + j] = arr [n + j] + arr [n + j - k] Ruby warns you that you can not add a number with a value of class Nil. At this point I can only speculate that the nil value is generated in this case thanks to the operations with the Index of the arrays. Some of it is out of range in relation to the elements contained in the array.
24th Apr 2018, 1:37 AM
Mickel
Mickel - avatar
+ 2
It's strange, because I tried running it out of the class and still giving error. Btw, that serie that you put is part of the N-Bonacci numbers, right? Although I recognize the series the way you try to generate it makes me confused
24th Apr 2018, 6:51 AM
Mickel
Mickel - avatar
+ 2
That code is easier to read. If you want to generate the series in base n - 2, you only have to make the range inclusive: In your code you currently have it as for i in 0 ... n - 1 (that means you are excluding n - 1). The exclusion generates an empty space when you start the iteration in the next cycle. One way to solve it could be to make the range inclusive: for i in 0..n - 2 Even so, I'm not entirely sure. I was able to generate all the series, including the range of your two cycles, but I still have some concerns. (It also influences what is currently here 3:00 A.M. and I'm half asleep, lol)
24th Apr 2018, 7:17 AM
Mickel
Mickel - avatar
+ 1
now link is given
23rd Apr 2018, 6:36 PM
Sadik Sikder
Sadik Sikder - avatar
0
thanks... but this program when runs on without class-method. then it has no such as problem as i above mentioned. but why within class menthod one index position becomes nil?
24th Apr 2018, 6:42 AM
Sadik Sikder
Sadik Sikder - avatar
0
its a N-bonacci series. try this link https://code.sololearn.com/cXlbCZ0ANW3Q/?ref=app
24th Apr 2018, 6:53 AM
Sadik Sikder
Sadik Sikder - avatar