Have you learned your fib-abc?

31

1

I don't like numbers, but I do like the Fibonacci sequence. I'm sure we could work something out.

Please read one integer n from STDIN and output the nth Fibonacci number in base 26 (abcdefghijklmnopqrstuvwxyz instead of 0123456789) to STDOUT.

The first Fibonacci number is 0. The second one is 1. The nth fibonacci number is the sum of the n-2nd and n-1st Fibonacci numbers.

First 32 fib-abc numbers:

fib(0) = a
fib(1) = b
fib(2) = b
fib(3) = c
fib(4) = d
fib(5) = f
fib(6) = i
fib(7) = n
fib(8) = v
fib(9) = bi
fib(10) = cd
fib(11) = dl
fib(12) = fo
fib(13) = iz
fib(14) = on
fib(15) = xm
fib(16) = blz
fib(17) = cjl
fib(18) = dvk
fib(19) = gev
fib(20) = kaf
fib(21) = qfa
fib(22) = baff
fib(23) = bqkf
fib(24) = cqpk
fib(25) = egzp
fib(26) = gxoz
fib(27) = leoo
fib(28) = scdn
fib(29) = bdgsb
fib(30) = bvivo
fib(31) = cypnp

This is code golf, so shortest code in bytes wins!

Filip Haglund

Posted 2015-10-24T18:38:03.347

Reputation: 1 789

3@l0b0 you'd still call it base 26 because the choice of characters to represent the digits is entirely arbitrary and the common hexadecimal digits are just a convention. – Martin Ender – 2015-10-24T23:07:17.297

2It's still base26. What characters you use is arbitrary, and here we use a-z (in alphabetical order). – Filip Haglund – 2015-10-24T23:08:24.690

Right, it's a different base-26 notation from the conventional, but it's still a base-26 notation. – Lightness Races with Monica – 2015-10-25T02:05:44.720

5Why use them nasty numbers for the input? – ugoren – 2015-10-25T08:12:25.153

Name suggestion: Fibona-b-c – Matthew Roh – 2017-03-27T09:12:03.127

Answers

12

CJam, 18 bytes

UXri{_@+}*;26b'af+

Try it online in the CJam interpreter.

How it works

UX    e# Push 0 and 1.
ri{   e# Read an integer and execute the loop that many times.
  _   e#   Push a copy the topmost integer.
  @   e#   Rotate the bottom-most integer on top of the stack.
  +   e#   Pop the two topmost integers and push their sum.
}*    e#
;     e# Discard the topmost integer from the stack.
26b   e# Convert the remaining integer to base 26.
'af+  e# Add the character 'a' to each base-26 digit.

Dennis

Posted 2015-10-24T18:38:03.347

Reputation: 196 637

8

TeaScript, 34 bytes 37 51 54

TeaScript is JavaScript for golfing. It also brings ES2015 features to the average browser.

F(x)b(26)l(#C(lc()+(l<'a'?49:10)))

Try it online

Explanation

          // x is the input
F(x)      // Fibonacci from input
.b(26)    // To Base-26 string but with 0-9, a-p
          // instead of a-z, to fix this...
.l(#      // Loops through each char
   C(          // Charcode from...
       l.c()+  // Charcode from char
       (l<'a'? // If number
           49  // Add 49 to char code
          :10  // Else add 10
       )
   )
)

*This answer is non-competing

Downgoat

Posted 2015-10-24T18:38:03.347

Reputation: 27 116

1Nice golf-y version of JS! I designed my own version about a month ago, but haven't yet started an interpreter. Without a Fibonacci built-in or implicit input, this same program would be 48 bytes long. However, if I were to create a built-in and add implicit input, it'd be 34. Perhaps I should start work on an interpreter. ;) – ETHproductions – 2015-10-24T21:26:52.357

Ooh, that's a lot better. One of the tricks in my language that may apply here is making all variables uppercase (including Math, Date, etc.) and all methods lowercase, which removes the need for periods. This is just a suggestion; it may not be the best idea for this language, but I'll let you decide. (Love the name, BTW.) – ETHproductions – 2015-10-25T01:38:20.310

@ETHproductions interesting idea. I'll see if I can implement it for some cases, but as of now, I'm implementing most features through a simple find-replace method, making it difficult to implement the more complex semantics. – Downgoat – 2015-10-25T15:21:25.080

6

Mathematica, 67 61 bytes

Print[""<>Alphabet[][[Fibonacci@Input[]~IntegerDigits~26+1]]]

Calculates f(1000000) in about 51 milliseconds.

LegionMammal978

Posted 2015-10-24T18:38:03.347

Reputation: 15 731

Ah, didn't see that there was a Mathematica answer already! Mine used IntegerString to format the digits: IntegerString[Fibonacci@#~IntegerDigits~26+10,36]<>""& – None – 2015-10-25T00:17:34.330

I deleted it; using Input[] and Print[] for a fair comparison, my solution would be 66 bytes long. But Alphabet[] is a 10.1 feature, so I thought I'll leave it as a comment. – None – 2015-10-25T00:26:16.327

@user5254 I first used FromLetterNumber before seeing that it internally used Alphabet with Part and used that, except with a list of indices. – LegionMammal978 – 2015-10-25T00:30:30.120

5

Simplex v.0.6, 35 bytes

Sometimes I sigh and think, "Is this even worth submitting? It doesn't win, so why bother?" In response, I think, "Heck. It was fun. Besides, this is really fancied-up brainf*** anyhow. Not too shabby."

5_*Ij1~SRpRi@T[Uj&ERp]pSR5_Vj26@pWo
5_                                  ~~ sqrt(5)
  *                                 ~~ copy to next byte, move right
   I                                ~~ increment [sqrt(5),sqrt(5)+1]
    j1                              ~~ insert a new cell and set it to one 
                                    ~~ [sqrt(5),1,sqrt(5)+1]
      ~                             ~~ switch the previous with the current byte
                                    ~~ [1,sqrt(5),sqrt(5)+1]
       S                            ~~ perform subtraction [1-sqrt(5),0,sqrt(5)+1]
        Rp                          ~~ remove next cell [1-sqrt(5),sqrt(5)+1]
          Ri@                       ~~ take numeric input (n) into register
             T[      ]              ~~ applies the following to every cell
               U                    ~~ halves the current cell
                j&                  ~~ dumps and restores the value to the register
                  ERp               ~~ raises cell to the nth power, remove cell made
                      p             ~~ remove last cell
                       S            ~~ subtract the two values
                        R5_         ~~ goes right and sets sqrt(5)
                           V        ~~ divides the prev. two cells
                            j       ~~ inserts new cell
                             26@    ~~ puts 26 into the register
                                p   ~~ removes cell
                                 Wo ~~ converts the current to base 26 and outputs as number

Conor O'Brien

Posted 2015-10-24T18:38:03.347

Reputation: 36 228

Oh, and, as an aside, the W command interprets base 26 as the lowercase alphabet, base 52 as the upper and lowercase alphabet, and base 64 is essentially JavaScripts btoa function. – Conor O'Brien – 2015-10-25T16:24:26.837

4

Pyth, 17 bytes

s@LGjhu,eGsGQU2lG

Try it online.

orlp

Posted 2015-10-24T18:38:03.347

Reputation: 37 067

Awesome, I can verify that this works – Hack-R – 2015-10-26T17:51:33.730

3

Minkolang 0.9, 40 bytes

10n[0c+r]$x'26'r(d0c%1G0c:d$)xrx("a"+O).

Try it here.

Explanation

10n[0c+r]                                   Calculates f(n) where n is taken from input
         $x'26'r                            Dumps the addend I don't need and pushes a 26
                (d0c%1G0c:d$)               Base-encodes f(n) in base 26
                             xrx            Dumps the 0, reverses, dumps the 26
                                ("a"+O).    Outputs the letters

El'endia Starman

Posted 2015-10-24T18:38:03.347

Reputation: 14 504

Really nice! Works great for huge inputs! – Filip Haglund – 2015-10-24T19:37:57.843

3

Python 2.7, 82 bytes

a=0
b=1
s=''
exec"a,b=b,a+b;"*input()
while a:s=chr(a%26+97)+s;a/=26
print s or'a'

xsot

Posted 2015-10-24T18:38:03.347

Reputation: 5 069

1

Haskell, 114 chars.

It is unexpectedly long. Any help welcome. Previously found a bug for fib(0)

f=scanl(+)0$1:f
k 0=[]
k x=k(x`div`26)++[toEnum$97+x`mod`26]
l 0=0
l x=k x
main=interact$l.(f!!).read.head.lines

f is infinite list of fibonacci. toEnum is same with chr, except that former doesn't need to import Data.Char.

Akangka

Posted 2015-10-24T18:38:03.347

Reputation: 1 859

0

Ruby, 67 bytes

a,b=0,1
gets.to_i.times{a,b=b,a+b}
puts a.to_s(26).tr"0-9a-p","a-z"

daniero

Posted 2015-10-24T18:38:03.347

Reputation: 17 193

0

Matlab, 133 bytes

n=input('');if n<2,y=n;else
f=0;g=1;for k=2:n
h=f+g;f=g;g=h;end
y=fix(mod(g./26.^(fix(log(g)/log(26)):-1:0),26));end
disp(char(y+97))

Luis Mendo

Posted 2015-10-24T18:38:03.347

Reputation: 87 464

0

Ruby, 125 bytes

Not gonna win any time soon, but it was fun & my first code golf :')

def f(n);n<3?(n>0?1:0):f(n-1)+f(n-2);end
def a(s);s.bytes.map{|n|n<58?n+49:n+10}.pack("C*");end
puts a(f(gets.to_i).to_s(26))

First line is a function to compute fibonacci, second converts from Ruby's built-in base 26 encoding (0-9 then a-p) into a-z encoding, third gets a line from STDIN and runs it through both.

gntskn

Posted 2015-10-24T18:38:03.347

Reputation: 243

-1

Python 2, 112 bytes

n=input()
if n<1:print'a';exit()
a,f=0,1
while n>1:a,f,n=f,a+f,n-1
r=''
while f:r,f=chr(f%26+97)+r,f//26
print r

Try it online.

Mego

Posted 2015-10-24T18:38:03.347

Reputation: 32 998

This seems to be slightly off for large values; first overflow at 71. Here's for fib(1337): https://www.diffchecker.com/bwjpg7bb where the correct answer ends with "win".

– Filip Haglund – 2015-10-24T19:24:14.683

4@FilipHaglund Probably floating point nonsense. I'll revert to the iterative formula. – Mego – 2015-10-24T19:25:40.563