Find element position in given monotonic sequence | PHP implementation of the approach from math import log2 , floor ; Function to return the value of f ( n ) for given values of a , b , c , n ; if c is 0 , then value of n can be in order of 10 ^ 15. if c != 0 , then n ^ 3 value has to be in order of 10 ^ 18 so maximum value of n can be 10 ^ 6. ; for efficient searching , use binary search . ; Driver code
< ? php $ SMALL_N = 1000000 ; $ LARGE_N = 1000000000000000 ; function func ( $ a , $ b , $ c , $ n ) { $ res = $ a * $ n ; $ logVlaue = floor ( log ( $ n , 2 ) ) ; $ res += $ b * $ n * $ logVlaue ; $ res += $ c * ( $ n * $ n * $ n ) ; return $ res ; } function getPositionInSeries ( $ a , $ b , $ c , $ k ) { global $ SMALL_N , $ LARGE_N ; $ start = 1 ; $ end = $ SMALL_N ; if ( $ c == 0 ) $ end = $ LARGE_N ; $ ans = 0 ; while ( $ start <= $ end ) { $ mid = ( int ) ( ( $ start + $ end ) / 2 ) ; $ val = func ( $ a , $ b , $ c , $ mid ) ; if ( $ val == $ k ) { $ ans = $ mid ; break ; } else if ( $ val > $ k ) $ end = $ mid - 1 ; else $ start = $ mid + 1 ; } return $ ans ; } $ a = 2 ; $ b = 1 ; $ c = 1 ; $ k = 12168587437017 ; print ( getPositionInSeries ( $ a , $ b , $ c , $ k ) ) ; ? >