diff --git a/lychrel.cpp b/lychrel.cpp new file mode 100644 index 0000000..e8144ee --- /dev/null +++ b/lychrel.cpp @@ -0,0 +1,52 @@ +#include +#include +#include "lychrel.hpp" + +long long get_len (string a, string b) { + + return a.length() == b.length() ? a.length(): -1; + +} + +string add (string a, string b) { + + long long len = get_len(a,b); + + char result[len + 2] = { '0' }; + + if(len == -1) { + return "-1"; + } else { + int reg = 0; + for(long long i = len +1; i >= 0; i--) { + long long rescount = i; + long long numcount = i - 1; + if(rescount == 0) { + result[rescount] = (char) reg + '0'; + } else { + int x = a[numcount] - '0'; + int y = b[numcount] - '0'; + int temp = x+y+reg; + if(temp > 9) { + result[rescount] = (char) temp - 10 + '0'; + reg = 1; + } else { + result[rescount] = (char) temp + '0'; + reg = 0; + } + } + } + + } + result[len+1] = '\0'; + string finresult = result; + if(result[0] == 0 + '0') + finresult.erase(0,1); + return finresult; + +} + +bool is_palindrome (string str) { + return str == string(str.rbegin(),str.rend()); +} + diff --git a/lychrel.hpp b/lychrel.hpp new file mode 100644 index 0000000..276b755 --- /dev/null +++ b/lychrel.hpp @@ -0,0 +1,8 @@ +#include +#include + +using namespace std; + +long long get_len (string a, string b); +string add (string a, string b); +bool is_palindrome (string str); diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..246b2ea --- /dev/null +++ b/main.cpp @@ -0,0 +1,23 @@ +#include +#include +#include "lychrel.hpp" + + +int main(int argc, char **argv) +{ + string a = argv[1]; + string b(a.rbegin(),a.rend()); + string added = add(a,b); + 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) ); + cout << "Iteration " << ++it << " currently working on " << added << endl; + cout << "That consists of\n" << a << "\n+\n" << b << "\nand that it is a palindrome." << endl; + + + return 0; +}