You could try something like this:
IntStream.rangeClosed(1, 100).forEach(target -> {
final int max1 = (target + 1 - 1) / 1;
final int max2 = (target + 2 - 1) / 2;
final int max5 = (target + 5 - 1) / 5;
IntStream .rangeClosed( 1, max5).forEach(count5 -> {
IntStream .rangeClosed(count5 + 1, max2).forEach(count2 -> {
IntStream.rangeClosed(count2 + 1, max1).forEach(count1 -> {
final int sum = count5*5 + count2*2 + count1*1;
if (sum == target) {
System.out.println("Target.: " + target + " -> " + count5 + "*5$ " + count2 + "*2$ " + count1 + "*1$");
}
});
});
});
System.out.println();
});
...but, as others have pointed out, your constraints make a solution for some amounts impossible.
Here's a variation of this theme, giving a solution for all N...
IntStream.rangeClosed(1, 100).forEach(target -> {
final int max1 = (target + 1 - 1) / 1;
final int max2 = (target + 2 - 1) / 2;
final int max5 = (target + 5 - 1) / 5;
for (int count1= max1; count1 > 0; count1--) {
for (int count2=Math.min(max2, count1 - 1); count2 > 0; count2--) {
for (int count5=Math.min(max5, count2 - 1); count5 > 0; count5--) {
final int sum = count5*5 + count2*2 + count1*1;
if (sum == target) {
System.out.println("Target..............: " + target + " -> " + count5 + "*5$ " + count2 + "*2$ " + count1 + "*1$");
return; // Note.: solution found, exit from Consumer, NOT Method!
}
}
}
}
/*
* Fallback 1: at least one of each denomination...
*/
for (int count1=max1; count1 > 0; count1--) {
for (int count2=max2; count2 > 0; count2--) {
for (int count5=max5; count5 > 0; count5--) {
final int sum = count5*5 + count2*2 + count1*1;
if (sum == target) {
System.out.println("Target (fallback 1).: " + target + " -> " + count5 + "*5$ " + count2 + "*2$ " + count1 + "*1$");
return; // Note.: solution found, exit from Consumer, NOT Method!
}
}
}
}
/*
* Fallback 2: "anything goes"...
*/
for (int count1=max1; count1 >= 0; count1--) {
for (int count2=max2; count2 >= 0; count2--) {
for (int count5=max5; count5 >= 0; count5--) {
final int sum = count5*5 + count2*2 + count1*1;
if (sum == target) {
System.out.println("Target (fallback 2).: " + target + " -> " + count5 + "*5$ " + count2 + "*2$ " + count1 + "*1$");
return; // Note.: solution found, exit from Consumer, NOT Method!
}
}
}
}
System.out.println("Target..............: " + target + " NO Solution possible!");
});