5
\$\begingroup\$

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.

\$\endgroup\$
8
  • \$\begingroup\$ Can we read program code using filesystem IO? \$\endgroup\$ Jan 31, 2016 at 7:36
  • \$\begingroup\$ @HannesKarppila That's considered a standard loophole for quines. \$\endgroup\$
    – Alex A.
    Jan 31, 2016 at 7:37
  • \$\begingroup\$ 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. \$\endgroup\$ Jan 31, 2016 at 7:40
  • \$\begingroup\$ @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). \$\endgroup\$
    – es1024
    Jan 31, 2016 at 7:51
  • 1
    \$\begingroup\$ Is the program allowed to be a palindrome? (Same backwards and forwards) \$\endgroup\$
    – xenia
    Jan 31, 2016 at 9:17

5 Answers 5

2
\$\begingroup\$

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.
\$\endgroup\$
2
\$\begingroup\$

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.

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

𝔼𝕊𝕄𝕚𝕟, 24 chars / 44 bytes

⟮Мȫïⓑⓢⓜ+$?(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
\$\endgroup\$
7
  • \$\begingroup\$ What's ɕṡ+ᶈ0? Is that a quine builtin? \$\endgroup\$
    – lirtosiast
    Feb 1, 2016 at 2:03
  • \$\begingroup\$ Naw, ɕṡ refers to character and ᶈ0 is a string representation of the contents of the copy block. \$\endgroup\$ Feb 1, 2016 at 2:05
  • \$\begingroup\$ is the quine function, which isn't used/allowed here. \$\endgroup\$ Feb 1, 2016 at 2:06
  • \$\begingroup\$ That's a good way to quine without a quine builtin. \$\endgroup\$
    – lirtosiast
    Feb 1, 2016 at 2:08
  • \$\begingroup\$ Found it by accident. \$\endgroup\$ Feb 1, 2016 at 2:09
0
\$\begingroup\$

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...

\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.