Find the smallest value of N such that sum of first N natural numbers is â ‰¥ X | C ++ Program to implement the above approach ; Function to check if sum of first N natural numbers is >= X ; Finds minimum value of N such that sum of first N natural number >= X ; Check if sum of first i natural number >= X ; Driver Code ; Input ; Finds minimum value of N such that sum of first N natural number >= X
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isGreaterEqual ( int N , int X ) { return ( N * 1LL * ( N + 1 ) / 2 ) >= X ; } int minimumPossible ( int X ) { for ( int i = 1 ; i <= X ; i ++ ) { if ( isGreaterEqual ( i , X ) ) return i ; } } int main ( ) { int X = 14 ; cout << minimumPossible ( X ) ; return 0 ; }