This was a registration CAPTCHA problem as of 2025:
Among the first 510 thousand square numbers, what is the sum of all the odd squares?
Python solution:
s = 0
for i in range(1, 510001, 2):
    s += i*i
print(s)
At: euler/0.py
Solution:
233168
Solutions to the ProjectEuler+ version:
The original can be found with:
printf '1\n1000\n' | euler/1.py
A(x) = x + 1
Z(u)(v) = v
S(u)(v)(w) = v(u(v)(w))
Let's resolve the second example ourselves:
S
  (S)
  (S(S))
  (S(Z))
(A)
(0)

S
(S)
(
  S
  (S(S))
  (S(Z))
)
(A)
(0)

S
(S(S))
(S(Z))
(
  S
  (
    S
    (S(S))
    (S(Z))
  )
  (A)
)
(0)

S
(Z)
(
  S(S)
  (S(Z))
  (
    S
    (
      S
      (S(S))
      (S(Z))
    )
    (A)
  )
)
(0)

S(S)
(S(Z))
(
  S
  (
    S
    (S(S))
    (S(Z))
  )
  (A)
)
(
  Z
  (
    S(S)
    (S(Z))
    (
      S
      (
        S
        (S(S))
        (S(Z))
      )
      (A)
    )
  )
  (0)
)

S
(S)
(S(Z))
(
  S
  (
    S
    (S(S))
    (S(Z))
  )
  (A)
)
(0)
TODO: how long would this be?
So we see that all of these rules resolve quite quickly and do not go into each other. S however offers some problems, in that:
C_0 = Z
C_i = S(C_{i-1})
D_i = C_i(S)(S)
So we see that D_i goes somewhat simply into C_i, and C_i is recursive giving:
S^i(Z)
Calculate the nine first digits of:
D_a(D_b)(D_c)(C_d)(A)(e)
Removing D_a:
S^i(Z)S)(S)(D_b)(D_c)(C_d)(A)(e)
A naive T in Python is:
from collections import deque

def T(a: int, b: int, N: int) -> int:
    total = a
    q = deque([a] * (a - 1))
    is_a = False
    for i in range(N - 1):
        cur = q.popleft()
        total += cur
        q.extend([a if is_a else b] * cur)
        is_a = not is_a
    return total

assert T(2, 3, 10) == 25
assert T(4, 2, 10**4) == 30004
assert T(5, 8, 10**6) == 6499871
which passes the tests, but takes half a second on PyPy. So clearly it is not going to work for 22332223332233 which has 14 digits.
Maybe if T is optimized enough, then we can just bruteforce over the ~40k possible sum ranges 2 to 223. 1 second would mean 14 hours to do them all, so bruteforce but doable. Otherwise another optimization step may be needed at that level as well: one wonders if multiple sums can be factored out, or if the modularity can of the answer can help in a meaningful way. The first solver was ecnerwala using C/C++ in 1 hour, so there must be another insight missing, unless they have access to a supercomputer.
The first idea that comes to mind to try and optimize T is that this is a dynamic programming, but then the question is what is the recurrence relation.
The sequence appears to be a generalization of the Kolakoski sequence but to numbers other than 1 and 2.
maths-people.anu.edu.au/~brent/pd/Kolakoski-ACCMCC.pdf "A fast algorithm for the Kolakoski sequence" might provide the solution, the paper says:
It is conjectured that the algorithm runs in time and space , where
and provides exactly a recurrence relation and a dynamic programming approach.
www.reddit.com/r/algorithms/comments/8cv3se/kolakoski_sequence/ asks for an implementation but no one gave anything. Dupe question: math.stackexchange.com/questions/2740997/kolakoski-sequence contains an answer with Python and Rust code but just for the original 1,2 case.
github.com/runbobby/Kolakoski has some C++ code but it is not well documented so it's not immediately easy to understand what it actually does. It does appear to consider the m n case however.

Articles by others on the same topic (0)

There are currently no matching articles.