Skip to content

Commit 91ee464

Browse files
delete matrix, using only shortcut instead
1 parent 685995d commit 91ee464

File tree

5 files changed

+84
-210
lines changed

5 files changed

+84
-210
lines changed

CMakePresets.json

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
{
2-
"version": 3,
3-
"configurePresets": [
4-
{
5-
"name": "clang-release-windows",
6-
"displayName": "Clang 20 + vcpkg (Release)",
7-
"generator": "Ninja",
8-
"binaryDir": "${sourceDir}/build",
9-
"cacheVariables": {
10-
"CMAKE_BUILD_TYPE": "Release",
11-
"CMAKE_C_COMPILER": "C:/Program Files/LLVM/bin/clang.exe",
12-
"CMAKE_CXX_COMPILER": "C:/Program Files/LLVM/bin/clang++.exe",
13-
"CMAKE_TOOLCHAIN_FILE": "C:/vcpkg/scripts/buildsystems/vcpkg.cmake"
14-
}
15-
},
16-
{
17-
"name": "clang-release-linux",
18-
"displayName": "Clang 20 + linux (Release)",
19-
"generator": "Unix Makefiles",
20-
"binaryDir": "${sourceDir}/build",
21-
"cacheVariables": {
22-
"CMAKE_BUILD_TYPE": "Release",
23-
"CMAKE_C_COMPILER": "/bin/clang",
24-
"CMAKE_CXX_COMPILER": "/bin/clang++"
25-
}
26-
}
27-
]
2+
"version": 3,
3+
"configurePresets": [
4+
{
5+
"name": "clang-release-windows",
6+
"displayName": "Clang 20 + vcpkg (Release)",
7+
"generator": "Ninja",
8+
"binaryDir": "${sourceDir}/build",
9+
"cacheVariables": {
10+
"CMAKE_BUILD_TYPE": "Release",
11+
"CMAKE_C_COMPILER": "C:/Program Files/LLVM/bin/clang.exe",
12+
"CMAKE_CXX_COMPILER": "C:/Program Files/LLVM/bin/clang++.exe",
13+
"CMAKE_TOOLCHAIN_FILE": "C:/vcpkg/scripts/buildsystems/vcpkg.cmake"
14+
}
15+
},
16+
{
17+
"name": "clang-release-linux",
18+
"displayName": "Clang 20 + linux (Release)",
19+
"generator": "Unix Makefiles",
20+
"binaryDir": "${sourceDir}/build",
21+
"cacheVariables": {
22+
"CMAKE_BUILD_TYPE": "Release",
23+
"CMAKE_C_COMPILER": "/bin/clang",
24+
"CMAKE_CXX_COMPILER": "/bin/clang++"
25+
}
26+
}
27+
]
2828
}

collatz/include/args_handle.hxx

Lines changed: 0 additions & 29 deletions
This file was deleted.

collatz/include/collatz_matrix.hxx

Lines changed: 0 additions & 124 deletions
This file was deleted.

collatz/include/collatz_path.hxx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <concepts>
2+
#include <vector>
3+
4+
template <std::integral I>
5+
I int_pow(I base, I exp) {
6+
if (exp < 0) return 0;
7+
if (!exp) return 1;
8+
if (exp & 1) return base * int_pow(base, exp - 1);
9+
return int_pow(base * base, exp >> 1);
10+
}
11+
12+
template <std::integral I>
13+
std::vector<I> collatz_get_path(I seed) {
14+
using namespace std;
15+
vector<I> result;
16+
while (true) {
17+
while (!(seed & 1)) seed >>= 1;
18+
result.push_back(seed);
19+
if (seed == 1) return result;
20+
++seed;
21+
I v2 = 0;
22+
while (!(seed & 1)) {
23+
seed >>= 1;
24+
++v2;
25+
}
26+
seed = seed * int_pow<I>(3, v2) - 1;
27+
}
28+
return result;
29+
}

collatz/src/collatz.cpp

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,36 @@
1-
#include <args_handle.hxx>
2-
#include <collatz_matrix.hxx>
3-
#include <exception>
4-
#include <iomanip>
1+
#include <collatz_path.hxx>
52
#include <iostream>
6-
#include <stdexcept>
73
#include <string>
8-
#include <unordered_map>
94

10-
int main(int argc, const char **argv) {
5+
void print_help() {
116
using namespace std;
12-
unordered_map<string, string> args;
13-
args = get_args(argc, argv);
14-
uint64_t highestOddStarter = 0, longestPath = 0, seed = 0;
15-
if (args.empty() || args.find("help") != args.end() || args.find("h") != args.end()) return 0;
16-
17-
try {
18-
highestOddStarter = stoull(args.at("highestOddStarter"));
19-
seed = stoull(args.at("seed"));
20-
longestPath = stoull(args.at("longestPath"));
21-
} catch (const out_of_range &ofer) { cout << ofer.what() << endl; }
22-
23-
try {
24-
CollatzMatrix colMat(highestOddStarter, longestPath);
25-
vector<Vec4<uint64_t>> path = std::move(colMat.find_path(seed));
26-
27-
if (args.find("debug") != args.end() && args.at("debug") == "true") colMat.printMatrices();
28-
29-
for (auto val : path)
30-
cout << setw(10) << "Matrix index = " << val.w << setw(10) << "row = " << val.x << setw(10) << "col = " << val.y << setw(10) << "value = " << val.z
31-
<< endl;
32-
cout << endl << "Real Path on the original collatz function =";
33-
for (auto val : path) cout << 2 * val.z - 1 << " ";
34-
cout << endl;
35-
} catch (const exception &e) { cout << e.what() << endl; }
7+
cout << "-s or --seed to set the seed" << endl;
8+
cout << "-e or --exp to activate expMode" << endl << "\twhen the next argument passed as integer, it will be setted as 2^seed*odd -1";
9+
}
3610

11+
int main(int argc, const char **argv) {
12+
using namespace std;
13+
if (argc < 3) print_help();
14+
uint64_t seed = 0, odd = 1;
15+
bool expMode = false;
16+
for (int i = 1; i < argc; ++i) {
17+
if ((string(argv[i]) == "-s" || string(argv[i]) == "--seed") && argc > i + 1) seed = stoull(argv[i + 1]);
18+
if ((string(argv[i]) == "-e" || string(argv[i]) == "--exp")) {
19+
expMode = true;
20+
if (argc > i + 1) odd = stoull(string(argv[i + 1]));
21+
}
22+
}
23+
if (seed == 0) {
24+
print_help();
25+
return -1;
26+
}
27+
vector<uint64_t> result;
28+
if (expMode) {
29+
seed = int_pow<uint64_t>(2, seed) * odd - 1;
30+
result = collatz_get_path(seed);
31+
} else result = collatz_get_path(seed);
32+
cout << "path for " << seed << " =";
33+
for (auto val : result) cout << " " << val;
34+
cout << endl;
3735
return 0;
3836
}

0 commit comments

Comments
 (0)