
Jeslie C. answered 09/14/14
Tutor
3
(2)
Experienced Computer Science tutor - computers can be fun and easy!
Let's be smarter than raw math. Find the first multiple of 7 and then output every value that is 7 more than the last value, skipping the 5th such value. We just need to figure out where we are when we start. Here's the Java code, sans indentation, that will do this. Generalizing this should be simple, if that's what you really need.
/**
* A program to find all numbers that are divisible by 7 but are not a multiple of 5, between 2000 and 3200 (inclusive).
* The numbers obtained should be printed in a comma-separated sequence on a single line.
* <p>
* For our solution we're just going to find the first multiple to 7 and then output 4 of the next 4 values, skipping
* the fifth (a multiple of 7 and 5).
* For this to work we need to find the first multiple of 7 and then determine if it is the first, second, third, fourth
* or fifth multiple of 7 (i.e., how far we are in the sequence: the initial state).
*
* @author Jeslie Chermak
*/
public class NoCommonMultiple
{
// our two primes
private static final int P1 = 7;
private static final int P2 = 5;
private static final int COMMON_MULTIPLE = NoCommonMultiple.P1 * NoCommonMultiple.P2;
// adjust the bounds as required in the problem statement
private static final int LO = 2000;
private static final int HI = 3200;
// first value that is a multiple of {@value P1} >= LO
private static final int FROM_P1 = NoCommonMultiple.firstMultiple(NoCommonMultiple.LO, NoCommonMultiple.P1);
// first value that is a multiple of {@value COMMON_MULTIPLE} >= LO
private static final int FROM_COMMON_MULTIPLE
= NoCommonMultiple.firstMultiple(NoCommonMultiple.LO, NoCommonMultiple.COMMON_MULTIPLE);
// initial number of values to consider output
private static final int SKIPPING
= NoCommonMultiple.P2 - (NoCommonMultiple.FROM_COMMON_MULTIPLE - NoCommonMultiple.FROM_P1) / NoCommonMultiple.P1;
private static int firstMultiple(final int lowEnd, final int multiple) {
return lowEnd + (lowEnd % multiple == 0 ? 0 : multiple - (lowEnd % multiple));
}
public static void main(final String... args) {
boolean firstValue = true;
int multiple = NoCommonMultiple.FROM_P1;
for (int skip = NoCommonMultiple.SKIPPING; multiple <= NoCommonMultiple.HI; multiple += NoCommonMultiple.P1) {
if (skip++ == NoCommonMultiple.P2) {
skip = 1;
} else {
if (firstValue) {
firstValue = false;
} else {
System.out.print(',');
}
System.out.print(multiple);
}
}
System.out.println();
}
}
* A program to find all numbers that are divisible by 7 but are not a multiple of 5, between 2000 and 3200 (inclusive).
* The numbers obtained should be printed in a comma-separated sequence on a single line.
* <p>
* For our solution we're just going to find the first multiple to 7 and then output 4 of the next 4 values, skipping
* the fifth (a multiple of 7 and 5).
* For this to work we need to find the first multiple of 7 and then determine if it is the first, second, third, fourth
* or fifth multiple of 7 (i.e., how far we are in the sequence: the initial state).
*
* @author Jeslie Chermak
*/
public class NoCommonMultiple
{
// our two primes
private static final int P1 = 7;
private static final int P2 = 5;
private static final int COMMON_MULTIPLE = NoCommonMultiple.P1 * NoCommonMultiple.P2;
// adjust the bounds as required in the problem statement
private static final int LO = 2000;
private static final int HI = 3200;
// first value that is a multiple of {@value P1} >= LO
private static final int FROM_P1 = NoCommonMultiple.firstMultiple(NoCommonMultiple.LO, NoCommonMultiple.P1);
// first value that is a multiple of {@value COMMON_MULTIPLE} >= LO
private static final int FROM_COMMON_MULTIPLE
= NoCommonMultiple.firstMultiple(NoCommonMultiple.LO, NoCommonMultiple.COMMON_MULTIPLE);
// initial number of values to consider output
private static final int SKIPPING
= NoCommonMultiple.P2 - (NoCommonMultiple.FROM_COMMON_MULTIPLE - NoCommonMultiple.FROM_P1) / NoCommonMultiple.P1;
private static int firstMultiple(final int lowEnd, final int multiple) {
return lowEnd + (lowEnd % multiple == 0 ? 0 : multiple - (lowEnd % multiple));
}
public static void main(final String... args) {
boolean firstValue = true;
int multiple = NoCommonMultiple.FROM_P1;
for (int skip = NoCommonMultiple.SKIPPING; multiple <= NoCommonMultiple.HI; multiple += NoCommonMultiple.P1) {
if (skip++ == NoCommonMultiple.P2) {
skip = 1;
} else {
if (firstValue) {
firstValue = false;
} else {
System.out.print(',');
}
System.out.print(multiple);
}
}
System.out.println();
}
}