All Euler problems
Project Euler

High Powers of Irrational Numbers

Given a positive integer a that is not a perfect square, define f(a, n) = floor((ceil(sqrt(a)) + sqrt(a))^n) where floor() and ceil() denote the floor and ceiling functions. Compute G(N) = sum_(a=1...

Source sync Apr 19, 2026
Problem #0721
Level Level 23
Solved By 508
Languages C++, Python
Answer 700792959
Length 291 words
modular_arithmeticlinear_algebrasequence

Problem Statement

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

Given is the function \(f(a,n)=\lfloor (\lceil \sqrt a \rceil + \sqrt a)^n \rfloor \).

\(\lfloor \cdot \rfloor \) denotes the floor function and \(\lceil \cdot \rceil \) denotes the ceiling function.

\(f(5,2)=27\) and \(f(5,5)=3935\).

\(G(n) = \displaystyle \sum _{a=1}^n f(a, a^2).\)

\(G(1000) \bmod 999\,999\,937=163861845. \)

Find \(G(5\,000\,000).\) Give your answer modulo \(999\,999\,937\).

Problem 721: High Powers of Irrational Numbers

Mathematical Foundation

Theorem 1 (Companion Integer Sequence). Let aa be a positive integer that is not a perfect square and set m=am = \lceil\sqrt{a}\rceil. Define α=m+a\alpha = m + \sqrt{a} and β=ma\beta = m - \sqrt{a}. Then the sequence Tn=αn+βnT_n = \alpha^n + \beta^n satisfies the linear recurrence

Tn=2mTn1(m2a)Tn2,T0=2,  T1=2m,T_n = 2m\,T_{n-1} - (m^2 - a)\,T_{n-2}, \quad T_0 = 2,\; T_1 = 2m,

and TnZT_n \in \mathbb{Z} for all n0n \ge 0.

Proof. The values α\alpha and β\beta are the two roots of the quadratic

x2(α+β)x+αβ=x22mx+(m2a)=0.x^2 - (\alpha + \beta)x + \alpha\beta = x^2 - 2mx + (m^2 - a) = 0.

By Newton’s identity for power sums of roots, Tn=(α+β)Tn1αβTn2=2mTn1(m2a)Tn2T_n = (\alpha+\beta)T_{n-1} - \alpha\beta\,T_{n-2} = 2m\,T_{n-1} - (m^2-a)\,T_{n-2}. The initial conditions are T0=α0+β0=2T_0 = \alpha^0 + \beta^0 = 2 and T1=α+β=2mT_1 = \alpha + \beta = 2m. Since 2m2m and m2am^2 - a are integers and T0,T1ZT_0, T_1 \in \mathbb{Z}, induction on nn gives TnZT_n \in \mathbb{Z} for all n0n \ge 0. \square

Lemma 1 (Floor Extraction). For non-perfect-square aa and n1n \ge 1, we have f(a,n)=Tn1f(a,n) = T_n - 1.

Proof. Since aa is not a perfect square, m=am = \lceil\sqrt{a}\rceil satisfies m>am > \sqrt{a}, so β=ma>0\beta = m - \sqrt{a} > 0. Moreover, ma+1m \le \sqrt{a} + 1 (as mm is the least integer a\ge \sqrt{a}), which gives β=ma1\beta = m - \sqrt{a} \le 1. The inequality is strict because aa is not a perfect square, so aZ\sqrt{a} \notin \mathbb{Z} and thus ma<1m - \sqrt{a} < 1. Hence 0<β<10 < \beta < 1, and therefore 0<βn<10 < \beta^n < 1 for all n1n \ge 1.

Now αn=Tnβn\alpha^n = T_n - \beta^n where TnT_n is a positive integer and 0<βn<10 < \beta^n < 1. Therefore Tn1<αn<TnT_n - 1 < \alpha^n < T_n, which gives αn=Tn1\lfloor \alpha^n \rfloor = T_n - 1. \square

Lemma 2 (Matrix Recurrence). The recurrence Tn=2mTn1cTn2T_n = 2m\,T_{n-1} - c\,T_{n-2} (where c=m2ac = m^2 - a) admits the matrix form

(TnTn1)=(2mc10)n1(2m2).\begin{pmatrix} T_n \\ T_{n-1} \end{pmatrix} = \begin{pmatrix} 2m & -c \\ 1 & 0 \end{pmatrix}^{n-1} \begin{pmatrix} 2m \\ 2 \end{pmatrix}.

Proof. Direct verification: the matrix equation encodes Tn=2mTn1cTn2T_n = 2m\,T_{n-1} - c\,T_{n-2} in the first row, and Tn1=Tn1T_{n-1} = T_{n-1} in the second row. Induction on nn yields the stated power formula. \square

Editorial

Compute G(N) = sum_{a=1}^{N} f(a, a^2) mod 999999937 where f(a, n) = floor((ceil(sqrt(a)) + sqrt(a))^n) and a ranges over non-perfect-squares. Key insight: alpha = ceil(sqrt(a)) + sqrt(a), beta = ceil(sqrt(a)) - sqrt(a) satisfy a quadratic, so alpha^n + beta^n is an integer (via linear recurrence). Since 0 < beta < 1, f(a,n) = alpha^n + beta^n - 1.

Pseudocode

Compute T_{a^2} mod p via matrix exponentiation
if exp is odd

Complexity Analysis

  • Time: For each non-perfect-square aNa \le N, matrix exponentiation computes Ma21M^{a^2-1} using O(log(a2))=O(loga)O(\log(a^2)) = O(\log a) multiplications of 2×22 \times 2 matrices over Z/pZ\mathbb{Z}/p\mathbb{Z}. Each multiplication is O(1)O(1). There are NO(N)N - O(\sqrt{N}) non-perfect-square values. Total: O(NlogN)O(N \log N).
  • Space: O(1)O(1) auxiliary space per value of aa (two 2×22\times 2 matrices). Overall O(N)O(\sqrt{N}) for the perfect-square set, or O(1)O(1) if squares are detected on the fly.

Answer

700792959\boxed{700792959}

Code

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

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

/*
 * Problem 721: High Powers of Irrational Numbers
 *
 * For non-perfect-square a, f(a,n) = floor((ceil(sqrt(a)) + sqrt(a))^n).
 * Using the companion algebraic integer beta = ceil(sqrt(a)) - sqrt(a),
 * we have alpha^n + beta^n = T_n (integer), and f(a,n) = T_n - 1.
 *
 * T_n satisfies: T_k = 2m * T_{k-1} - (m^2-a) * T_{k-2}
 * where m = ceil(sqrt(a)).
 *
 * We compute T_{a^2} mod p via 2x2 matrix exponentiation.
 */

const long long MOD = 999999937LL;

typedef vector<vector<long long>> Mat;

Mat mat_mul(const Mat& A, const Mat& B, long long mod) {
    int n = A.size();
    Mat C(n, vector<long long>(n, 0));
    for (int i = 0; i < n; i++)
        for (int k = 0; k < n; k++) if (A[i][k])
            for (int j = 0; j < n; j++)
                C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % mod;
    return C;
}

Mat mat_pow(Mat M, long long p, long long mod) {
    int n = M.size();
    Mat result(n, vector<long long>(n, 0));
    for (int i = 0; i < n; i++) result[i][i] = 1;
    while (p > 0) {
        if (p & 1) result = mat_mul(result, M, mod);
        M = mat_mul(M, M, mod);
        p >>= 1;
    }
    return result;
}

long long compute_T(long long m, long long c, long long n, long long mod) {
    // T_k = 2m * T_{k-1} - c * T_{k-2}, T_0=2, T_1=2m
    if (n == 0) return 2 % mod;
    if (n == 1) return (2 * m) % mod;
    Mat M = {{(2*m) % mod, (mod - c % mod) % mod}, {1, 0}};
    Mat R = mat_pow(M, n - 1, mod);
    return (R[0][0] * (2*m % mod) % mod + R[0][1] * 2 % mod) % mod;
}

bool is_perfect_square(long long a) {
    long long s = (long long)sqrt((double)a);
    while (s * s > a) s--;
    while ((s+1)*(s+1) <= a) s++;
    return s * s == a;
}

int main() {
    long long N = 1000; // Change to 5000000 for full answer
    long long total = 0;

    for (long long a = 1; a <= N; a++) {
        if (is_perfect_square(a)) continue;
        long long s = (long long)sqrt((double)a);
        while (s * s > a) s--;
        long long m = s + 1;
        long long c = m * m - a;
        long long n = a * a;
        long long T = compute_T(m, c, n, MOD);
        total = (total + T - 1 + MOD) % MOD;
    }

    cout << total << endl;
    return 0;
}