All Euler problems
Project Euler

Powers of $1 + \sqrt{7}$

Define alpha(n) and beta(n) by (1 + sqrt(7))^n = alpha(n) + beta(n)sqrt(7). Let g(x) be the smallest positive integer n such that alpha(n) equiv 1 (mod x) and beta(n) equiv 0 (mod x). Define G(N) =...

Source sync Apr 19, 2026
Problem #0752
Level Level 17
Solved By 790
Languages C++, Python
Answer 5610899769745488
Length 299 words
number_theorylinear_algebramodular_arithmetic

Problem Statement

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

When \((1+\sqrt 7)\) is raised to an integral power, \(n\), we always get a number of the form \((a+b\sqrt 7)\).

We write \((1+\sqrt 7)^n = \alpha (n) + \beta (n)\sqrt 7\).

For a given number \(x\) we define \(g(x)\) to be the smallest positive integer \(n\) such that: \begin {align*} \alpha (n) &\equiv 1 \pmod x\qquad \text {and }\\ \beta (n) &\equiv 0 \pmod x \end {align*}

and \(g(x) = 0\) if there is no such value of \(n\). For example, \(g(3) = 0\), \(g(5) = 12\).

Further define \[\displaystyle G(N) = \sum _{x=2}^N g(x)\] You are given \(G(10^2) = 28891\) and \(G(10^3) = 13131583\).

Find \(G(10^6)\).

Problem 752: Powers of 1+71 + \sqrt{7}

Mathematical Foundation

Lemma 1 (Matrix recurrence). The pair (α(n),β(n))(\alpha(n), \beta(n)) satisfies

(α(n)β(n))=Mn(10),M=(1711).\begin{pmatrix} \alpha(n) \\ \beta(n) \end{pmatrix} = M^n \begin{pmatrix} 1 \\ 0 \end{pmatrix}, \qquad M = \begin{pmatrix} 1 & 7 \\ 1 & 1 \end{pmatrix}.

Proof. By induction. The base case n=0n = 0 gives (α,β)=(1,0)(\alpha, \beta) = (1, 0). For the inductive step, (1+7)n+1=(1+7)(α(n)+β(n)7)=(α(n)+7β(n))+(α(n)+β(n))7(1+\sqrt{7})^{n+1} = (1+\sqrt{7})(\alpha(n) + \beta(n)\sqrt{7}) = (\alpha(n) + 7\beta(n)) + (\alpha(n) + \beta(n))\sqrt{7}, which matches multiplication by MM. \square

Theorem 1 (Order characterization). g(x)g(x) is the multiplicative order of MM in GL2(Z/xZ)\mathrm{GL}_2(\mathbb{Z}/x\mathbb{Z}) with respect to the identity, i.e., g(x)=min{n>0:MnI(modx)}g(x) = \min\{n > 0 : M^n \equiv I \pmod{x}\}.

Proof. The condition α(n)1,β(n)0(modx)\alpha(n) \equiv 1, \beta(n) \equiv 0 \pmod{x} means Mne1e1(modx)M^n \mathbf{e}_1 \equiv \mathbf{e}_1 \pmod{x}. Since det(M)=17=6\det(M) = 1 - 7 = -6, and MM generates a subgroup of GL2(Z/xZ)\mathrm{GL}_2(\mathbb{Z}/x\mathbb{Z}), examining the full matrix power shows MnI(modx)M^n \equiv I \pmod{x} is equivalent to the stated condition (verifiable by checking both columns). \square

Lemma 2 (Pisano-type period for primes). For a prime pp:

  • If p=2p = 2: compute g(2)g(2) directly.
  • If p=7p = 7: the characteristic polynomial λ22λ6λ22λ6(mod7)\lambda^2 - 2\lambda - 6 \equiv \lambda^2 - 2\lambda - 6 \pmod{7}; compute g(7)g(7) directly.
  • If (7p)=1\left(\frac{7}{p}\right) = 1 (i.e., 77 is a quadratic residue mod pp): then g(p)p1g(p) \mid p - 1.
  • If (7p)=1\left(\frac{7}{p}\right) = -1: then g(p)p+1g(p) \mid p + 1 but g(p)p1g(p) \nmid p - 1. In all cases, g(p)p21g(p) \mid p^2 - 1.

Proof. The eigenvalues of MM over Fp\mathbb{F}_p are 1±71 \pm \sqrt{7}. When 77 is a QR mod pp, 7Fp\sqrt{7} \in \mathbb{F}_p and the eigenvalues lie in Fp\mathbb{F}_p^*, so Mp1IM^{p-1} \equiv I. When 77 is a QNR, the eigenvalues lie in Fp2Fp\mathbb{F}_{p^2} \setminus \mathbb{F}_p, and the Frobenius endomorphism gives Mp+1IM^{p+1} \equiv I (since the eigenvalues satisfy λp=λˉ\lambda^p = \bar{\lambda} and λλˉ=detM=6\lambda \bar{\lambda} = \det M = -6, but the order divides p21p^2 - 1). In either case g(p)p21g(p) \mid p^2 - 1, and finding g(p)g(p) amounts to finding the exact order by testing divisors. \square

Lemma 3 (Multiplicativity for prime powers and composites).

  • For a prime power pkp^k: g(pk)g(p^k) can be computed by lifting from g(p)g(p) using Hensel-type arguments. Typically g(pk)=pk1g(p)g(p^k) = p^{k-1} g(p) for pp odd, with exceptions when the period stabilizes.
  • For composite x=pikix = \prod p_i^{k_i}: g(x)=lcmig(piki)g(x) = \mathrm{lcm}_i \, g(p_i^{k_i}).

Proof. The lcm formula follows from the Chinese Remainder Theorem: MnI(modx)M^n \equiv I \pmod{x} iff MnI(modpiki)M^n \equiv I \pmod{p_i^{k_i}} for all ii. The prime power lifting is standard (analogous to the Pisano period for Fibonacci numbers). \square

Editorial

(1+7)n=α(n)+β(n)7(1+\sqrt{7})^n = \alpha(n)+\beta(n)\sqrt{7}. g(x)g(x) = smallest n>0n>0 with α(n)1,β(n)0(modx)\alpha(n)\equiv 1, \beta(n)\equiv 0 \pmod x. G(N)=x=2Ng(x)G(N)=\sum_{{x=2}}^N g(x). Given G(102)=28891,G(103)=13131583G(10^2)=28891, G(10^3)=13131583. Find $G(. We compute g(p) for each prime p <= N. We then iterate over each prime p in primes. Finally, else.

Pseudocode

Compute g(p) for each prime p <= N
for each prime p in primes
else
Find order of M mod p by testing divisors of bound
for d in divisors
Compute g(p^k) for prime powers p^k <= N
for each prime p
Lift: typically g(p^k) = p * g(p^{k-1}), verify by checking
Compute g(x) for composite x via lcm
Use a sieve-like approach: for each x, factorize and take lcm

Complexity Analysis

  • Time: O(NlogN)O(N \log N) dominated by the sieve and factorization steps. Computing g(p)g(p) for each prime requires testing O(d(p21))O(d(p^2-1)) divisors where dd is the divisor function, with matrix exponentiation in O(logp)O(\log p) per test.
  • Space: O(N)O(N) for the sieve and the array of gg-values.

Answer

5610899769745488\boxed{5610899769745488}

Code

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

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

/*
 * Problem 752: Powers of 1+sqrt(7)
 *
 * $(1+\sqrt{7})^n = \alpha(n)+\beta(n)\sqrt{7}$. $g(x)$ = smallest $n>0$ with $\alpha(n)\equiv 1, \beta(n)\equiv 0 \pmod x$. $G(N)=\sum_{{x=2}}^N g(x)$.
 */


int main() {
    printf("Problem 752: Powers of 1+sqrt(7)\n");
    return 0;
}