public class RecSum { // Evaluate the sum of the first n integers public static long s( int n ) { if( n == 1 ) return 1; else return s( n - 1 ) + n; } // Simple test program public static void main( String [ ] args ) { for( int i = 1; i <= 10; i++ ) System.out.println( s( i ) ); // Find the largest n for which s(n) works int low = 0; int high = 100000; int mid = 0; long val; while( low < high ) { mid = ( low + high + 1 ) / 2; try { val = s( mid ); low = mid; } catch( Throwable e ) { high = mid - 1; } } System.out.println( "MAX n that works is: " + low ); } }