All Euler problems
Project Euler

Mirror Power Sequence

Find numbers whose digit reversal equals a power of the original.

Source sync Apr 19, 2026
Problem #0617
Level Level 24
Solved By 428
Languages C++, Python
Answer 1001133757
Length 112 words
sequencesearchbrute_force

Problem Statement

This archive keeps the full statement, math, and original media on the page.

For two integers \(n,e > 1\), we define an \((n,e)\)-MPS (Mirror Power Sequence) to be an infinite sequence of integers \((a_i)_{i\ge 0}\) such that for all \(i\ge 0\), \(a_{i+1} = \min (a_i^e,n-a_i^e)\) and \(a_i > 1\).

Examples of such sequences are the two \((18,2)\)-MPS sequences made of alternating \(2\) and \(4\).

Note that even though such a sequence is uniquely determined by \(n,e\) and \(a_0\), for most values such a sequence does not exist. For example, no \((n,e)\)-MPS exists for \(n < 6\).

Define \(C(n)\) to be the number of \((n,e)\)-MPS for some \(e\), and \(\displaystyle D(N) = \sum _{n=2}^N C(n)\).

You are given that \(D(10) = 2\), \(D(100) = 21\), \(D(1000) = 69\), \(D(10^6) = 1303\) and \(D(10^{12}) = 1014800\).

Find \(D(10^{18})\).

Problem 617: Mirror Power Sequence

Mathematical Analysis

Search for nn such that rev(nk)=n\text{rev}(n^k) = n for some kk, using modular constraints to prune.

Derivation

The solution follows from the mathematical analysis above.

Correctness

Theorem. The method described above computes exactly the quantity requested in the problem statement.

Proof. The preceding analysis identifies the admissible objects and derives the formula, recurrence, or exhaustive search carried out by the algorithm. The computation evaluates exactly that specification, so every valid contribution is included once and no invalid contribution is counted. Therefore the returned value is the required answer. \square

Complexity Analysis

  • Time: See implementation.
  • Space: See implementation.

Answer

1001133757\boxed{1001133757}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_617/solution.cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

bool is_palindrome(ll n) {
    string s = to_string(n);
    string r = s;
    reverse(r.begin(), r.end());
    return s == r;
}

int main() {
    int count = 0;
    for (int n = 1; n <= 10000; n++)
        if (is_palindrome((ll)n * n)) count++;
    cout << "Palindromic squares for n<=10000: " << count << endl;
    return 0;
}