Recaman sequenece
This commit is contained in:
64
recaman.cpp
Normal file
64
recaman.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Compile with GCC using "g++ --std=c++23 recaman.cpp -o program""
|
||||
*
|
||||
* SPDX-FileCopyrightText: Dora "cat" <cat@thenight.club>
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public License,
|
||||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
||||
* obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <print>
|
||||
#include <vector>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc != 2) {
|
||||
std::print(std::cerr, "You must exclusively specify 1 argument as n (MUST "
|
||||
"be uint64). Terminating.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint64_t goalN = 0, currentN = 0;
|
||||
try {
|
||||
goalN = std::stoull(argv[1]);
|
||||
} catch (std::exception e) {
|
||||
std::print(std::cerr, "Error: could not convert {} to uint64. Aborting.\n",
|
||||
argv[1]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::vector<uint64_t> values(goalN);
|
||||
int64_t expressionResult = 0;
|
||||
|
||||
// Idea is that each Recaman's number is reliant on the number below it, so we
|
||||
// can start from the bottom and generate to the top
|
||||
while (goalN > currentN) {
|
||||
if (currentN == 0) {
|
||||
++currentN;
|
||||
continue;
|
||||
}
|
||||
|
||||
expressionResult = values[currentN - 1] - currentN;
|
||||
if (expressionResult <= 0 || std::find(values.begin(), values.end(),
|
||||
expressionResult) != values.end()) {
|
||||
expressionResult += currentN * 2;
|
||||
}
|
||||
|
||||
values[currentN] = expressionResult;
|
||||
++currentN;
|
||||
}
|
||||
|
||||
// Once GCC fully implements C++23, I could do
|
||||
// std::print("result: {}\n", values);
|
||||
std::print("result:");
|
||||
for (uint64_t value : values) {
|
||||
std::print(" {} ", value);
|
||||
}
|
||||
std::print("\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user