Padovan n-step number sequences: Difference between revisions

Content added Content deleted
Line 747: Line 747:


=={{header|Java}}==
=={{header|Java}}==
<syntaxhighlight>
<syntaxhighligh>


import java.util.ArrayList;
import java.util.List;

public final class PadovanNStep {

public static void main(String[] aArgs) {
final int limit = 8;
final int termCount = 15;
System.out.println("First " + termCount + " terms of the Padovan n-step number sequences:");
padovan(limit, termCount);
}
private static void padovan(int aLimit, int aTermCount) {
List<Integer> previous = List.of( 1, 1, 1 );
for ( int N = 2; N <= aLimit; N++ ) {
List<Integer> next = new ArrayList<Integer>(previous.subList(0, N + 1));
while ( next.size() < aTermCount ) {
int sum = 0;
for ( int stepBack = 2; stepBack <= N + 1; stepBack++ ) {
sum += next.get(next.size() - stepBack);
}
next.add(sum);
}
System.out.print(N + ": ");
next.forEach( term -> System.out.print(String.format("%4d", term)));
System.out.println();
previous = next;
}
}

}
</syntaxhighlight>
</syntaxhighlight>
{{ out }}
{{ out }}
<pre>
<pre>
First 15 terms of the Padovan n-step number sequences:

2: 1 1 1 2 2 3 4 5 7 9 12 16 21 28 37
3: 1 1 1 2 3 4 6 9 13 19 28 41 60 88 129
4: 1 1 1 2 3 5 7 11 17 26 40 61 94 144 221
5: 1 1 1 2 3 5 8 12 19 30 47 74 116 182 286
6: 1 1 1 2 3 5 8 13 20 32 51 81 129 205 326
7: 1 1 1 2 3 5 8 13 21 33 53 85 136 218 349
8: 1 1 1 2 3 5 8 13 21 34 54 87 140 225 362
</pre>
</pre>