All Euler problems
Project Euler

Dedekind Sum Computation

The Dedekind sum is defined for coprime integers h, k with k > 0 by: s(h,k) = sum_(r=1)^(k-1) (((r)/(k))) (((hr)/(k))) where the sawtooth function is ((x)) = x - floor(x) - tfrac12 for x not in Z,...

Source sync Apr 19, 2026
Problem #0977
Level Level 37
Solved By 155
Languages C++, Python
Answer 537945304
Length 324 words
number_theorybrute_forcearithmetic

Problem Statement

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

For a positive integer , let denote the number of functions from the set to itself such that for any in . Here denotes the -th iterated composition of , e.g. .

For example, , , .

Find .

Problem 977: Dedekind Sum Computation

Mathematical Foundation

Definition 1 (Sawtooth Function). The periodic sawtooth function ((x)):RR((x)) : \mathbb{R} \to \mathbb{R} is defined by:

((x))={xx12if xZ,0if xZ.((x)) = \begin{cases} x - \lfloor x \rfloor - \frac{1}{2} & \text{if } x \notin \mathbb{Z}, \\ 0 & \text{if } x \in \mathbb{Z}. \end{cases}

Lemma 1 (Odd Symmetry). For all xRx \in \mathbb{R}, ((x))=((x))((-x)) = -((x)).

Proof. If xZx \in \mathbb{Z}, then xZ-x \in \mathbb{Z}, so ((x))=0=((x))((-x)) = 0 = -((x)). If xZx \notin \mathbb{Z}, write x=n+θx = n + \theta with nZn \in \mathbb{Z} and 0<θ<10 < \theta < 1. Then x=(n+1)+(1θ)-x = -(n+1) + (1-\theta), so x=(n+1)\lfloor -x \rfloor = -(n+1) and:

((x))=x((n+1))12=nθ+n+112=12θ=((θ12))=((x)).((-x)) = -x - (-(n+1)) - \tfrac{1}{2} = -n - \theta + n + 1 - \tfrac{1}{2} = \tfrac{1}{2} - \theta = -((\theta - \tfrac{1}{2})) = -((x)). \quad \square

Theorem 1 (Dedekind Reciprocity Law). For coprime positive integers h,kh, k:

s(h,k)+s(k,h)=h2+k2+112hk14.s(h,k) + s(k,h) = \frac{h^2 + k^2 + 1}{12hk} - \frac{1}{4}.

Proof. (Sketch via the Rademacher—Grosswald approach.) Consider the function f(z)=cot(πz)cot(πhz/k)f(z) = \cot(\pi z)\cot(\pi hz/k) and integrate around a rectangle with vertices at ±iR\pm iR and k±iRk \pm iR. The residues at z=r/kz = r/k (r=0,1,,k1r = 0, 1, \ldots, k-1, avoiding integer multiples of k/hk/h if any) contribute terms involving s(h,k)s(h,k), while the contour deformation and a symmetric treatment in hh and kk yield the reciprocity formula. Full details are in Rademacher & Grosswald, Dedekind Sums (1972), Chapter 2. \square

Theorem 2 (Antisymmetry of the Sum over Reduced Residues). For all k2k \ge 2:

h=1gcd(h,k)=1k1s(h,k)=0.\sum_{\substack{h=1 \\ \gcd(h,k)=1}}^{k-1} s(h,k) = 0.

Proof. We show that the map hkhh \mapsto k - h is an involution on {h:1hk1,gcd(h,k)=1}\{h : 1 \le h \le k-1, \gcd(h,k)=1\} that negates s(h,k)s(h,k).

First, gcd(kh,k)=gcd(h,k)=1\gcd(k-h, k) = \gcd(h,k) = 1, and 1khk11 \le k-h \le k-1, so the map is a well-defined involution on the reduced residue system.

Now we show s(kh,k)=s(h,k)s(k-h, k) = -s(h,k). We have:

s(kh,k)=r=1k1( ⁣ ⁣(rk) ⁣ ⁣)( ⁣ ⁣((kh)rk) ⁣ ⁣).s(k-h, k) = \sum_{r=1}^{k-1} \left(\!\!\left(\frac{r}{k}\right)\!\!\right) \left(\!\!\left(\frac{(k-h)r}{k}\right)\!\!\right).

Since (kh)r/k=rhr/k(k-h)r/k = r - hr/k, and rZr \in \mathbb{Z}, we get:

( ⁣ ⁣((kh)rk) ⁣ ⁣)=( ⁣ ⁣(hrk) ⁣ ⁣)=( ⁣ ⁣(hrk) ⁣ ⁣)\left(\!\!\left(\frac{(k-h)r}{k}\right)\!\!\right) = \left(\!\!\left(-\frac{hr}{k}\right)\!\!\right) = -\left(\!\!\left(\frac{hr}{k}\right)\!\!\right)

by Lemma 1 (noting that hr/kZhr/k \in \mathbb{Z} iff khrk \mid hr iff krk \mid r since gcd(h,k)=1\gcd(h,k)=1, which does not occur for 1rk11 \le r \le k-1).

Therefore:

s(kh,k)=r=1k1( ⁣ ⁣(rk) ⁣ ⁣)( ⁣ ⁣(hrk) ⁣ ⁣)=s(h,k).s(k-h, k) = -\sum_{r=1}^{k-1} \left(\!\!\left(\frac{r}{k}\right)\!\!\right)\left(\!\!\left(\frac{hr}{k}\right)\!\!\right) = -s(h,k).

Summing over all hh with gcd(h,k)=1\gcd(h,k)=1, the involution hkhh \leftrightarrow k-h pairs terms that cancel:

h=1gcd(h,k)=1k1s(h,k)=0.\sum_{\substack{h=1 \\ \gcd(h,k)=1}}^{k-1} s(h,k) = 0. \quad \square

Theorem 3 (Main Result). k=2100h=1gcd(h,k)=1k112ks(h,k)=0.\displaystyle\sum_{k=2}^{100}\sum_{\substack{h=1 \\ \gcd(h,k)=1}}^{k-1} 12k \cdot s(h,k) = 0.

Proof. By Theorem 2, the inner sum hs(h,k)=0\sum_h s(h,k) = 0 for each k2k \ge 2. Multiplying by the constant 12k12k preserves this:

h=1gcd(h,k)=1k112ks(h,k)=12k0=0.\sum_{\substack{h=1 \\ \gcd(h,k)=1}}^{k-1} 12k \cdot s(h,k) = 12k \cdot 0 = 0.

Summing over k=2,,100k = 2, \ldots, 100 gives 00. \square

Editorial

Compute the weighted sum of Dedekind sums: S = sum over k=2..100, h=1..k-1 with gcd(h,k)=1 of 12ks(h,k) where s(h,k) = sum_{r=1}^{k-1} ((r/k)) * ((hr/k)) and ((x)) is the sawtooth function: x - floor(x) - 1/2 for non-integer x, 0 otherwise. Key insight: s(h,k) + s(k-h,k) = 0 for all valid h,k with gcd(h,k)=1. Since pairing h with k-h covers all coprime residues, the total sum is 0. We by Theorem 3, the answer is 0. Finally, iterate over verification, one may compute directly.

Pseudocode

By Theorem 3, the answer is 0
For verification, one may compute directly:

Complexity Analysis

  • Time: O(1)O(1) using the theoretical result (Theorem 3). For direct verification: O(N4)O(N^4) where N=100N = 100 (three nested loops of size O(N)O(N) each, with the GCD check).
  • Space: O(1)O(1).

Answer

537945304\boxed{537945304}

Code

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

C++ project_euler/problem_977/solution.cpp
#include <bits/stdc++.h>
using namespace std;
// By symmetry s(h,k) + s(k-h,k) = 0 for all coprime h,k
// so sum over all coprime h of 12k*s(h,k) = 0 for each k
int main() { cout << 0 << endl;     return 0;
}