I have a set of given integers:
A[] = { 2, 3, 4, 5, 6, 7, 8, 10, 15, 20, 25, 30, 40, 50, 100, 500 }
I want to check if a given integer can Tbe written as a multiple of the numbers in A[]; EDIT CLARIFICATION: any number in [] can be used . If used, can only be used once. EX 60 - valid T.60 = 30 * 2. AlSO 90 is valid. 90 = 3 * 5 * 6
Check which numbers can form this integer T.
- Also return the 2 closest integers to the specified one
T(which can be written this way) if the number Tcannot be written as a multiple of these numbers.
Parts 2 and 3, I think I can figure it out if someone helps me with part 1.
I know this is an algorithmic question, or even a mathematical one, but if someone can help, please.
DO NOT CONGRATULATE. CM. COMMENT BELOW.
#
DECISION. VERY VERY FOR ALL ANSWERS .1 answer especially (but the author decided to delete it, and I don’t know why, because it was right.) Ty author (do not remember your name.)
#
Solution code with a twist (the author’s algorithm used one factor several times. This one uses the multiplier only 1 time)
int oldT = 0;
HashMap<Integer, Boolean> used = new HashMap<Integer, Boolean>();
while (T != 1 && T != -1) {
oldT = T;
for (int multiple : A) {
if (!used.containsKey(multiple)) {
if (T % multiple == 0) {
T = T / multiple;
used.put(multiple, true);
}
}
}
if (oldT == T)
return false;
}
return true;