31
\$\begingroup\$

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!

\$\endgroup\$
  • 3
    \$\begingroup\$ @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. \$\endgroup\$ – Martin Ender Oct 24 '15 at 23:07
  • 2
    \$\begingroup\$ It's still base26. What characters you use is arbitrary, and here we use a-z (in alphabetical order). \$\endgroup\$ – Filip Haglund Oct 24 '15 at 23:08
  • \$\begingroup\$ Right, it's a different base-26 notation from the conventional, but it's still a base-26 notation. \$\endgroup\$ – Lightness Races in Orbit Oct 25 '15 at 2:05
  • 5
    \$\begingroup\$ Why use them nasty numbers for the input? \$\endgroup\$ – ugoren Oct 25 '15 at 8:12
  • \$\begingroup\$ Name suggestion: Fibona-b-c \$\endgroup\$ – Matthew Roh Mar 27 '17 at 9:12

12 Answers 12

12
\$\begingroup\$

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

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

\$\endgroup\$
  • 1
    \$\begingroup\$ Nice 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. ;) \$\endgroup\$ – ETHproductions Oct 24 '15 at 21:26
  • \$\begingroup\$ 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.) \$\endgroup\$ – ETHproductions Oct 25 '15 at 1:38
  • \$\begingroup\$ @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. \$\endgroup\$ – Downgoat Oct 25 '15 at 15:21
6
\$\begingroup\$

Mathematica, 67 61 bytes

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

Calculates f(1000000) in about 51 milliseconds.

\$\endgroup\$
  • \$\begingroup\$ Ah, didn't see that there was a Mathematica answer already! Mine used IntegerString to format the digits: IntegerString[Fibonacci@#~IntegerDigits~26+10,36]<>""& \$\endgroup\$ – user46060 Oct 25 '15 at 0:17
  • \$\begingroup\$ 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. \$\endgroup\$ – user46060 Oct 25 '15 at 0:26
  • \$\begingroup\$ @user5254 I first used FromLetterNumber before seeing that it internally used Alphabet with Part and used that, except with a list of indices. \$\endgroup\$ – LegionMammal978 Oct 25 '15 at 0:30
5
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Conor O'Brien Oct 25 '15 at 16:24
4
\$\begingroup\$

Pyth, 17 bytes

s@LGjhu,eGsGQU2lG

Try it online.

\$\endgroup\$
  • \$\begingroup\$ Awesome, I can verify that this works \$\endgroup\$ – Hack-R Oct 26 '15 at 17:51
3
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ Really nice! Works great for huge inputs! \$\endgroup\$ – Filip Haglund Oct 24 '15 at 19:37
3
\$\begingroup\$

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'
\$\endgroup\$
1
\$\begingroup\$

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.

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

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"
\$\endgroup\$
0
\$\begingroup\$

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))
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
-1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ This seems to be slightly off for large values; first overflow at 71. Here's for fib(1337): diffchecker.com/bwjpg7bb where the correct answer ends with "win". \$\endgroup\$ – Filip Haglund Oct 24 '15 at 19:24
  • 4
    \$\begingroup\$ @FilipHaglund Probably floating point nonsense. I'll revert to the iterative formula. \$\endgroup\$ – Mego Oct 24 '15 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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