public class CompareFunctions { public static double f(double x){ return x*x*x - 2000*x*x; } public static double g(double x){ return 100*x*x + 20*x + 3500; } /** * @param args */ public static void main(String[] args) { int x = 1; int oldX = 0; //double the value we check at each step while(g(x) > f(x)){ oldX = x; x = x*2; } //do a binary search in the interval where the //crossing is found while(oldX <= x){ int mid = (x + oldX)/2; if(g(mid) < f(mid)){ x = mid - 1; } else { oldX = mid + 1; } } /* int y = 1; while(f(y) < g(y)){ y++; } System.out.printf("f(%d) = %f\n", y, f(y)); System.out.printf("g(%d) = %f\n", y, g(y)); */ System.out.printf("f(%d) = %f\n", x, f(x)); System.out.printf("g(%d) = %f\n", x, g(x)); System.out.printf("f(%d) = %f\n", oldX, f(oldX)); System.out.printf("g(%d) = %f\n", oldX, g(oldX)); } }