34
\$\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\$
6
  • 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
  • 3
    \$\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

16 Answers 16

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\$
3
  • 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\$
3
  • \$\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\$
1
  • \$\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\$
1
  • \$\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\$
1
  • \$\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\$
2
\$\begingroup\$

Whispers v3, 111 bytes

> Input
> fₙ
>> ≺1
>> 2ᶠ1
>> 4ⁿ3
> 26
>> 5⊥6
> 97
>> 8+R
>> Each 9 7
>> 'R
>> Each 11 10
>> Output 12

Try it on repl.it!

Needs to be run as python3 whispers\ v3.py fibabc.wisp < input.txt 2> /dev/null with input given on the first line of input.txt.

I think the first and last lines can be removed as per the consensus here, but I'm not sure.

8 bytes shorter than hardcoding the alphabet: Try it on repl.it!

\$\endgroup\$
1
  • \$\begingroup\$ You can say that line 12 is your main function and avoid the need for line 13, but you can't simply get rid of the Input line. You would need to restructure your program to use R/L instead of Input to make it into a function taking input via arguments (but I think doing that would make the code longer). I will try to clarify this in the meta post \$\endgroup\$ – Leo Feb 15 at 0:11
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\$
1
\$\begingroup\$

Jelly, 8 bytes

ÆḞṃØaṙ1¤

Try it online!

How it works

ÆḞṃØaṙ1¤ - Main link. Takes an integer N on the left
ÆḞ       - Take the Nth Fibonacci number
       ¤ - Group the previous links into a nilad:
   Øa    -   "abc...xyz"
     ṙ1  -   Rotate once; "bcd...yza"
  ṃ      - Base decompress; Convert to base 26 then index into the string
\$\endgroup\$
1
\$\begingroup\$

JavaScript (V8), 94 bytes

x=>(f=n=>n>1?f(n-1)+f(n-2):1)(x)[S="toString"](26).replace(/./g,l=>(parseInt(l,26)+10)[S](36))

Try it online!

Uses recursion to find factorial, and some toString, replace, and parseInt magic to convert to letters.

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

Zsh, 68 bytes

<<<A;{for ((a=1;b+=a;a+=b))<<<$[[##26]a]<<<$[[##26]b]}|tr 0-9A-P A-Z

Try it online!

Pure fibonacci code stolen from here.

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

05AB1E, 6 bytes

If a list of characters is fine as output, the J can be removed.

ÅfAÅвJ

Try it online!

Åf      # get the nth Fibonacci number
  A     # push the lowercase alphabet
   Åв   # convert the Fibonacci number to this custom base
     J  # join the list of digits into a string
\$\endgroup\$
1
  • \$\begingroup\$ Why does someone always find my ideas first... :P \$\endgroup\$ – Makonede Feb 16 at 21:48
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\$

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.