# Print a letter Fibonacci

Given N (2 <= N), print N lines of the letter Fibonacci series like this (i.e. N = 5) First, start with a and b:

a
b


a
b
ab


Keep adding the last two lines.

a
b
ab
bab


Keep going...

a
b
ab
bab
abbab


And we're done.

Remmeber, this is , so the code with the fewest bytes wins.

• Related – AdmBorkBork Nov 3 '16 at 21:01
• Can it be a function which returns a list of terms up to N? – FlipTack Nov 3 '16 at 21:21
• Do we have to print the result or can we return a list of strings from a function? – nimi Nov 3 '16 at 21:21
• Wait, so it doesn't have to work for n=1? – Socratic Phoenix Nov 3 '16 at 22:39
• Also, can we use 0-based indexing? – Socratic Phoenix Nov 3 '16 at 22:45

# Pyth - 14 bytes

A<G2VQG=H+~GHH


Try it online

Explanation

A<G2              - take "ab" from the var G, dump "a" into G and "b" into H.
VQ            - for N in range(Q)
G           - print G
~GH     - like =GH but returns the previous G
+   H    - concatenate the old G with the current H
=H         - put that value back into H


# Actually, 16 bytes

There might be a shorter way to duplicate the top two elements of the stack in Actually, but I'm not sure what that way is. Golfing suggestions welcome. Try it online!

'a'b,¬2╟2αiona


Ungolfing

'a'b     Push "a" then "b" to the stack.
,        Explicitly take input n.
¬        Push n-2.
...n   Run the following function n-2 times.
2╟2α     Push a list that repeats the top two elements of the stack twice.
i        Flatten that list to the stack. Stack: b, a, b, a, prev_iterations
o        Append b to the end of a. Stack: ab, b, a, prev_iterations
a        Reverse the stack so that the strings will print in the correct order.
Implicit print all of the elements in the stack.


A different approach

This is also 16 bytes. Try it online!

'a'b,¬│ok╔Rina


Ungolfing

The main difference is the function in the middle, so I'll only ungolf that here.

...n   Run the following function n-2 times.
│        Duplicate the stack.
o        Append the last item to the end of the second-to-last item.
k╔       Uniquify the stack by wrapping the stack in a list and then calling uniquify().
Ri       Reverse that list before returning everything to the stack
so that the last items are at TOS again.


# Java 8 40 bytes

Same answer as @Numberknot only written as a lambda in Java 8 which makes it smaller.

c=(n,a,b)->n--<1?"":a+"\n"+c.f(n,b,a+b);


Ungolfed version:

interface Function
{
String f(int n, String a, String b);
}

public class Main
{
static Function c; //c needs to be a member or static variable in order
//to reference it recursively inside the lambda

public static void main(String[] args)
{
c=(n,a,b)->n--<1?"":a+"\n"+c.f(n,b,a+b);
}
}


# R, 82 bytes

This can surely be golfed.

x=c("b","a");for(i in 2:scan()-2)x=c(paste0(x,x),x);cat(rev(x[-1]),sep="\n")


# GNU sed, 43 Bytes

Includes +2 for -rn.

s/1/a\nb\n/
:
P
s/(.*)\n(.*\n)./\2\1\2/
t


Takes input in unary (see this consensus).

Try it Online!

Explanation:

s/1/a\nb\n/              # prepend an 'a' and a 'b' on their own lines and remove a '1'
:                        # nameless label
P                        # print everything before the first newline
s/(.*)\n(.*\n)./\2\1\2/  # replace the first two lines and the next character
# (one of the '1's) with the second line then the first line
# concatenated with the second
# E.g.
#     a           b
#     b      ->   ab
#     11111       1111
t                        # loop to the label if there was a '1' left (i.e. there was
# a substitution)


# C#, 142 Bytes

Edit: only had it printing the last line out... V2:

Golfed:

string L(int n){var r=new List<string>(){"a","b"};int i=3;while(i<=n){r.Add(string.Concat(r[i-3],r[i-2]));i++;}return string.Join("\r\n", r);}


Ungolfed:

public string L(int n)
{
var r = new List<string>() { "a", "b" };

int i = 3;

while (i <= n)
{
i++;
}

return string.Join("\r\n", r);
}


Test:

var printALetterFibonacci = new PrintALetterFibonacci();
Console.WriteLine(printALetterFibonacci.L(5));

a
b
ab
bab
abbab

var printALetterFibonacci = new PrintALetterFibonacci();
Console.WriteLine(printALetterFibonacci.L(7));

a
b
ab
bab
abbab
bababbab
abbabbababbab

• This just returns the final line, you should print/output the (N-1) lines leading up to it as well. – user61264 Nov 7 '16 at 13:45
• @HaraldKorneliussen I missed that in the post, corrected now, thanks :) – Pete Arden Nov 7 '16 at 18:35

# PHP, 61 57 bytes

<?=$c=a;for($b=b;--$argv;$b=$c.$b,$c=$a)echo"
",$a=$b;


come php 7.1 you can save 3 bytes by changing to: an "old" version that I wish was short:

<?=$a=a;for($b=b;--$argv;[$a,$b]=[$b,$a.$b])echo"
$b";  If a trailing new line is acceptable then: for($b=b;$argv--;$a=$b,$b=$c.$b)echo$c=$a?:a,"
";


is only 52 bytes.

edit: 4 bytes saved (on both usable versions) thanks to Titus.

• You do not have to use open tags unless there's a full program requirement :) – chocochaos Nov 4 '16 at 15:45
• @chocochaos I know, but i'm using it to print the first a and <?= is shorter than echo. I can do better if a trailing new line is fine and will edit the answer to that effect. – user59178 Nov 4 '16 at 17:20
• save 4 bytes on your 1st version <?=$c=a;for($b=b;--$argv;$b=$c.$b,$c=$a)echo"\n",$a=$b; and 4 bytes on your 3rd version for($b=b;$argv--;$a=$b,$b=$c.$b)echo$c=$a?:a,"\n"; – Titus Nov 11 '16 at 15:32 • @Titus thanks, that's really clever, I'll pay attention to that in the future! – user59178 Nov 11 '16 at 17:50 # Powershell 1.0+, 41 51 :( Bytes $x,$y='a','b';0..(read-host)|%{$x;$x,$y=$y,($x+$y)}  4 *nix goto https://github.com/PowerShell/PowerShell Corrected for user input :) • Welcome to PPCG! This answer doesn't look like it takes input -- instead it just always prints out the first 10 results. – AdmBorkBork Nov 11 '16 at 13:49 • Hi TimmyD, thats right, but you can alter the amount of lines here :) 0..N – Cellcore Nov 11 '16 at 16:12 • I did not really figure out if it is needed to ask the user for Input in the question, but I can extend it for user Input ;) – Cellcore Nov 11 '16 at 16:13 • Yes, you'll need to take input via a standard input method. For this, something like 0..$args and having the input be a command-line parameter would probably be shortest. – AdmBorkBork Nov 11 '16 at 16:17
• Unfortunately i found only (read-host) then it would be like: – Cellcore Nov 11 '16 at 16:50

# Javascript (ES6), 35 bytes

f=v=>v&&v-1?f(v-2)+f(v-1):v?'b':'a'

• Can you make use of 'ab'[v]? – Neil Nov 4 '16 at 15:58