Write a program to output the nth fibonacci number in quinary

6

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.

k_g

Posted 2016-01-31T07:18:34.443

Reputation: 171

Can we read program code using filesystem IO? – Hannes Karppila – 2016-01-31T07:36:27.103

@HannesKarppila That's considered a standard loophole for quines. – Alex A. – 2016-01-31T07:37:01.707

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. – Hannes Karppila – 2016-01-31T07:40:53.867

@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). – es1024 – 2016-01-31T07:51:47.843

@es1024 Ah, thank you for clearing that up, I didn't read so well. – Mego – 2016-01-31T07:53:01.713

@Hannes, it's tagged [tag:quine] – Peter Taylor – 2016-01-31T07:53:49.810

1Is the program allowed to be a palindrome? (Same backwards and forwards) – Loovjo – 2016-01-31T09:17:54.017

@Loovjo yes, it is, but I don't think that makes it any easier – k_g – 2016-01-31T09:21:19.200

Answers

2

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.

Dennis

Posted 2016-01-31T07:18:34.443

Reputation: 196 637

2

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.

Loovjo

Posted 2016-01-31T07:18:34.443

Reputation: 7 357

I get syntax error on both python 2 and 3. – k_g – 2016-01-31T09:22:36.220

@k_g Weird, the program works for me in 2.7.3. Are you sure you copied the whole thing? – Loovjo – 2016-01-31T09:25:17.480

1Oh, whoops, it works. I was using python3 in both my tests. Who knew python -> python3? – k_g – 2016-01-31T09:29:20.400

0

, 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

Mama Fun Roll

Posted 2016-01-31T07:18:34.443

Reputation: 7 234

What's ɕṡ+ᶈ0? Is that a quine builtin? – lirtosiast – 2016-02-01T02:03:58.993

Naw, ɕṡ refers to character and ᶈ0 is a string representation of the contents of the copy block. – Mama Fun Roll – 2016-02-01T02:05:19.063

is the quine function, which isn't used/allowed here. – Mama Fun Roll – 2016-02-01T02:06:09.577

That's a good way to quine without a quine builtin. – lirtosiast – 2016-02-01T02:08:41.793

Found it by accident. – Mama Fun Roll – 2016-02-01T02:09:06.167

Could you add a link to this language? – rpax – 2016-02-07T10:04:46.243

https://github.com/molarmanful/ESMin – Mama Fun Roll – 2016-02-07T15:45:12.410

0

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

Mama Fun Roll

Posted 2016-01-31T07:18:34.443

Reputation: 7 234