First term from given Nth term of the equation F ( N ) = ( 2 * F ( N | Python3 program to implement the above approach ; Function to find the value of power ( X , N ) % M ; Stores the value of ( X ^ N ) % M ; Calculate the value of power ( x , N ) % M ; If N is odd ; Update res ; Update x ; Update N ; Function to find modulo multiplicative inverse of X under modulo M ; Function to find the value of F ( 1 ) ; Stores power ( 2 , N - 1 ) ; Stores modulo multiplicative inverse of P_2 under modulo M ; Stores the value of F ( 1 ) ; Update res ; Driver code
M = 1000000007 NEW_LINE def power ( x , N ) : NEW_LINE INDENT res = 1 NEW_LINE while ( N > 0 ) : NEW_LINE INDENT if ( N & 1 ) : NEW_LINE INDENT res = ( res * x ) % M NEW_LINE DEDENT x = ( x * x ) % M NEW_LINE N = N >> 1 NEW_LINE DEDENT return res NEW_LINE DEDENT def moduloInverse ( X ) : NEW_LINE INDENT return power ( X , M - 2 ) NEW_LINE DEDENT def F_1 ( N , F_N ) : NEW_LINE INDENT P_2 = power ( 2 , N - 1 ) NEW_LINE modInv = moduloInverse ( P_2 ) NEW_LINE res = 0 NEW_LINE res = ( ( modInv % M ) * ( F_N % M ) ) % M NEW_LINE return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE F_N = 6 NEW_LINE print ( F_1 ( N , F_N ) ) NEW_LINE DEDENT