Skip to content

Instantly share code, notes, and snippets.

@scythe
Last active August 29, 2015 14:17
Show Gist options
  • Save scythe/844a4977fa22b67e5c9e to your computer and use it in GitHub Desktop.
Save scythe/844a4977fa22b67e5c9e to your computer and use it in GitHub Desktop.
hofstadter_female_differences.lua
f = {[0] = 1}
m = {[0] = 0}
len = 100000000
for i = 1, len do
m[i] = i - f[m[i-1]]
f[i] = i - m[f[i-1]]
end
df, dm = {}, {}
for i = 1, len do
df[i], dm[i] = f[i] - f[i-1], m[i] - m[i-1]
end
function cond_a(t, n)
return t[n] == 1 and t[n-1] == 1 and t[n-2] == 0 and t[n-3] == 1 and t[n-4] == 0
end
function cond_b(t, n)
return t[n] == 1 and t[n-1] == 1 and t[n-2] == 0
end
function extract_fractal_and_print(t)
local counter, cnt_t = (level == 0 and 1 or 0), {}
for i = 1, #t do
if i < 9 then print(df[i]) end
if i > 3 then
counter = counter + 1
if cond_a(df, i) and counter == 5 then
cnt_t[#cnt_t+1] = 1
counter = 0
elseif cond_b(df, i) and counter == 3 then
cnt_t[#cnt_t+1] = 0
counter = 0
elseif counter > 5 then
print("XXXX")
cnt_t[#cnt_t+1] = "XXXX"
end
end
end
return cnt_t
end
level = 0
repeat
print("Level: " .. level)
df = extract_fractal_and_print(df)
level = level + 1
until #df < 15
print("End:")
print(table.concat(df, "\n"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment