Tim Varley Logo
Tim Varley Systems Engineer
Problem #55 medium

Lychrel numbers

View on Project Euler

If we take 47, reverse and add, 47 + 74 = 121, which is palindromic.

Not all numbers produce palindromes so quickly. For example, 349 took three iterations to arrive at a palindrome.

Although no one has proved it yet, it is thought that some numbers, like 196, never produce a palindrome. A number that never forms a palindrome through the reverse and add process is called a Lychrel number.

How many Lychrel numbers are there below ten-thousand?

Implementations

cpp
#include <iostream>
#include <string>
#include <algorithm>
static std::string add_strings(const std::string& a, const std::string& b) {
std::string result;
int carry = 0;
int i = a.size() - 1;
int j = b.size() - 1;
while (i >= 0 || j >= 0 || carry) {
int sum = carry;
if (i >= 0) sum += a[i--] - '0';
if (j >= 0) sum += b[j--] - '0';
carry = sum / 10;
result.push_back(sum % 10 + '0');
}
std::reverse(result.begin(), result.end());
return result;
}
static bool is_palindrome_lychrel(const std::string& s) {
std::string rev = s;
std::reverse(rev.begin(), rev.end());
return s == rev;
}
bool is_lychrel(long long n) {
std::string n_str = std::to_string(n);
for (int iter = 0; iter < 50; ++iter) {
std::string rev = n_str;
std::reverse(rev.begin(), rev.end());
std::string sum_str = add_strings(n_str, rev);
if (is_palindrome_lychrel(sum_str)) return false;
n_str = sum_str;
}
return true;
}
int lychrel_numbers() {
int count = 0;
for (long long i = 1; i < 10000; ++i) {
if (is_lychrel(i)) ++count;
}
return count;
}
#if ! defined UNITTEST_MODE
int main(int argc, char const *argv[]) {
std::cout << lychrel_numbers() << std::endl;
}
#endif // UNITTEST_MODE

Solution Notes

Mathematical Background

Lychrel numbers emerge from the mysterious “reverse-and-add” process, where numbers are repeatedly added to their digit reversals until a palindrome forms. While most numbers quickly converge to palindromes, certain numbers (like 196) resist, potentially forever. This process reveals deep connections to number theory and unsolved conjectures in mathematics.

Algorithm Analysis

The solution iterates through numbers 1-9999, applying the reverse-and-add process up to 50 times per number. Each iteration involves string manipulation for reversal and big integer arithmetic to handle growing numbers. The 50-iteration limit prevents infinite loops while capturing all known Lychrel numbers below 10,000.

Key Insights

Exactly 249 Lychrel numbers lurk below 10,000, with 196 being the smallest and most notorious candidate. Surprisingly, some palindromes themselves are Lychrel numbers! The algorithm’s efficiency stems from early termination on palindrome detection, making it feasible despite the computational intensity.

Educational Value

This problem introduces elegant string algorithms for digit manipulation and highlights the beauty of iterative processes in computational math. It touches on unsolved problems in mathematical conjectures, showing how programming can explore theoretical frontiers. Perfect for understanding why some numbers “fight back” against becoming palindromic!