If I correctly understood the condition of the problem, then the sequence of the number of pairs is obtained as follows:
1, 1, 1, 2, 3, 4, 6, 9, 13, 19, 28, 41, 60
Where the first number is the number of pairs at the beginning of the first month, and then the number of pairs at the end of each month, up to and including the 12th.
If everything is so, and the answer is "120 rabbits" (60 pairs), then the Fibonacci sequence is not called. However, the linear recurrent sequence is still traced here. In particular:
F n = F n-1 + F n-3
Code to calculate:
final int monthsCount = 12; int[] counts = new int[monthsCount + 1]; counts[0] = 1; counts[1] = 1; counts[2] = 1; for (int i = 3; i <= monthsCount; i++) { counts[i] = counts[i - 1] + counts[i - 3]; } System.out.println(Arrays.toString(counts));
The following was used as a “head on” solution and to check the results:
private static class Pair { private static final int GET_MONTHS = 3; private int monthsCount = 0; public Pair inc() { monthsCount++; return (monthsCount >= GET_MONTHS) ? new Pair() : null; } } public static void main(String[] args) { ArrayList<Pair> pairs = new ArrayList<>(); pairs.add(new Pair()); for (int i = 0; i < monthsCount; i++) { List<Pair> newPairs = pairs.stream() .map(Pair::inc).filter(e -> e != null) .collect(Collectors.toList()); pairs.addAll(newPairs); } System.out.println(pairs.size()); }