Fibonacci Modified


Problem Statement :


Implement a modified Fibonacci sequence using the following definition:

Given terms t[i] and t[i+1] where i ∈ (1,∞), term t[i+2] is computed as:
t(i+2) = ti + t(i+2)^2

Given three integers, t1, t2, and n, compute and print the nth term of a modified Fibonacci sequence.

Example
t1 = 0
t2 =0
n =6

  t3 = 0+1^2 = 1
  t4 = 1+1^2 = 2
  t5 = 1+2^2 = 5
  t6 = 2+5^2 = 27
Return 27.

Function Description

Complete the fibonacciModified function in the editor below. It must return the nth number in the sequence.

fibonacciModified has the following parameter(s):

int t1: an integer
int t2: an integer
int n: the iteration to report

Returns

int: the nth number in the sequence
Note: The value of t[n] may far exceed the range of a 64-bit integer. Many submission languages have libraries that can handle such large results but, for those that don't (e.g., C++), you will need to compensate for the size of the result.

Input Format

A single line of three space-separated integers, the values of t1, t2, and n.

Constraints

0 <= t1, t2 <= 2
3 <= n <= 20
tn may far exceed the range of a 64-bit integer.



Solution :



title-img


                            Solution in C :

In C++ :






#include <iostream>
#include <utility>

#include <boost/multiprecision/cpp_int.hpp>

using boost::multiprecision::cpp_int;

cpp_int fib(cpp_int a, cpp_int b, unsigned int n)
{
    for(unsigned int i = 2; i < n; ++i)
    {
        cpp_int temp = a + b*b;
        a = b;
        b = temp;
    }
    return b;
}

int main()
{
    unsigned int a, b, n;

    std::cin >> a >> b >> n;
    std::cout << fib(a, b, n);
}








In Java :






import java.math.BigInteger;
import java.util.Scanner;


public class Solution {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		BigInteger a = BigInteger.valueOf(in.nextInt());
		BigInteger b = BigInteger.valueOf(in.nextInt());
		int n = in.nextInt();
		for(int i = 2; i < n; i++) {
			BigInteger next = b.multiply(b).add(a);
			a = b;
			b = next;
		}
		System.out.println(b);
	}
}








In C :






#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

#define MAXL    26624

unsigned int MADD(unsigned int* pC, unsigned int* pB, unsigned int* pA, unsigned int n)
{
    unsigned int i,j,x;
    
    for (i=0; i<n; i++) pC[i] = pA[i];
    for (i=0; i<n; i++)
    {
        for (j=0; j<n; j++)
        {
            if ((x = (pC[i+j] += pB[i]*pB[j])) < 10000) continue;
            x /= 10000; pC[i+j+1] += x; pC[i+j] -= x*10000;            
        }
        if ((x = pC[i+j]) < 10000) continue;
        x /= 10000; pC[i+j+1] += x; pC[i+j] -= x*10000;            
    }
    n <<= 1; while (pC[n-1] == 0) n--;
    return n;
}

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    unsigned int N,m,n=1;
    unsigned int* p;
    unsigned int* pA;
    unsigned int* pB;
    unsigned int* pC;
    unsigned int A[MAXL];
    unsigned int B[MAXL];
    unsigned int C[MAXL];

    memset(pA=A, 0, sizeof(A));
    memset(pB=B, 0, sizeof(B));
    memset(pC=C, 0, sizeof(C));

    scanf("%d %d %d\n", pA, pB, &N);

    while (N-- > 2)
    {
        n = MADD(pC, pB, pA, n);
        p = pC; pC=pA; pA = pB; pB=p;
    }
    printf("%d", p[--n]);
    while (n > 0) printf("%04u", p[--n]);
    printf("\n");
    return 0;
}








In Python3 :






a, b, n = [int(x) for x in input().split(" ")]

if n == 0:
    print(a)
if n == 1:
    print(b)

for _ in range(n-2):
    a, b = b, b*b + a
    
print(b)
                        








View More Similar Problems

Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

View Solution →

Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):

View Solution →

Down to Zero II

You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.

View Solution →

Truck Tour

Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr

View Solution →

Queries with Fixed Length

Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon

View Solution →

QHEAP1

This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element

View Solution →