Initial commit
This commit is contained in:
commit
612f25b5ca
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# Ignore emacs backups
|
||||||
|
*~
|
||||||
|
|
||||||
|
# Ignore build
|
||||||
|
collatz*
|
13
README.md
Normal file
13
README.md
Normal 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
24
main.cpp
Normal 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';
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user