# Write a program to output the nth fibonacci number in quinary

Quinary is like binary, except that 1 is represented by the text of the program, and 0 is represented as the backwards text of the program.

The Fibonacci Sequence is the well known sequence

F(0) = 0, F(1) = 1, F(n) = F(n - 1) + F(n - 2)

Write a program that takes some input n and outputs the nth fibonacci number in quinary.

Standard code golf rules apply.

• Can we read program code using filesystem IO? Jan 31, 2016 at 7:36
• @HannesKarppila That's considered a standard loophole for quines. Jan 31, 2016 at 7:37
• Yes it is, but actually question does not use word quine, so I asked. Of course it usually is, but I wanted to be sure if that applies here. Jan 31, 2016 at 7:40
• @Mego I believe you are supposed to output F(n) in binary, but with the reversed program for 0 and the program code for 1. So if your code was 1234, and the input was 4, you'd output 12341234 (11), and for 5, you'd output 123443211234 (101). Jan 31, 2016 at 7:51
• Is the program allowed to be a palindrome? (Same backwards and forwards) Jan 31, 2016 at 9:17

# CJam, 30 bytes

{s"_~"+UXri{_@+}*;2bWfe|\f%}_~


Try it online!

### How it works

{                          }_~  Define a code block, and push and execute a copy.
s                              Convert the original code block to a string.
"_~"+                         Append "_~" to the string.
UXri                     Push 0, 1 and an integer N from STDIN.
{   }*               Do N times:
_                     Push a copy of the topmost integer.
@                    Rotate the bottom-most integer on top of it.
+                   Push the sum of the copy and the rotated integer.
;              Discard the topmost stack item, i.e., F(N+1).
2b            Convert the remaining stack item to base 2.
Wfe|        Logical OR all binary digits with -1.
This turns 0's into -1's.
\f%     For each binary digits A, take each Ath item of the
generated string.
This pushes the string itself for A = 1, and the
reversed string for A = -1.


# Python, 226 bytes

s='s=%r;a,b=0,1;exec"a,b=b,a+b;"*input();print"".join(map(lambda i:(s%%s)*int(i)+(s%%s)[::-1]*(1-int(i)),bin(a)[2:]))';a,b=0,1;exec"a,b=b,a+b;"*input();print"".join(map(lambda i:(s%s)*int(i)+(s%s)[::-1]*(1-int(i)),bin(a)[2:]))


It's quite a heavy modification to the standard Python Quine. It uses a,b=0,1;exec"a,b=b,a+b;" to generate Fibonacci numbers.

• I get syntax error on both python 2 and 3.
– k_g
Jan 31, 2016 at 9:22
• @k_g Weird, the program works for me in 2.7.3. Are you sure you copied the whole thing? Jan 31, 2016 at 9:25
• Oh, whoops, it works. I was using python3 in both my tests. Who knew python -> python3?
– k_g
Jan 31, 2016 at 9:29

⟮Мȫïⓑⓢⓜ+$?(a=ɕṡ+ᶈ0):aᴙ)⨝  Try it here (Firefox only). # Explanation Standard quine framework: ⟮ɕṡ+ᶈ0 ⟮Мȫïⓑⓢⓜ+$?(a=ɕṡ+ᶈ0):aᴙ)⨝ // implicit: ï=input
⟮                           // copy block start
Мȫï                       // get nth Fibonacci number
ⓑⓢⓜ                  // convert to binary, split along chars, map
+$? // is mapitem truthy? (a=ɕṡ+ᶈ0) // if so, replace w/ quine as is :aᴙ) // else replace w/ reverse quine ⨝ // join // implicit output  • What's ɕṡ+ᶈ0? Is that a quine builtin? Feb 1, 2016 at 2:03 • Naw, ɕṡ refers to character ⟮ and ᶈ0 is a string representation of the contents of the copy block. Feb 1, 2016 at 2:05 • ℹ is the quine function, which isn't used/allowed here. Feb 1, 2016 at 2:06 • That's a good way to quine without a quine builtin. Feb 1, 2016 at 2:08 • Found it by accident. Feb 1, 2016 at 2:09 # Javascript ES6, 121 bytes $=_=>[...(a=$=${$};$(),f=x=>x>1?f(x-1)+f(x-2):x)(prompt()).toString(2)].map(x=>+x?a:[...a].reverse().join).join;\$()


I'm looking for ways to shorten the Fibonacci function...

# Python 3.8 (pre-release), 113 bytes

exec(s:="f=lambda x:x<2or f(x-1)+f(x-2)\nfor i in f'{f(int(input())):b}':print(('exec(s:=%r)'%s)[::2*int(i)-1])")


Try it online!