2007 Self Divisor

<< 2008Checker | APQuestionsTrailIndex | 2007StudentAnswerSheet >>

Question from College Board

An integer is a "self-divisor" if every decimal digit is a divisor of the number.
For example, 128 is a self divisor since 1 divides 128, 2 divides 128, and 8 divides 128.
26 on the other hand is not a self-divisor. 2 does divide 26, but 6 does not divide 26.
Because you can't divide by zero, and number that has a zero in it like 1024, can never be a self-divisor

SelfDivisor

public class SelfDivisor
{
  /** @param number the number to be tested
   *         Precondition: number > 0
   *  @return true if every decimal digit of number is a divisor of number;
   *          false otherwise
   */
  public static boolean isSelfDivisor(int number)
  {
     //part a here

  }

  /** @param start starting point for values to be checked
   *         Precondition: start > 0
   *  @param num the size of the array to be returned
   *         Precondition: num > 0
   *  @return an array containing the first num integers >= start that are self-divisors
   */
  public static int[] firstNumSelfDivisors(int start, int num)
  {
    //part b here


  }

  /****************/

  public static void main (String[] args)
  {
    System.out.println("Is 128 a self divisor (should be true):  " + isSelfDivisor(128));
    System.out.println("Is 26 a self divisor (should be false): " + isSelfDivisor(26));
    System.out.println("Is 1024 a self divisor (should be false): " + isSelfDivisor(1024));
    System.out.println("Is 15 a self divisor (should be true):" + isSelfDivisor(15));
    System.out.println("The first 3 self divisors greater or equal to 10 (should be 11,12,15):");
    for (int n : firstNumSelfDivisors(10, 3))
      System.out.print(n + " ");
    System.out.println();
  }
}