All Euler problems
Project Euler

Circular Logic

A 6-input binary truth table tau(a,b,c,d,e,f) must satisfy: tau(a,b,c,d,e,f) wedge tau(b,c,d,e,f, a + (b wedge c)) = 0 for all inputs (a,b,c,d,e,f) in {0,1}^6. How many such truth tables exist?

Source sync Apr 19, 2026
Problem #0209
Level Level 09
Solved By 2,908
Languages C++, Python
Answer 15964587728784
Length 282 words
combinatoricsgraphlinear_algebra

Problem Statement

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

\(k\)-input binary truth table is a map from \(k\) input bits (binary digits, \(0\) [false] or \(1\) [true]) to \(1\) output bit. For example, the \(2\)-input binary truth tables for the logical \(\mathbin {\text {AND}}\) and \(\mathbin {\text {XOR}}\) functions are:




\(x\) \(y\) \(x\) \(\mathbin {\text {AND}}\) \(y\)



\(0\) \(0\) \(0\)



\(0\) \(1\) \(0\)



\(1\) \(0\) \(0\)



\(1\) \(1\) \(1\)



auto




\(x\) \(y\) \(x\) \(\mathbin {\text {XOR}}\) \(y\)



\(0\) \(0\) \(0\)



\(0\) \(1\) \(1\)



\(1\) \(0\) \(1\)



\(1\) \(1\) \(0\)



How many \(6\)-input binary truth tables, \(\tau \), satisfy the formula \[\tau (a, b, c, d, e, f) \mathbin {\text {AND}} \tau (b, c, d, e, f, a \mathbin {\text {XOR}} (b \mathbin {\text {AND}} c)) = 0\] for all \(6\)-bit inputs \((a, b, c, d, e, f)\)?

Problem 209: Circular Logic

Analysis

Constraint as a Graph

The condition says: for each input x=(a,b,c,d,e,f)x = (a,b,c,d,e,f), at most one of τ(x)\tau(x) and τ(σ(x))\tau(\sigma(x)) can be 1, where:

σ(a,b,c,d,e,f)=(b,c,d,e,f,a(bc))\sigma(a,b,c,d,e,f) = (b,c,d,e,f, a \oplus (b \wedge c))

This defines a permutation σ\sigma on the 64 inputs {0,1}6\{0,1\}^6. The constraint is that for each input xx, we cannot have both τ(x)=1\tau(x) = 1 and τ(σ(x))=1\tau(\sigma(x)) = 1.

Cycle Decomposition

Since σ\sigma is a function from a finite set to itself, it decomposes into cycles and tails. In fact, σ\sigma is a bijection (it’s invertible: given (b,c,d,e,f,g)(b,c,d,e,f,g), we can recover a=g(bc)a = g \oplus (b \wedge c)), so it’s a permutation and decomposes into disjoint cycles.

Independent Set Counting on Cycles

The constraint says: on each cycle of σ\sigma, we cannot assign τ=1\tau = 1 to two consecutive elements. This is the problem of counting independent sets on a cycle graph.

For a cycle of length nn, the number of independent sets (including the empty set) is:

Ln=Fn1+Fn+1L_n = F_{n-1} + F_{n+1}

where FkF_k is the kk-th Fibonacci number (Lucas numbers).

Equivalently, Ln=Lucas(n)L_n = \text{Lucas}(n).

Computing the Cycle Structure

We enumerate all 64 inputs, apply σ\sigma repeatedly, and find the cycle lengths. The total count is the product of Lucas numbers over all cycles.

Cycle Lengths

Computing σ\sigma for all 64 inputs and finding cycles:

  • The permutation σ\sigma on {0,1}6\{0,1\}^6 has the following cycle structure (computed programmatically):
    • Cycles of various lengths

The product of Lucas numbers for each cycle gives the answer.

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

Answer

15964587728784\boxed{15964587728784}

Code

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

C++ project_euler/problem_209/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    // sigma(a,b,c,d,e,f) = (b,c,d,e,f, a XOR (b AND c))
    // Encode (a,b,c,d,e,f) as a 6-bit integer: a*32 + b*16 + c*8 + d*4 + e*2 + f

    auto sigma = [](int x) -> int {
        int a = (x >> 5) & 1;
        int b = (x >> 4) & 1;
        int c = (x >> 3) & 1;
        int d = (x >> 2) & 1;
        int e = (x >> 1) & 1;
        int f = x & 1;
        int new_f = a ^ (b & c);
        return (b << 5) | (c << 4) | (d << 3) | (e << 2) | (f << 1) | new_f;
    };

    // Find cycle structure
    vector<bool> visited(64, false);
    vector<int> cycle_lengths;

    for (int i = 0; i < 64; i++) {
        if (visited[i]) continue;
        int len = 0;
        int cur = i;
        while (!visited[cur]) {
            visited[cur] = true;
            cur = sigma(cur);
            len++;
        }
        cycle_lengths.push_back(len);
    }

    // For each cycle of length n, count independent sets = Lucas(n)
    // Lucas(1) = 1, Lucas(2) = 3, Lucas(n) = Lucas(n-1) + Lucas(n-2)
    // But Lucas(1) should be 1 for self-loops (tau(x) AND tau(x) = 0 => tau(x) = 0)
    // Standard Lucas: L(1)=1, L(2)=3, L(n)=L(n-1)+L(n-2)

    auto lucas = [](int n) -> long long {
        if (n == 1) return 1;
        if (n == 2) return 3;
        long long a = 1, b = 3;
        for (int i = 3; i <= n; i++) {
            long long c = a + b;
            a = b;
            b = c;
        }
        return b;
    };

    long long answer = 1;
    for (int len : cycle_lengths) {
        answer *= lucas(len);
    }

    cout << answer << endl;
    return 0;
}