#include <cmath>
#include <iostream>
const double pheta = 0.5 * ( std:: sqrt ( 5 ) + 1 ) ;
double fib( unsigned int n)
{
return ( std:: pow ( pheta, n) - std:: pow ( 1 - pheta, n) ) / std:: sqrt ( 5 ) ;
}
unsigned int fibo_lowerbound( double N, unsigned min= 0 , unsigned max= 1000 )
{
unsigned newpivot = ( min+ max) / 2 ;
if ( min== newpivot)
return newpivot;
if ( fib( newpivot) <= N)
return fibo_lowerbound( N, newpivot, max) ;
else
return fibo_lowerbound( N, min, newpivot) ;
}
std:: pair < double , double > fibo_range( unsigned int n)
{
unsigned int lbound = fibo_lowerbound( n) ;
return std:: make_pair ( fib( lbound) , fib( lbound+ 1 ) ) ;
}
void display( unsigned int n)
{
std:: pair < double , double > range = fibo_range( n) ;
std:: cout << "Fibonacci range wrapping " << n << " is "
<< "[" << ( unsigned long long ) range.first << ", " << ( unsigned long long ) range.second << "]"
<< std:: endl ;
}
int main( )
{
display( 1044 ) ;
display( 8999913 ) ;
display( 7 ) ;
display( 67 ) ;
}
I2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8aW9zdHJlYW0+Cgpjb25zdCBkb3VibGUgcGhldGEgPSAwLjUqKHN0ZDo6c3FydCg1KSsxKTsKCmRvdWJsZSBmaWIodW5zaWduZWQgaW50IG4pCnsKICAgIHJldHVybiAoc3RkOjpwb3cocGhldGEsIG4pIC0gc3RkOjpwb3coMSAtIHBoZXRhLCBuKSkgLyBzdGQ6OnNxcnQoNSk7Cn0KCnVuc2lnbmVkIGludCBmaWJvX2xvd2VyYm91bmQoZG91YmxlIE4sIHVuc2lnbmVkIG1pbj0wLCB1bnNpZ25lZCBtYXg9MTAwMCkKewogICAgdW5zaWduZWQgbmV3cGl2b3QgPSAobWluK21heCkvMjsKICAgIGlmIChtaW49PW5ld3Bpdm90KQogICAgICAgIHJldHVybiBuZXdwaXZvdDsKCiAgICBpZiAoZmliKG5ld3Bpdm90KSA8PSBOKQogICAgICAgIHJldHVybiBmaWJvX2xvd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