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

Posted by HH on Stack Overflow See other posts from Stack Overflow or by HH
Published on 2010-05-15T15:11:40Z Indexed on 2010/05/15 19:54 UTC
Read the original article Hit count: 163

Filed under:
|
|
|

It takes about minute to achieve 3000 in my comp but I need to know the millionth number in the series. The definition is recursive so I cannot see any shortcuts except to calculate everything before the millionth number. How can you fast calculate millionth number in the series?

Series Def

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

Interestingly, only one site list the series according to Google: this one.

Too slow Bash code

#!/bin/bash

function series 
{
        n=$( echo "3/2*$n" | bc -l | tr '\n' ' ' | sed -e 's@\\@@g' -e 's@ @@g' );
                                        # bc gives \ at very large numbers, sed-tr for it
        n=$( echo $n/1 | bc )           #DUMMY FLOOR func
}

n=2
nth=1

while [ true ]; #$nth -lt 500 ];
do
        series $n                        # n gets new value in the function through global value
        echo $nth $n
        nth=$( echo $nth + 1 | bc )     #n++
done

© Stack Overflow or respective owner

Related posts about bash

Related posts about recursion