I'm trying to write an algorithm for calculating numbers in a number sequence, starting with { 0, 1, 2 }, where additional numbers are calculated using the formula, n = (n - 1) + (n - 3), resulting in the sequence: { 0, 1, 2, 2, 3, 5, 7, 10, 15, 22, 32, 47, 69, 101 }, etc., using an iterative algorithm.
This is what I have tried:
public long calculate(long n) {
if (n <= 2) {
return n;
} else {
return (n - 1) + (n - 3);
}
}
However, when I'm testing it, it doesn't seem to be fully correct. Do you have any ideas on what I can improve?