Find the number of jumps to reach X in the number line from zero | Utility function to calculate sum of numbers from 1 to x ; Function to find the number of jumps to reach X in the number line from zero ; First make number positive Answer will be same either it is Positive or negative ; To store required answer ; Continue till number is lesser or not in same parity ; Return the required answer ; Driver code
< ? php function getsum ( $ x ) { return ( $ x * ( $ x + 1 ) ) / 2 ; } function countJumps ( $ n ) { $ n = abs ( $ n ) ; $ ans = 0 ; while ( getsum ( $ ans ) < $ n or ( getsum ( $ ans ) - $ n ) & 1 ) $ ans ++ ; return $ ans ; } $ n = 9 ; echo countJumps ( $ n ) ; ? >