I’ve been having a little fun while I’ve been keeping my programming and C# skills sharp. Here is one of the problems I solved a little while ago. The biggest stinker about this problem was that the numbers could get very large. Larger than the Int64 would handle, so I had to install the BigInteger class to manage the really big numbers. After that, the problem is pretty straight forward.
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
Tn+2 = (Tn+1)2 + Tn
So, if the first two terms of the series are 0 and 1:
the third term = 12 + 0 = 1
fourth term = 12 + 1 = 2
fifth term = 22 + 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 Brespectively, 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.
Note
- Some output may even exceed the range of 64 bit integer.
Sample Input
0 1 5
Sample Output
5
Explanation
The first two terms of the series are 0 and 1. The fifth term is 5. How we arrive at the fifth term, is explained step by step in the introductory sections.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;
namespace FibonacciModified
{
class Program
{
// Input Constraints
// 0 <= A,B <= 2
// 3 <= N <= 20
static void Main(string[] args)
{
string[] tokens_ABN = Console.ReadLine().Split(' ');
int A, B, N;
BigInteger biA, biB, biN;
A = Convert.ToInt32(tokens_ABN[0]);
B = Convert.ToInt32(tokens_ABN[1]);
N = Convert.ToInt32(tokens_ABN[2]);
biA = A;
biB = B;
biN = 0;
for (int i = 2; i < N; i++)
{
biN = (biA + (biB * biB));
biA = biB;
biB = biN;
}
Console.WriteLine(biN);
}
}
}

Comments
One response to “Fibonacci Modified”
Keep working ,splendid job!