import java.lang.Math.*; public class numProbes { /** * * Bret Curtis CPSC 321 * Number of Probes Java Program * * Simple Log2(N)+1 program where N is the array size. * **/ public static void main( String[] args) { if (args.length == 0) { throw new IllegalArgumentException("Please pass a number as an argument."); } else { int number = Integer.parseInt(args[0]); System.out.println("Array size of " + number + " can take up to " + manProbes(number) + " iterations "); System.out.println("to find an element using the Binary Search algorithm."); } } /** * Compute maximum number of probes necessary to determine * if an item is in an array where the binary search * algorithm is being used. * * @param n * an integer * @precondition * n > 0 and n < 1,000,000 * @postcondition * return the maximum number of probes as described above * @throws java.lang.IllegalArgumentException * indicates that n is less than or equal to 0 * @return * return the maximum number of probes as described above **/ public static int manProbes( int n ) { double number = 0; if ( ( n > 0 ) && ( n < 1000000 ) ) { number = Math.floor((Math.log(n)/Math.log(2))+1); return (int)number; } else { throw new IllegalArgumentException("Argument " + n + " is < 1 or > 1,000,000, please try again."); } } }