diff --git a/README.md b/README.md index 7e8ca51..52887b7 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,8 @@ # lychrel Iterate through numbers to rule out that they are lychrel numbers +compile with: g++ -o lychrel lychrel.cpp lychrel.hpp main.cpp + +run with: ./lychrel + https://en.wikipedia.org/wiki/Lychrel_number diff --git a/lychrel b/lychrel new file mode 100755 index 0000000..f7a2e93 Binary files /dev/null and b/lychrel differ diff --git a/lychrel.cpp b/lychrel.cpp index e8144ee..5fd57bb 100644 --- a/lychrel.cpp +++ b/lychrel.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "lychrel.hpp" long long get_len (string a, string b) { @@ -14,7 +15,7 @@ string add (string a, string b) { char result[len + 2] = { '0' }; - if(len == -1) { + if(len == -1 or len == LLONG_MAX) { return "-1"; } else { int reg = 0; diff --git a/main.cpp b/main.cpp index 246b2ea..c5e6f88 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "lychrel.hpp" @@ -8,16 +9,23 @@ int main(int argc, char **argv) string a = argv[1]; string b(a.rbegin(),a.rend()); string added = add(a,b); + if(added == "-1") { + cerr << "ERROR: adding error detected, we might have reached the end of our datatype or some how the two numbers don't have the same legth\n"; + return 1; + } long long it = 0; do { cout << "Iteration " << ++it << " currently working on " << added << endl; a = added; b = string(a.rbegin(),a.rend()); added = add(a,b); - } while(! is_palindrome(added) ); + if(added == "-1") { + cerr << "ERROR: adding error detected, we might have reached the end of our datatype or some how the two numbers don't have the same legth\n"; + return 1; + } + } while(! is_palindrome(added)); cout << "Iteration " << ++it << " currently working on " << added << endl; - cout << "That consists of\n" << a << "\n+\n" << b << "\nand that it is a palindrome." << endl; - + cout << "That consists of\n" << a << "\n+\n" << b << "\nand that it " << (is_palindrome(added) ? "is" : "is not") << " a palindrome." << endl; return 0; }