Unix: millionth number in the serie 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 15:14 UTC
Read the original article Hit count: 288

Filed under:
|
|
|

It takes about minute to achieve 3000 in my comp but I need to know the millionth number in the serie. 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 serie?

Serie Def

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

Interestingly, only one site list the serie according to Goolge: this one.

Too slow Bash code

#!/bin/bash

function serie 
{
        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
        serie $n                        # n gets new value in the function throught global value
        echo $nth $n
        nth=$( echo $nth + 1 | bc )     #n++
done

© Stack Overflow or respective owner

Related posts about unix

Related posts about challenge