<!-- canonical: efficientnewlanguage.org/ai/examples/137-sum-of-squares-sigma | ai_layer_version: 0.1.0 | updated: 2026-07-28 -->

# Example 137 — Sum of squares (Σ)

`sum_of_squares_sigma.eml` computes `Σ(i^2, i in [1:n])` for five values of `n`, checking each against the closed form `n(n+1)(2n+1)/6`.

## EML

```eml
# Self-authored for the EML case corpus (no external origin). The FIRST
# case in this corpus to use EML's summation operator.
#
# That is worth saying plainly. Before this case the corpus held 119
# programs, and not one of them used `Σ` — the symbol the whole language
# is named for. Every case was written in EML's Python-shaped subset,
# which is correct EML but teaches a reader nothing about what makes EML
# different from Python with unusual punctuation. `Σ` appeared only in the
# `examples/phase*` language-feature fixtures, which are regression tests
# rather than worked examples.
#
#   Σ(i^2, i in [1:n])      compresses the accumulate-over-a-range intent
#                           into one expression, and transpiles to
#                           `sum(i ** 2 for i in range(1, n + 1))`.
#
# Each total is checked against the closed form n(n+1)(2n+1)/6 — an
# independent computation, so this verifies the summation rather than
# restating it. The closed form also makes the samples cheap to confirm by
# hand: n=10 gives 385, n=100 gives 338350.
#
# Note that `Σ` appears in the SOURCE but never inside a printed string.
# That is deliberate and was verified, not assumed: Python's stdout on a
# Windows cp950 host encodes Σ as the Big5 bytes A3 55, which the toolchain
# then reads back as UTF-8 and turns into U+FFFD. The eml:equiv gate does
# not catch it, because the interpreter and the real Python run traverse
# the same encoding path and agree with each other while both are wrong.
# Source text is read as UTF-8 and is unaffected — only stdout is.

def closed_form(n):
    return int(n * (n + 1) * (2 * n + 1) / 6)

sizes^+[1, 5, 10, 100, 1000]

0 => matches
for n in sizes:
    Σ(i^2, i in [1:n]) => total
    closed_form(n) => expected
    if total == expected:
        matches + 1 => matches
        "sum of squares to " + str(n) + " = " + str(total) + "  (closed form agrees)" => line
    else:
        "sum of squares to " + str(n) + " = " + str(total) + "  (CLOSED FORM SAYS " + str(expected) + ")" => line
    line^0

str(matches) + " of " + str(len(sizes)) + " match n(n+1)(2n+1)/6" => summary
summary^0

"" => blank
blank^0
"The same intent written out as a loop, for comparison:" => header
header^0
0 => manual
for i in [1:10]:
    manual + i * i => manual
Σ(i^2, i in [1:10]) => compressed
"  four-line loop: " + str(manual) + "   one-line summation: " + str(compressed) => compare
compare^0
if manual == compressed:
    "  four lines and one line, same answer" => note
else:
    "  MISMATCH between the loop and the summation" => note
note^0
```

## Python (deterministic transpilation)

```python
def closed_form(n):
    return int(n * (n + 1) * (2 * n + 1) / 6)

sizes = [1, 5, 10, 100, 1000]
matches = 0
for n in sizes:
    total = sum(i**2 for i in range(1, n+1))
    expected = closed_form(n)
    if total == expected:
        matches = matches + 1
        line = "sum of squares to " + str(n) + " = " + str(total) + "  (closed form agrees)"
    else:
        line = "sum of squares to " + str(n) + " = " + str(total) + "  (CLOSED FORM SAYS " + str(expected) + ")"
    print(line)
summary = str(matches) + " of " + str(len(sizes)) + " match n(n+1)(2n+1)/6"
print(summary)
blank = ""
print(blank)
header = "The same intent written out as a loop, for comparison:"
print(header)
manual = 0
for i in range(1, 11):
    manual = manual + i * i
compressed = sum(i**2 for i in range(1, 11))
compare = "  four-line loop: " + str(manual) + "   one-line summation: " + str(compressed)
print(compare)
if manual == compressed:
    note = "  four lines and one line, same answer"
else:
    note = "  MISMATCH between the loop and the summation"
print(note)
```

## stdout (executed)

```text
sum of squares to 1 = 1  (closed form agrees)
sum of squares to 5 = 55  (closed form agrees)
sum of squares to 10 = 385  (closed form agrees)
sum of squares to 100 = 338350  (closed form agrees)
sum of squares to 1000 = 333833500  (closed form agrees)
5 of 5 match n(n+1)(2n+1)/6

The same intent written out as a loop, for comparison:
  four-line loop: 385   one-line summation: 385
  four lines and one line, same answer
```

## Round-trip

`ok: true` — round-trip fixpoint reached (python1 == python2)

## Trace event types

eml:run:start · eml:def · eml:assign · eml:sum · eml:call · eml:return · eml:output · eml:run:done
