Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
A formula is given to calculate F as
F=F(n-1)+F(n-2)+F(n-1)*F(n-2).
we have to find F for the values of n.
For small values . formula is working , but for large values , i will have to manipulate it.i m getting run time error in this code.

What I have tried:

C++
#include<bits/stdc++.h>
using namespace std;
int f(long long int F0, long long int F1,long long int n);

#define M 1000000007
int main()
{
    long long int F1,F0,F, T,N;

    cin >> T;
    while(T--)
    {
        cin>>F0>>F1>>N;

        F=f(F0,F1,N);
        cout << F%M << endl;
    }
    return 0;
}

int f(long long int F0, long long int F1,long long int n)
{
     if (n==0)
         return F0%M;
     else if(n==1)
         return F1%M;
     else
     {
         return (int)(((int)pow(F0,f(F0,F1,n-1))%M)
                    * ((int)pow(F1,f(F0,F1,n)))%M)%M;
     }
}
Posted
Updated 21-Jan-17 1:02am
v3
Comments
Patrice T 20-Jan-17 1:07am    
"run time error" is not informative.
What is the error message.
nv3 20-Jan-17 3:22am    
The name "Fibonacci" is misleading. Those are not Fibonacci numbers, which have no term "+ Fn-1 * Fn-2". Hence the number series even grow faster than Fibonacci's series! Meaning: N cannot be chose very large and you will arrive at the limits of number representation in a 32-bit int.
Member 12959092 21-Jan-17 4:22am    
ok, i have updated the code but it is not working for even small values. error code is not being shown . ideone is giving run time error only.
Richard MacCutchan 21-Jan-17 4:48am    
Please do not expect us to guess what is happening. Which line of code gives the error, on what variable values, and what is the exact text of the error message?
Richard MacCutchan 21-Jan-17 5:15am    
Writing a statement such as
return (int)(((int)pow(F0,f(F0,F1,n-1))%M) * ((int)pow(F1,f(F0,F1,n)))%M)%M;
is really asking for trouble.

First problem:
Quote:
A formula is given to calculate F as
F=F(n-1)+F(n-2)+F(n-1)*F(n-2).

This formula is not the one in your program and neither of them is Fibonacci.
So statement, program and Fibonacci are 3 different things, make your mind and choose 1 of them.

---------
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

Advice:
C++
return (int)(((int)pow(F0,f(F0,F1,n-1))%M)*((int)pow(F1,f(F0,F1,n)))%M)%M;

Split this line using temporary variables and 1 operation per line.
It will allow you to see what is going on with the debugger.

Question:
Quote:
i m getting run time error in this code.

Since this is not standard way of things. What compiler are you using? do you run this on your PC ? What exact message do you get?
 
Share this answer
 
v2
Comments
[no name] 21-Jan-17 7:53am    
I like this answer, a 5.
Patrice T 21-Jan-17 7:55am    
Thank you
Fibounachi numbers are getting really hugh, so somewhere is a border. I think around 40 its gets problematic with int size. Make some output for debugging purposes.

You should use the data type long long int in all situations. Your formulara to calculate looks wrong.

I know it as:

C++
 long long int fibonacci(long long int n) {
   if ( n == 0 ) return 0;
   else if ( n == 1 ) return 1;
   else
      return (fibonacci(n-1) + fibonacci(n-2));
} 
 
Share this answer
 
Okay, so you are trying to do a recursion via N. But then you should not call your function f with the same N as you got in, but only with N-1 and N-2! Calling f with the same N will cause an indefinitely deep nesting of calls to f and you will end up with a stack overflow!

Also, I can't see that the calls to pow should be good for. If you want to implement the function given in your description that line in function f should be:
C++
int fnm1, fnm2;
...
fnm1 = f(F0, F1, n-1);
fnm2 = f(F0, F1, n-2);
return fnm1 + fnm2 + fnm1*fnm2;

As you see, I have split the expression up into two sub-expressions in order to avoid multiple redundant calls to f.

And this whole business with %M is nonsense anyway. Just remove that.
 
Share this answer
 
Comments
Member 12959092 22-Jan-17 6:23am    
Thanks for help.
nv3 22-Jan-17 7:38am    
You are welcome. Don't forget to accept one or more solutions that have helped you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900