2007 FRQ No 1
/* * @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) {
int copynum = number;
while (number > 0) {
int digit = number % 10; // get last digit by getting remainder of division by 10 (int will eliminate decimal point)
if (digit ==0 || copynum % digit != 0) { // if remainder of number and digit isnt 0, not divisble
return false; // cant have 0, every digit must be divisible
}
number /= 10; // divide number by 10 for next iteration in order to get the next digit
}
return true;
}
// Testing method with different inputs
System.out.println("Is 105 a self divisior? " + isSelfDivisor(105)); // Testing number with 0 in it
System.out.println("Is 143 a self divisior? " + isSelfDivisor(143)); // Testing non self divisor
System.out.println("Is 128 a self divisior? " + isSelfDivisor(128)); // Testing given example of self divisor: 128
/* * @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) {
int[] sds = new int[num]; // starting empty list with lenght equal to num
int total = 0; // keeps track of self divisors found
while (total < num) {
if (isSelfDivisor(start)) {
sds[total] = start; // adds selected number
total ++; // incremements total added
}
start ++; // moves on to next number
}
return sds;
}
System.out.println("First 5 Self Divisors from 13: ");
for (int n : firstNumSelfDivisors(13, 5))
{
System.out.print(n + ", ");
}
// FULL ANSWER
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) {
int copynum = number;
while (number > 0) {
int digit = number % 10; // get last digit by getting remainder of division by 10 (int will eliminate decimal point)
if (digit ==0 || copynum % digit != 0) { // if remainder of number and digit isnt 0, not divisble
return false; // cant have 0, every digit must be divisible
}
number /= 10; // divide number by 10 for next iteration in order to get the next digit
}
return true;
}
/* * @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) {
int[] sds = new int[num]; // starting empty list with lenght equal to num
int total = 0; // keeps track of self divisors found
while (total < num) {
if (isSelfDivisor(start)) {
sds[total] = start; // adds selected number
total ++; // incremements total added
}
start ++; // moves on to next number
}
return sds;
}
public static void main(String[] args) {
System.out.println("Is 135 a self divisor? " + isSelfDivisor(135));
System.out.println("Is 31 a self divisor? " + isSelfDivisor(31));
// print the array
System.out.println("5 self divisors after 33: ");
for (int n : firstNumSelfDivisors(33, 5))
{
System.out.print(n + ", ");
}
}
}
selfDivisor.main(null)