Initial commit

This commit is contained in:
ilikecats 2024-07-07 17:39:59 -07:00
commit 612f25b5ca
4 changed files with 44 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
# Ignore emacs backups
*~
# Ignore build
collatz*

2
Makefile Normal file
View File

@ -0,0 +1,2 @@
default:
g++ main.cpp -o collatz

13
README.md Normal file
View File

@ -0,0 +1,13 @@
# Collatz conjecture solver
### (x%2==0) ? x/=2 : x=x*3+1;
Program to solver the (Collatz conjecture)[https://en.wikipedia.org/wiki/Collatz_conjecture].
Algorithm:
- Start with number.
- If number is even, divide by two.
- If number is odd, multiply by three and add one.
- Repeat.
To figure out if it will repeat, save all the previous numbers, and check if the current number is one of those.
If it is, that means it will repeat, and you have disproved the Collatz conjecture. So far, though, nobody has disproven it up to 2^68.

24
main.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <iostream>
#include <string>
// TODO:
// Use GMP for numbers (to allow for really big numbers)
// Make disproving mode
// Make no print mode (for speed, implement after disproving mode)
int main(int argc, char* argv[]) {
// If number not provided
if (argc < 2) {
std::cout << "Usage: " << argv[0] << " <number>\n";
exit(1);
}
long long x { std::stoll(argv[1]) };
std::cout << x << '\n';
while (x != 1) {
if (x % 2 == 0)
x /= 2;
else if (x % 2)
x = x*3+1;
std::cout << x << '\n';
}
}