Skip to content

Instantly share code, notes, and snippets.

@lychaxo
lychaxo / fibrep.sage
Created August 20, 2020 04:06
Zeckendorf Fibonacci representation normalization/conversion in SAGE
# https://www.reddit.com/r/baseprime/comments/id37jn/similar_number_systems/g26hq6q/?utm_source=reddit&utm_medium=web2x&context=3
# since 011 can always be change to 100 in a fib rep, we can normalize fib reps
# www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibrep.html#section4.1
# we iterate from high to low for this, so that we can handle strings of ones
# 011111 = 011000 + 111 = 100000 + 111 = 100111 = 100001 + 0110 = 100001 + 1000
# additionally, 0200 can be replaced with 1001 anywhere it occurs,
# since it is equiv to 0100 + 0100 = 0100 + 0011 = 0111 = 0110 + 1 = 0100 + 1
# and 020 in lowest end can be replaced by 101, 02 in lowest end by 10
# so this gives us a way to decrease values greater than one, as well