28
\$\begingroup\$

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

Next, add the two lines.

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.

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

39 Answers 39

0
\$\begingroup\$

Pyth - 14 bytes

(yeah, another pyth answer)

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

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αio`na

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╔Ri`na

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

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

R, 82 bytes

This can surely be golfed.

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

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

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)
  {
    r.Add(string.Concat(r[i-3], r[i-2]));
    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
\$\endgroup\$
  • 1
    \$\begingroup\$ This just returns the final line, you should print/output the (N-1) lines leading up to it as well. \$\endgroup\$ – Harald Korneliussen Nov 7 '16 at 13:45
  • \$\begingroup\$ @HaraldKorneliussen I missed that in the post, corrected now, thanks :) \$\endgroup\$ – Pete Arden Nov 7 '16 at 18:35
0
\$\begingroup\$

PHP, 61 57 bytes

<?=$c=a;for($b=b;--$argv[1];$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[1];[$a,$b]=[$b,$a.$b])echo"
$b";

If a trailing new line is acceptable then:

for($b=b;$argv[1]--;$a=$b,$b=$c.$b)echo$c=$a?:a,"
";

is only 52 bytes.

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

\$\endgroup\$
  • \$\begingroup\$ You do not have to use open tags unless there's a full program requirement :) \$\endgroup\$ – chocochaos Nov 4 '16 at 15:45
  • \$\begingroup\$ @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. \$\endgroup\$ – user59178 Nov 4 '16 at 17:20
  • \$\begingroup\$ save 4 bytes on your 1st version <?=$c=a;for($b=b;--$argv[1];$b=$c.$b,$c=$a)echo"\n",$a=$b; and 4 bytes on your 3rd version for($b=b;$argv[1]--;$a=$b,$b=$c.$b)echo$c=$a?:a,"\n"; \$\endgroup\$ – Titus Nov 11 '16 at 15:32
  • \$\begingroup\$ @Titus thanks, that's really clever, I'll pay attention to that in the future! \$\endgroup\$ – user59178 Nov 11 '16 at 17:50
0
\$\begingroup\$

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 :)

\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! This answer doesn't look like it takes input -- instead it just always prints out the first 10 results. \$\endgroup\$ – AdmBorkBork Nov 11 '16 at 13:49
  • \$\begingroup\$ Hi TimmyD, thats right, but you can alter the amount of lines here :) 0..N \$\endgroup\$ – Cellcore Nov 11 '16 at 16:12
  • \$\begingroup\$ 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 ;) \$\endgroup\$ – Cellcore Nov 11 '16 at 16:13
  • \$\begingroup\$ Yes, you'll need to take input via a standard input method. For this, something like 0..$args[0] and having the input be a command-line parameter would probably be shortest. \$\endgroup\$ – AdmBorkBork Nov 11 '16 at 16:17
  • \$\begingroup\$ Unfortunately i found only (read-host) then it would be like: \$\endgroup\$ – Cellcore Nov 11 '16 at 16:50
-1
\$\begingroup\$

Javascript (ES6), 35 bytes

f=v=>v&&v-1?f(v-2)+f(v-1):v?'b':'a'
\$\endgroup\$
  • \$\begingroup\$ Can you make use of 'ab'[v]? \$\endgroup\$ – Neil Nov 4 '16 at 15:58

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.