Hackerrank - Algorithm - Fibonacci Modified

A series is defined in the following manner:

Given the nth and (n+1)th terms, the (n+2)th can be computed by the following relation T(n+2) = (Tn+1)^2 + T(n)
So, if the first two terms of the series are 0 and 1: the third term = 1^2 + 0 = 1 fourth term = 1^2 + 1 = 2 fifth term = 2^2 + 1 = 5 ... And so on.
Given three integers A, B and N, such that the first two terms of the series (1st and 2nd terms) are A and B respectively, compute the Nth term of the series.

Input Format
You are given three space separated integers A, B and N on one line.
Input Constraints 0 <= A,B <= 2 3 <= N <= 20

Output Format
One integer. This integer is the Nth term of the given series when the first two terms are A and B respectively.

Sample Input : 0 1 5
Output : 5


Solution
#!/bin/python
t1,t2,n = map(int,raw_input().split())
for i in range(1,n-1):
    ans = t1 + t2**2
    t1 = t2
    t2= ans
print(ans)         

Comments

Best Programming language for machine learning.

Popular posts from this blog

Hackerrank - Dynamic Programming - The Coin Change Problem

Hackerrank - Implementation - Picking Numbers