RosettaCodeData/Task/Padovan-n-step-number-sequences/C++/padovan-n-step-number-sequences.cpp

37 lines
986 B
C++
Raw Permalink Normal View History

2023-09-01 09:35:06 -07:00
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <vector>
void padovan(const int32_t& limit, const uint64_t& termCount) {
2026-04-30 12:34:36 -04:00
std::vector<int32_t> previous_terms = { 1, 1, 1 };
2023-09-01 09:35:06 -07:00
2026-04-30 12:34:36 -04:00
for ( int32_t N = 2; N <= limit; ++N ) {
std::vector<int32_t> next_terms = { previous_terms.begin(), previous_terms.begin() + N + 1 };
2023-09-01 09:35:06 -07:00
2026-04-30 12:34:36 -04:00
while ( next_terms.size() < termCount ) {
int32_t sum = 0;
for ( int32_t step_back = 2; step_back <= N + 1; ++step_back ) {
sum += next_terms[next_terms.size() - step_back];
}
next_terms.emplace_back(sum);
}
2023-09-01 09:35:06 -07:00
2026-04-30 12:34:36 -04:00
std::cout << N << ": ";
for ( const int32_t& term : next_terms ) {
std::cout << std::setw(4) << term;
}
std::cout << std::endl;;
2023-09-01 09:35:06 -07:00
2026-04-30 12:34:36 -04:00
previous_terms = next_terms;
}
2023-09-01 09:35:06 -07:00
}
int main() {
2026-04-30 12:34:36 -04:00
const int32_t limit = 8;
const uint64_t termCount = 15;
2023-09-01 09:35:06 -07:00
2026-04-30 12:34:36 -04:00
std::cout << "First " << termCount << " terms of the Padovan n-step number sequences:" << std::endl;
padovan(limit, termCount);
2023-09-01 09:35:06 -07:00
}