All Euler problems
Project Euler

Balanced Ternary Representation

In balanced ternary, digits are {-1, 0, 1} (written as T, 0, 1). Every integer has a unique balanced ternary representation. Find the sum of all positive integers n <= 10^6 whose balanced ternary r...

Source sync Apr 19, 2026
Problem #0913
Level Level 37
Solved By 163
Languages C++, Python
Answer 2101925115560555020
Length 209 words
linear_algebrasearchgreedy

Problem Statement

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

The numbers from $1$ to $12$ can be arranged into a $3 \times 4$ matrix in either row-major or column-major order: $$R=\begin{pmatrix} 1 & 2 & 3 & 4\\ 5 & 6 & 7 & 8\\ 9 & 10 & 11 & 12\end{pmatrix}, C=\begin{pmatrix} 1 & 4 & 7 & 10\\ 2 & 5 & 8 & 11\\ 3 & 6 & 9 & 12\end{pmatrix}$$ By swapping two entries at a time, at least $8$ swaps are needed to transform $R$ to $C$.

Let $S(n, m)$ be the minimal number of swaps needed to transform an $n\times m$ matrix of $1$ to $nm$ from row-major order to column-major order. Thus $S(3, 4) = 8$.

You are given that the sum of $S(n, m)$ for $2 \leq n \leq m \leq 100$ is $12578833$.

Find the sum of $S(n^4, m^4)$ for $2 \leq n \leq m \leq 100$.

Problem 913: Balanced Ternary Representation

Mathematical Analysis

A number in balanced ternary uses digits di{1,0,1}d_i \in \{-1, 0, 1\} so that n=idi3in = \sum_i d_i \cdot 3^i. We need representations with only digits ±1\pm 1.

Numbers expressible as i=0kϵi3i\sum_{i=0}^{k} \epsilon_i 3^i where ϵi{1,1}\epsilon_i \in \{-1, 1\} form a specific subset. For kk digits, there are 2k+12^{k+1} such numbers (half positive, half negative, by symmetry).

Derivation

For a kk-digit balanced ternary number (digits d0,,dkd_0, \ldots, d_k each ±1\pm 1), the value is i=0kdi3i\sum_{i=0}^{k} d_i \cdot 3^i.

By symmetry, for each positive such number nn, n-n is also representable. The positive numbers have dk=1d_k = 1.

We enumerate all 2k2^k choices for (d0,,dk1)(d_0,\ldots,d_{k-1}) with dk=1d_k=1 and collect those with value in [1,106][1, 10^6].

Max kk: 3k106+(3k1)/23^k \leq 10^6 + (3^k - 1)/2 gives k12k \leq 12 (since 312=5314413^{12} = 531441, and 313=1594323>1063^{13} = 1594323 > 10^6).

Proof of Correctness

Each balanced ternary representation is unique. We generate all values with no-zero digits and filter for [1,106][1, 10^6].

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

  • Enumeration: O(2k)O(2^k) where k=O(log3N)k = O(\log_3 N), so O(Nlog32)O(N0.63)O(N^{\log_3 2}) \approx O(N^{0.63}).
  • Brute-force: O(NlogN)O(N \log N) to convert each number.

Answer

2101925115560555020\boxed{2101925115560555020}

Code

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

C++ project_euler/problem_913/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    long long N = 1000000;
    vector<long long> vals = {0};
    long long pw = 1;
    while (pw <= 2 * N) {
        vector<long long> nv;
        for (long long v : vals) {
            nv.push_back(v + pw);
            nv.push_back(v - pw);
        }
        for (long long v : nv) vals.push_back(v);
        pw *= 3;
    }
    long long sum = 0;
    sort(vals.begin(), vals.end());
    vals.erase(unique(vals.begin(), vals.end()), vals.end());
    for (long long v : vals)
        if (v >= 1 && v <= N) sum += v;
    cout << sum << endl;
    return 0;
}