tribonnaci number O(3^n) code example

Example 1: Tribonacci

const tribonacci = (signature,n) => {
    let orgArr = [...signature]
    let len = orgArr.length
    let arr = []
    if(n === 0){
        return []
    }else if(n < 3){
        orgArr = orgArr.slice(0, n)
    }else{
        for(let i = len; i < n; i++){
            if(i <= n){
                arr = orgArr.slice(-3)
                let nextValue = arr.reduce((acc, curr) => acc + curr, 0)
                orgArr.push(nextValue)
                arr = []
            }else{
                return 1
            }
        }
    }
    return orgArr
}

Example 2: iterative tribonacci python

def trib_iter(m):
    lista=[m]
    c=0
    d=True
    while d==True:
        novalista=[]
        for n in lista:
            if n in [0,1,2]:
                c+=1
            else:
                for i in range(1,4):
                    if n-i in [0,1,2]:
                        c+=1
                    else:
                        if n-i<0:
                            pass
                        else:
                            novalista.append(n-i)
        if novalista==[]:
            d=False
        else:
            lista=novalista
    return c

Tags:

Misc Example