SUPPORT FOR GMP AND NONGMP IN THE SAME BRANCH!!!
This commit is contained in:
parent
dad227c0e1
commit
6d84e89353
9
Makefile
9
Makefile
@ -1,3 +1,10 @@
|
||||
default:
|
||||
g++ main.cpp -o collatz
|
||||
cp withgmp.hpp include.hpp
|
||||
g++ -Wall -Wextra -pedantic -Werror -lgmp -lgmpxx main.cpp -o collatz
|
||||
strip collatz
|
||||
rm include.hpp
|
||||
gmpless:
|
||||
cp nongmp.hpp include.hpp
|
||||
g++ -Wall -Wextra -pedantic -Werror main.cpp -o collatz
|
||||
strip collatz
|
||||
rm include.hpp
|
||||
|
@ -1,15 +1,14 @@
|
||||
# Collatz conjecture solver
|
||||
### (x%2==0) ? x/=2 : x=x*3+1;
|
||||
|
||||
### You probably want the GMP version for arbitrarily big numbers. Get it from the [GMP branch](https://git.sdf.org/ilikecats/collatz/src/branch/gmp).
|
||||
Program to solve the [Collatz conjecture](https://en.wikipedia.org/wiki/Collatz_conjecture).
|
||||
|
||||
Program to solver the [Collatz conjecture](https://en.wikipedia.org/wiki/Collatz_conjecture).
|
||||
To build:
|
||||
- With GMP: Run `make -j1` (any more than `-j1` doesn't really do anything, there's four things to do and they can't be put out of order).
|
||||
- Without GMP: Run `make gmpless -j1` (see above parenthetical statement).
|
||||
|
||||
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.
|
||||
|
29
main.cpp
29
main.cpp
@ -15,7 +15,7 @@
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "include.hpp" // For optional gmpless building (see Makefile)
|
||||
|
||||
// TODO:
|
||||
// Make disproving mode
|
||||
@ -27,13 +27,26 @@ int main(int argc, char* argv[]) {
|
||||
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;
|
||||
mpz_t x; // mpz_t is the GMP equiv. of int
|
||||
mpz_t xmod2; // Store x % 2
|
||||
// Init vars (if not disabled init)
|
||||
#ifndef GMPLESS
|
||||
mpz_init_set_str(x, argv[1],10);
|
||||
mpz_init(xmod2);
|
||||
#else
|
||||
x = std::stoll(argv[1]);
|
||||
#endif
|
||||
// Output x
|
||||
std::cout << x << '\n'; // impacted section of code
|
||||
// while x != 1 (mpz_cmp_ui is >0 if x>1, ==0 if x==1, and <0 if x<1, so !=0 means if not equal)
|
||||
while (mpz_cmp_ui(x,1) != 0) {
|
||||
mpz_mod_ui(xmod2,x,2); // xmod2 = x % 2
|
||||
if (mpz_cmp_ui(xmod2,0) == 0) { // If xmod2 == 0
|
||||
mpz_cdiv_q_ui(x,x,2); // x /= 2
|
||||
} else { // If xmod2 != 0
|
||||
mpz_mul_ui(x,x,3); // x *= 3
|
||||
mpz_add_ui(x,x,1); // x += 1
|
||||
}
|
||||
std::cout << x << '\n';
|
||||
}
|
||||
}
|
||||
|
11
nongmp.hpp
Normal file
11
nongmp.hpp
Normal file
@ -0,0 +1,11 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
// Make it build gmpless
|
||||
#define mpz_t long long
|
||||
#define GMPLESS
|
||||
#define mpz_cmp_ui(a,b) (a != b)
|
||||
#define mpz_mod_ui(a,b,c) a = b % c
|
||||
#define mpz_cdiv_q_ui(a,b,c) a = b / c
|
||||
#define mpz_mul_ui(a,b,c) a = b * c
|
||||
#define mpz_add_ui(a,b,c) a = b + c
|
1
withgmp.hpp
Normal file
1
withgmp.hpp
Normal file
@ -0,0 +1 @@
|
||||
#include <gmpxx.h>
|
Loading…
Reference in New Issue
Block a user