I'm not dividing by zero and there is no float datatype in my code, I still get floating point exception.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
unsigned long long int t,n;
cin>>t;
while(t--)
{
cin>>n;
unsigned long long int deno = pow(10,n-1),count=2,sum = 0,f1=1,f2=1;
while(1){
sum = f1+f2;
f1 = f2;
f2 = sum;
count++;
if((int)(sum/deno)>0){
cout<<count<<endl;
break;
}
}
}
return 0;
}
All the previous questions on the same had the similar problem of dividing by Zero but variable deno can never be zero as n>=2
.
Previous research from my side:
- “Floating point exception” in code that contains no floats
- Floating Point Exception C++ Why and what is it?
Problem statement: https://www.hackerrank.com/contests/projecteuler/challenges/euler025/problem
It passes 2 test cases and fails 2. All are hidden test cases. Result image
On passing the input 1 50 we can reproduce the error. Details:
GDB trace: Reading symbols from solution...done. [New LWP 15127] Core
was generated by `solution'. Program terminated with signal SIGFPE,
Arithmetic exception.
#0 main () at solution.cc:23
23 if((int)(sum/deno)>0){
#0 main () at solution.cc:23
deno
and see if it ever is zero. – Punchboardstd::pow()
only works with floating point numbers. – ParacaseinArithmetic1
was thinking it'd handleint
– Ultramarine1 50
– Roseberryunsigned long long
can only go up about 19 digits while the problem requires 5000? Even along double
can only go up to 308 digits (with floating point error, of course). You're going to need a new approach. – Paracasein