bash - How do I find the millionth number in the series: 2 3 4 6 9 13 19 28 42 63 ...? -


It takes minutes to get 3000 in my computer, but I need to know the millionth number in this series. Definition is recursive so I can not see any shortcut besides counting everything before a million numbers. How do you calculate the tenth number in the series?

Series DIF

n_ {i + 1} = \ floor {3/2 * n_ {i}} and n_ {0} = 2 .

Interestingly, only one site lists the series according to Google:

Very slow Bash code

 < Code> #! / Bin / bash function series {n = $ (echo "3/2 * $ n" | BC-L | TR '\ n' '' | SAD-A's @ \\ @@Gee-A's @ @@g '); # BC gives a large number, sed-tr n = $ (echo $ n / 1 | bc) #DUMMY forfour func} n = 2 nth = 1 while [true]; # $ Nth -lt500]; Series $ n # n gets new value in the function via global value $ nth $ n nth = $ (echo $ nth + 1 | bc) # n ++ done  

You can easily solve it by thinking about the problem in binary. Floor (3/2 * I) basically moves right, sort and combines. In pseudo code:

  0b_n [0] = 10 // start2 0b_n [1] = 10 + 1 (0) = 11 // move right, cut and add a bit 0 B_N [I + 1] = 0 B_N [ii] + Tilt (shiftrite (0 B_N [ii]))  

It should be very fast to implement in any form .

I have made this implementation in Mathematica and it seems that the bitShift Right operation also chops a bit before the position of the unit, so that it should be taken care of automatically. Here is a liner: [1]: = Time [number = nest [(bitShift Right [#] + #) & amp; 2, 999999];] [2] = {16.6022, tap in

  }  

16 seconds, the number prints properly, though it is quite long:

  in [2]: = integer length [num] out [2] = 176092 [3]: = num out [3] = 1963756 ... 123087  

Full results


Comments