sum of square numbers by tails (demo)

# this code demonstrates how to calculate with sed (not highly golfed)

# to ucd (e.g. 125="0-0--0-----")
:0
s/[1-9]/&-/g
y/123456789/012345678/
t0

# to unary number (multiplier only)
h
:1
s/-0/0----------/
t1
s/0//g
x

# make n*2+1
s/-/--/g
s/$/-/

# loop counter
s/$/"/

# mul by n++ twice
:2
y/-/!/
G         # load n
:3
s/!\(.*\n\(.*\)\)/\2\1/  # replace each ! with n (unary)
s/0\?-\{10\}/-0/         # carry up if applicable
t3
s/\n.*//  # unload n
x
s/$/-/    # n++
x
T
s/"//     # counter-- && redo :2
t2

# div by 6
:4
s/------/!/
t4
s/-\(0!*\)/\1!----/
t4

# to decimal
:5
s/!/<12345678901>/
s/\(.\?\)<.*\1\(.\).*>/\2/
t5

# print

Note that non-ascii characters in the above source code will be escaped (such as \x9f).

download

return to the top page