| Bytes | Lang | Time | Link |
|---|---|---|---|
| 047 | AWK | 250829T142808Z | xrs |
| 011 | Thunno 2 | 230729T172804Z | The Thon |
| 012 | Japt R | 230206T231730Z | Shaggy |
| nan | 230206T202558Z | The Thon | |
| 056 | Python 2 | 230206T191058Z | Alan Bag |
| 008 | SOML | 161106T094006Z | dzaima |
| 062 | PHP | 161104T093234Z | Crypto |
| 051 | Powershell 1.0+ | 161111T122952Z | Cellcore |
| 057 | PHP | 161104T093748Z | user5917 |
| 142 | C# | 161107T125751Z | Pete Ard |
| 043 | GNU sed | 161107T152705Z | Riley |
| 156 | C | 161104T155554Z | Phil |
| nan | Perl | 161104T135754Z | Gabriel |
| 015 | 05AB1E | 161106T181728Z | user6028 |
| 082 | R | 161106T152038Z | Billywob |
| 035 | Perl | 161106T124812Z | Ton Hosp |
| 040 | Java 8 | 161105T205235Z | BananyaD |
| 063 | Stack my Golf | 161106T022755Z | Quincy B |
| 048 | Perl | 161104T084927Z | Dom Hast |
| 016 | Actually | 161104T220953Z | Sherlock |
| 033 | Retina | 161104T085857Z | Dom Hast |
| 014 | Pyth | 161104T220615Z | Yotam Sa |
| 046 | Ruby 1.9+ | 161104T210111Z | shabunc |
| 042 | JavaScript ES6 | 161103T232437Z | ETHprodu |
| 098 | C++11 | 161104T142458Z | Karl Nap |
| 035 | Javascript ES6 | 161104T151130Z | Marcus D |
| 053 | PHP | 161104T150454Z | chococha |
| 055 | Python 2 | 161103T215327Z | miles |
| 017 | CJam | 161103T223535Z | Setop |
| 041 | Python 2 | 161103T232821Z | ETHprodu |
| 079 | Groovy | 161104T132942Z | Magic Oc |
| 011 | 05AB1E | 161103T214207Z | Adnan |
| 025 | Emacs | 161104T124506Z | YSC |
| 049 | Mathematica | 161104T081108Z | Greg Mar |
| 032 | Haskell | 161103T211442Z | nimi |
| 069 | Java 7 | 161104T042857Z | Numberkn |
| 010 | Jelly | 161104T010044Z | Dennis |
| 076 | Swift 3 | 161104T025208Z | Alexande |
| 030 | APL | 161104T004010Z | Adalynn |
| 093 | Batch | 161103T225334Z | Neil |
| 015 | Pyth | 161103T234212Z | Maltysen |
| 014 | MATL | 161103T222005Z | Luis Men |
| 017 | Pyth | 161103T220805Z | TheBikin |
| 018 | V | 161103T211857Z | DJMcMayh |
Thunno 2 --, 11 bytes
'aß'bß${Ɓ+ß
Given n, prints the first n terms (as the challenge asks).
Thunno 2, 7 bytes
`abdµµ+
Prints the infinite sequence (probably not allowed).
Explanation
'aß'bß${Ɓ+ß # Implicit input
'aß '# Print "a" without popping
'bß '# Print "b" without popping
${ # Repeat (input - 2) times:
Ɓ # Without popping,
+ # concatenate the last two items
ß # Print without popping
`abdµµ+ # Full program
`abd # Starting from ["a", "b"],
µµ # construct an infinite sequence
+ # by concatenating the last two terms
# Implicit output
Python 2, 56 bytes
a,b="ab"
for i in range(input()):
print(a+b)
a,b=b,a+b
Python 2, 44 bytes
a,b="ab"
exec"print(a+b);a,b=b,a+b;"*input()
SOML, 8 bytes
a b.{;t⁴+
explanation:
a b.{;t⁴+ stack on 1st cycle
a push "a" ["a"]
b push "b" ["a","b"]
.{ repeat input times ["a","b"]
; swap the two top things on the stack ["b","a"]
t output the top thing on the stack ["b","a"]
⁴ copy the 2nd from top thing from stack ["b","a","b"]
+ join them together ["b","ab"]
This language is still in development and I did add a couple new functions while writing this.
Also, 1st post on PPCG!
PHP, 63 62 bytes
Recursive version:
function f($n,$a=a,$b=b){return$n--?"$a
".f($n,$b,$a.$b):'';}
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 :)
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.
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
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).
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, 156 bytes (without indent)
void f(int n)
{
char u[999]="a",v[999]="b",*a=u,*b=a+1,*c=v,*d=c+1,*e,i;
for(i=0;i<n;++i)
{
printf("%s\n",a);
for(e=c;*b++=*e++;);
e=a;a=c;c=e;e=b+1;b=d;d=e;
}
}
Two buffers (u & v) store the last two lines. The newest line (tracked with two pointers: start=c, end=d) is appended to the oldest one (start=a, end=b). Swap (a,b) and (c,d), and loop. Pay attention to buffer size before requesting too much lines. Not so short (as expected of a low-level language), but was fun to code.
Perl, 45 +1 = 46 bytes
+1 byte for -n flag
$a=a,$b=b;say($a),($a,$b)=($b,$a.$b)for 1..$_
Slight improvement over the existing 49-byte solution, but developed separately. The parentheses for say($a) are necessary because otherwise, it interprets $a,($a,$b)=($b,$a.$b) as the argument of say which outputs more junk than we need.
Perl, 42 bytes
$b=<>;$_=a;say,y/ab/bc/,s/c/ab/g while$b--
A separate approach from the above solution:
$b=<>; #Read the input into $b
$_=a; #Create the initial string 'a' stored in $_
say #Print $_ on a new line
y/ab/bc/ #Perform a transliteration on $_ as follows:
#Replace 'a' with 'b' and 'b' with 'c' everywhere in $_
s/c/ab/g #Perform a replacement on $_ as follows:
#Replace 'c' with 'ab' everywhere in $_
, , while$b-- #Perform the operations separated by commas
#iteratively as long as $b-- remains truthy
I'm not yet convinced I can't combine the transliteration and replacement into a single, shorter operation. If I find one, I'll post it.
05AB1E, 15 bytes
'a'bVUFX,XYUYJV
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")
Perl, 36 35 bytes
Includes +3 for -n
Give count on STDIN
perl -M5.010 fibo.pl <<< 5
fibo.pl
#!/usr/bin/perl -n
$_=$'.s/1//.$_,say$`for(a1.b)x$_
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);
}
}
Stack my Golf, 63 bytes
Get my language here: https://github.com/cheertarts/Stack-My-Golf.
($1=(_(a))($2=(_(b))($2-f:1-f,)?)?)f\($1=(f)($1-p(\n),:f,)?)p\p
There is probably a shorter way but this is the most obvious.
Perl, 48 bytes
47 bytes code + 1 for -n.
Simple approach. Try using an array slice originally $a[@a]="@a[-2,-1]" but that necessitates $"="" or similar :(. Save 1 byte thanks to @Dada!
@;=(a,b);$;[@;]=$;[-2].$;[-1]for 3..$_;say for@
Usage
perl -nE '@;=(a,b);$;[@;]=$;[-2].$;[-1]for 3..$_;say for@' <<< 5
a
b
ab
bab
abbab
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.
Retina, 33 bytes
.+
$*
^11
a¶b
+`¶(.+?)1
¶$1¶$%`$1
Saved 10 (!) bytes thanks to @MartinEnder!
Explanation
Converts the input to unary, subtracts 2 and adds the a and the b, then recursively replaces the remaining 1s with the concatenation of the two previous strings.
Pyth - 14 bytes
(yeah, another pyth answer)
A<G2VQG=H+~GHH
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
Ruby (1.9+) 46 bytes
a,b=?a,?b;ARGV[0].to_i.times{puts a;a,b=b,a+b}
JavaScript (ES6), 43 42 bytes
Saved a byte thanks to @Arnauld
f=(n,a="a",b="b")=>n&&f(n-!alert(a),b,a+b)
C++11, 89 98 bytes
+7 bytes for all lines, not only the last one. +2 bytes more for N being the number of lines printed, not some 0-based stuff.
#include<string>
using S=std::string;S f(int n,S a="a",S b="b"){return n-1?a+"\n"+f(n-1,b,a+b):a;}
Usage:
f(5)
Javascript (ES6), 35 bytes
f=v=>v&&v-1?f(v-2)+f(v-1):v?'b':'a'
PHP, 53 bytes
for($a=b,$b=a;$argv[1]--;$a=($_=$b).$b=$a)echo$b.'
';
Python 2, 55 bytes
def f(n):m='a','b';exec'print m[-2];m+=m[-2]+m[-1],;'*n
CJam, 19 17 bytes
'a'b{_@_n\+}ri*;;
explanation
"a": Push character literal "a" onto the stack.
"b": Push character literal "b" onto the stack.
{_@_p\+}
{: Block begin.
_: duplicate top element on the stack
@: rotate top 3 elements on the stack
_: duplicate top element on the stack
n: print string representation
\: swap top 2 elements on the stack
+: add, concat
}: Block end.
r: read token (whitespace-separated)
i: convert to integer
*: multiply, join, repeat, fold (reduce)
;: pop and discard
;: pop and discard
Python 2, 41 bytes
Saved 3 bytes thanks to @xnor
a,b="ab";exec"print a;a,b=b,a+b;"*input()
Simply follows the recursive definition.
Groovy, 79 Bytes
{println("a\nb");x=['a','b'];(it-2).times{println(y=x.sum());x[0]=x[1];x[1]=y}}
05AB1E, 12 11 bytes
Thanks to Emigna for saving a byte!
'a='b¹GD=Š«
Uses the CP-1252 encoding. Try it online!
Emacs, 26, 25-ish keystrokes
Program
#n to be read as key with digit(s) n:
ARETBRETF3UPUPC-SPACEC-EM-WDOWNDOWNC-Y UPC-AC-SPACEC-EM-WDOWNC-EC-YRETF4C-#(n-2)F4
Explanation
command(s) explanation buffer reads (| = cursor aka point)
-----------------------------------------------------------------------------------------------
A<RET> B<RET> input starting points "a\nb\n|"
<F3> start new macro "a\nb\n|"
<UP><UP> move point two lines up "|a\nb\n"
C-<SPACE> C-E M-W copy line at point "a|\nb\n"
<DOWN><DOWN> move point two lines down "a\nb\n|"
C-Y yank (paste) "a\nb\na|"
<UP> move point one line up "a\nb|\na"
C-A C-<SPACE> C-E M-W copy line at point "a\nb|\na"
<DOWN> move point one line down "a\nb|\na|"
C-E C-Y <RET> yank (paste) and add new line "a\nb|\nab\n|"
<F4> stop macro recording "a\nb|\nab\n|"
C-#(n-3) <F4> apply macro n-3 times "a\nb|\nab\nbab\nabbab\n|"
With n=10
a
b
ab
bab
abbab
bababbab
abbabbababbab
bababbababbabbababbab
abbabbababbabbababbababbabbababbab
bababbababbabbababbababbabbababbabbababbababbabbababbab
Mathematica, 49 bytes
f@1="a";f@2="b";f@n_:=f[n-2]<>f[n-1];g=f~Array~#&
Defines a function g taking the single numerical input; returns a list of strings. Straightforward recursive implementation, using the string-joining operator <>.
Mathematica, 56 bytes
NestList[#~StringReplace~{"a"->"b","b"->"ab"}&,"a",#-1]&
Unnamed function, same input/output format as above. This solution uses an alternate way to generate the strings: each string in the list is the result of simultaneously replacing, in the previous string, all occurrences of "a" with "b" and all occurrences of "b" with "ab".
Haskell, 29 35 32 bytes
a%b=a:b%(a++b)
(`take`("a"%"b"))
Simple recursion.
For reference: the old version (an adaption of this answer),
concatenated the strings in the wrong order, so I had to add a flip(...) which made it too long (35 bytes).
f="a":scanl(flip(++))"b"f
(`take`f)
Java 7, 69 bytes
String c(int n,String a,String b){return n--<1?"":a+"\n"+c(n,b,a+b);}
ungolfed
class fibb {
public static void main(String[] args) {
System.out.println( c( 7, "a" , "b" ) );
}
static String c(int n,String a,String b) {
return n-- < 1 ? "" : a + "\n" + c(n,b,a + b);
}
}
Jelly, 11 10 bytes
”a”bṄ;¥@¡f
How it works
”a”bṄ;¥@¡f Main link. Argument: n
”a Set the return value to 'a'.
”b ¡ Call the link to the left n times, with left argument 'b' and right
argument 'a'. After each call, the right argument is replaced with the
left one, and the left argument with the return value. The final
return value is yielded by the quicklink.
¥ Combine the two atoms to the left into a dyadic chain.
Ṅ Print the left argument of the chain, followed by a linefeed.
; Concatenate the left and right argument of the chain.
@ Call the chain with reversed argument order.
f Filter the result by presence in n. This yields an empty string
and thus suppresses the implicit output.
Swift 3, 76 Bytes
func f(_ n:Int,_ x:String="a",_ y:String="b"){if n>1{print(x);f(n-1,y,x+y)}}
APL, 30 bytes.
⎕IO must be 1.
{⎕←⍵}¨{⍵∊⍳2:⍵⌷'ab'⋄∊∇¨⍵-⌽⍳2}¨⍳
Batch, 102 93 bytes
@set a=a
@set b=b
@for /l %%i in (2,1,%1)do @call:l
:l
@echo %a%
@set a=%b%&set b=%a%%b%
Fortunately variables are expanded for every line before assignments take effect, so I can set both a and b using their old values without needing a temporary. Edit: Saved 9 bytes thanks to @nephi12.
MATL, 14 bytes
97c98ci2-:"yyh
97c % Push 'a'
98c % Push 'b'
i2- % Input number. Subtract 2
:" % Repeat that many times
yy % Duplicate the top two elements
h % Concatenate them
Pyth, 17 bytes
J,\a\bjP.U=+Js>2J
A program that takes input of an integer and prints the result.
How it works
J,\a\bjP.U=+Js>2J Program. Input: Q
,\a\b Yield the two element list ['a', 'b']
J Assign to J
.U Reduce over [0, 1, 2, 3, ..., Q] (implicit input):
=J+ J = J +
>2J the last two elements of J
s concatenated
P All of that except the last element
j Join on newlines
Implicitly print
V, 18 bytes
ia
bkÀñyjGpgJkñdj
Or, the more readable version:
ia
b<esc>kÀñyjGpgJkñdj
Explanation:
ia
b<esc> " Insert the starting text and escape back to normal mode
k " Move up a line
Àñ ñ " Arg1 times:
yj " Yank the current line and the line below
G " Move to the end of the buffer
p " Paste what we just yanked
gJ " Join these two lines
k " Move up one line
dj " Delete the last two lines