Write a program fragment so that, when repeated N times it prints the Nth Fibonacci number. For example, if your program is print(x)
then:
print(x)
should print1
print(x)print(x)
should print1
print(x)print(x)print(x)
should print2
print(x)print(x)print(x)print(x)
should print3
print(x)print(x)print(x)print(x)print(x)
should print5
- etc.
First 2 items are 1 then each consecutive item in the sequence is the sum of the previous two.
You may include a non-repeated header and footer, but a smaller header and footer will always beat an answer with a larger sum of header+footer. For example, a 1000 byte answer with no header wins from a 20 byte answer with a 1 byte footer. However, a 0 byte header 20 byte body will win from both.
With header length being equal, smallest number of bytes, in each language, wins.
Tips
Output in unary
Use shell escape codes like \r
to erase the previous print.
Use your languages implicit output feature
Example Submission
Python, 0 header/footer, 97 body
if"k"not in globals():k=["","x"];print("x",end="") else:k.append(k[-1]+k[-2]);print(k[-3],end="")
Output in unary
Comments are entirely valid, but solutions that don't use comments are extra impressive.
44 Answers 44
R, 12 bytes, no header/footer
+(F=+T)->T;F
Right-hand assignment ->
is useful for suppressing output. Paging Robin Ryder and his various solutions involving ->
.
Why this works
F
and T
are automatically assigned as variables for the constants FALSE
and TRUE
, and happily are coerced to 0
and 1
in arithmetic expressions.
The shortest R answer to the canonical Fibonacci challenge uses F<-T+(T=F)
as the body of a loop, using the fact that assignment returns the assigned value (right-hand side for <-
and =
) invisibly, so the updates to the values happen in the right order (and are made visible by show
). In that challenge, F
holds the leading value \$F_n\$ and T
holds \$F_{n-1}\$.
A few adjustments are required here:
F
now holds the trailing value \$F_{n}\$, andT
is updated on each loop asF+(F=T)->T
to be \$F_{n+1}\$, on every iteration.- We use
F=+T
so that the first copy prints out1
instead ofTRUE
. - Leading
+
and the infrequently-used right-hand assignment->
operator to satisfy the concatenation requirement. - Trailing
;F
to use implicit output to print out the required value.
Piet + ascii-piet, 19 bytes (×ばつ4=48 codels)
_TLDJdAaAQIbcr_cc _
A1 nop
B1-F1 1 1 ! roll `1 0 roll`; an overall no-op if there were some values
on the stack, but leaves 1 0 if empty
(therefore pushes 1 0 on the first iteration only)
F1-K1 dup 3 1 roll + apply Fibonacci to the top two values
[a b] -> [b a+b]
When the program is repeated, the grid is concatenated vertically, putting A1's white cell on L1 and so on. At K1, two things can happen:
- If there are more repetitions below, the IP keeps going down, running more iterations of Fibonacci.
- Otherwise, it heads east, prints the top value as integer (J2-J3) and halts (J4-K4).
Python, 34 bytes
a='x';b=''
print(end=a);a,b=b,a+b#
Outputs in unary.
How?
Streamlined version of OP's example code
T-SQL (削除) 171 (削除ここまで) (削除) 160 (削除ここまで) 159 bytes (no header or footer)
-1 byte thanks to @MickyT
/*
WHERE 1=0--*//*
--*/Select 0 x,1 y into T;EXEC('create trigger X on T for delete as if @@rowcount>0select x from DELETED')
UPDATE T SET x=y,y=x+y;DELETE T--
The trick is using comments to have the initial select and trigger creation on only the first repeat, and the WHERE
clause preventing the deletion of the fibbonacci record on all except the final repeat. Selecting from DELETED
then outputs it.
-
\$\begingroup\$ Welcome to Code Golf, and nice answer! \$\endgroup\$2023年08月23日 23:14:54 +00:00Commented Aug 23, 2023 at 23:14
-
\$\begingroup\$ Thanks LizWeir for the sql comment inspiration! \$\endgroup\$Katie Gardner– Katie Gardner2023年08月24日 06:49:46 +00:00Commented Aug 24, 2023 at 6:49
-
2\$\begingroup\$ Nice to see an SQL answer, you can save a btye removing the space between the 0 and select in the trigger '@@rowcount>0select' \$\endgroup\$MickyT– MickyT2023年08月24日 22:22:50 +00:00Commented Aug 24, 2023 at 22:22
Itr, (削除) 5 (削除ここまで) 4 bytes (no header or footer)
â+1w
Nx
(uses F
operator to repeat sequence input number of times)
Explanation
â+1w ; implicitly start with 0,0 on stack
â ; push top value below second value a,b -> b,a,b
+ ; add top two values -> b,a+b
1w ; maximum of 1 and top stack value
; implicit output
Piet + ascii-piet, 53 bytes (×ばつ7=105 codels), no header/footer
ttlddtN ub?fnNssSkKCVrTtTLcfkdtlElt?l??T lL sdD S??
Program as picture:
Output in unary.
This is so bad but my brain is completely fried rn, this will do for now. Definitely can be golfed though (削除) (watch @Bubbler get some insane 30 byte answer lmao). (削除ここまで) Bruh ain't no way, Bubbler just post some insane 19 bytes answer. Just move on and go upvote Bubbler's answer lmao.
-
\$\begingroup\$ Your answer is still very impressive even if bubbler's is shorter \$\endgroup\$mousetail– mousetail2023年08月23日 05:13:50 +00:00Commented Aug 23, 2023 at 5:13
-
\$\begingroup\$ @mousetail Lol shorter by nearly 3x, that's how much I overcomplicated it... \$\endgroup\$Aiden Chow– Aiden Chow2023年08月23日 05:20:40 +00:00Commented Aug 23, 2023 at 5:20
Cascade, 6 bytes
#|
\?
Try it online! (prints 0
)
Try it online!Try it online!Try it online! (prints 00
)
Try it online!Try it online!Try it online!Try it online!Try it online!Try it online! (prints 00000000
)
Outputs in unary with zeros. Uses the fact that \$f_n = 1 + \sum_{i=0}^{n-2} f_{i}\$.
#| Start evaluating at the first `#`; prints 0 once (acting as 1+)
\? `\` Move to the second column; skip evaluating f(n-1)
#| `|` Pass through
\? `?` Evaluate both columns; the left column evaluates f(n-2) (by induction)
#| and the right passes through, eventually evaluating all branches
... down to f(1)
Cascade, 10 bytes
#1
\+/
\||
Try it online! (outputs 1)
Try it online!Try it online!Try it online! (outputs f3 = 2)
Try it online!Try it online!Try it online!Try it online!Try it online!Try it online! (outputs f6 = 8)
After repeating, the code looks like this:
#1
\+/
\||#1
\+/
\||#1
\+/
\||#1
\+/
\||
The 3 columns on the left are relevant. Read the explanation from bottom to top:
# Print the value as number
\
| Ignore one iteration and fetch fn
+/ Iterate n-1 times (when n is the number of repetition):
\|| (a, b) = (a+b, a)
10 Initialize to a = 1 (column 2) and b = 0 (column 3)
Cascade, 3 bytes footer, 3 bytes body
body:
\?
footer:
#
Try it online! (outputs 0
)
Try it oTry it oTry it online! (outputs f3 = 2 = 00
)
Try it oTry it oTry it oTry it oTry it oTry it online! (outputs f6 = 8 = 00000000
)
Outputs in unary with zeros. ?
evaluates the center (of the next row), and then evaluates the left (of the next row) because the center always evaluates to 0. This certainly achieves better encoding of fibonacci loop, (削除) but I couldn't figure out how to include "print 0" in the loop (削除ここまで).
-
2\$\begingroup\$ repeated Cascade programs look like the weirdest Christmas tree \$\endgroup\$Jo King– Jo King2023年08月24日 00:43:37 +00:00Commented Aug 24, 2023 at 0:43
-
\$\begingroup\$ I appreciate the repeated
Try it online!Try it online!Try it online!Try it online!Try it online!Try it online!
\$\endgroup\$mousetail– mousetail2023年08月24日 20:59:50 +00:00Commented Aug 24, 2023 at 20:59
C (gcc), 84 bytes (no header/footer)
t
#ifndef A
,b;main(a){
#define At
#define A printf("%d",a);}
#endif
t=a;a=a+b;b=t;A
This really shows the power of the preprocessor 💪
-
2\$\begingroup\$ I like abusing the
#define At
to delete the}
from every iteration but the last \$\endgroup\$mousetail– mousetail2023年10月17日 15:03:35 +00:00Commented Oct 17, 2023 at 15:03 -
\$\begingroup\$ Welcome to Code Golf and nice answer! \$\endgroup\$2023年10月17日 15:06:53 +00:00Commented Oct 17, 2023 at 15:06
-
\$\begingroup\$ You might have misread the challenge specification (you should only print the last number) but this can easily be fixed: Try it online. Otherwise a great first answer. \$\endgroup\$bsoelch– bsoelch2023年10月17日 15:11:20 +00:00Commented Oct 17, 2023 at 15:11
-
\$\begingroup\$ yeah,I probably should have read the entire challenge. one possible fix for that would be to put the printf in the A macro something like
#define A printf("%d", a);}
should I edit my answer? \$\endgroup\$Consindine– Consindine2023年10月17日 15:20:12 +00:00Commented Oct 17, 2023 at 15:20 -
\$\begingroup\$ @Consindine Yes, please edit your answer to make it valid \$\endgroup\$mousetail– mousetail2023年10月18日 04:34:16 +00:00Commented Oct 18, 2023 at 4:34
05AB1E, 4 bytes (no header/footer)
1⁄43⁄4Åf
Try it online or verify the first ten iterations.
Explanation:
1⁄4 # Increase counter variable `3⁄4` by 1 (which is 0 by default)
3⁄4 # Push counter variable `3⁄4`
Åf # Pop and push the 0-based 3⁄4'th Fibonacci value
# (after which the top of the stack is output implicitly as result)
Brain-Flak, 16 bytes
(()[[]]({})<>{})
( # Push...
() # One
[[]] # Minus the current stack height
({}) # Plus peek the value on top of the stack
<> # Move to the other stack
{} # Plus pop the value on top of the stack
) #
The (()[[]]({})
trick is a convenient way to initialize the first stack to a '1' if it's empty, and leave it unmodified if it's non-empty. Also it's not everyday that a brain-flak solution has an odd number of <>
s!
Swift 5.8, no header/footer, 43 bytes
var x=0,y=1;defer{print(x)}
(x,y)=(y,x+y)//
Port of Value Ink's Ruby answer. Try it on SwiftFiddle!
Swift 5.6, no header/footer, 106 bytes
This was the original answer. Even though the other one is shorter and also works in 5.6, I'm leaving this one here for the absurdity of it.
var x = -1,y=1//"
print(String(repeating:"1",count:y),terminator:"")
(x,y)=(y,x+y)
#if swift(<4)
(#endif
"
This abuses a bug (patched in 5.7) where, if an inactive #if swift
block contains unmatched parentheses, the #endif
may be followed by an ill-formed string literal, including one with no close quote. I want to elaborate on why this is so strange:
When a #if
block is inactive due to os()
, arch()
, canImport()
, a -D
flag, or #if false
, it's still parsed, but not compiled: example. However, if it's inactive due to swift()
, it isn't parsed. It's tokenized so that the compiler knows where the correct #endif
is, but it's not fully parsed: example. So, the fact that mismatched parentheses inside an inactive #if swift()
do anything is bizarre, to say the least.
When this code is appended to itself, the redeclaration of the variables becomes an unused string:
var x = -1,y=1//"
print(String(repeating:"1",count:y),terminator:"")
(x,y)=(y,x+y)
#if swift(<4)
(#endif
"var x = -1,y=1//"
print(String(repeating:"1",count:y),terminator:"")
(x,y)=(y,x+y)
#if swift(<4)
(#endif
"
The lack of space before the line comment throws off SwiftFiddle's syntax highlighting; SE's highlighting is correct. With that out of the way,
Ruby, (削除) 26 (削除ここまで) 21 bytes
[a=$.+$.=a||1];p *[a]
Thanks Value Ink, Jo King and Sisyphus for the improvements.
In short
When it's repeated, it will just increase the number and print nothing:
[a=$.+$.=a||1] # -> initializes a with 1
#N times:
p *[a][a=$.+$.=a||1] # -> update a then print the non-existing a-th element of [a]
p *[a] # -> print all elements of [a]
I didn't know that the splat operator could be used like this.
-
1\$\begingroup\$ You don't actually need to declare a global variable like
$a
, the local versiona
works just as well. Also,[]
is truthy in Ruby, so you don't need to put a value just to make it truthy. Try it online! \$\endgroup\$Value Ink– Value Ink2023年08月23日 18:50:31 +00:00Commented Aug 23, 2023 at 18:50 -
\$\begingroup\$ numbers are also truthy, so you could technically just do
1
, even if it's the same size \$\endgroup\$Jo King– Jo King2023年08月24日 00:41:55 +00:00Commented Aug 24, 2023 at 0:41 -
1\$\begingroup\$
[a=$.+$.=a||1];p *[a]
for 21 \$\endgroup\$Sisyphus– Sisyphus2023年08月24日 02:06:36 +00:00Commented Aug 24, 2023 at 2:06
Uiua, (削除) 89 (削除ここまで) (削除) 36 (削除ここまで) (削除) 25 (削除ここまで) 19 bytes, no header/footer/tag
:try(+,e)(1 0;)
e=
Try it online!Try it online!Try it online!
Try it online!Try it online!Try it online!Try it online!Try it online!Try it online!
The programs look a bit different in the links thanks to the auto-formatter; if you paste the program in as given it turns into that then works. Note the trailing newline.
Although tag
is the only non-random way to make an expression stateful, this exploits the ability to repeatedly shadow built-in constant e
, as well as to catch empty-stack errors with ⍣ try
.
For the first copy, the stack is empty, so:
try( ) Try:
e Push Euler's constant,
, then push the nonexistent stack element under it.
try ( ) That errors, so
; discard the error message
1 0 and push 0 and 1.
: Swap to 1 and 0.
e= Pop the 0 to redefine e as 0.
The stack now contains only 1
which prints 1
as the only value on the stack if no further copies are appended.
For the second copy, the stack is non-empty, so:
try( ) Try:
e Push e (now 0),
, then push the value under it (1),
+ and add them.
try ( ) That doesn't error, so nothing weird happens to it.
: Swap, so the old result (1) is on top
and the sum (also 1) is on the bottom.
e= Pop the old result to re-redefine e as 1.
The stack now contains only 1
then
try( ) Try:
e Push e (now 1),
, then push the value under it (1),
+ and add them.
try ( ) That doesn't error, so nothing weird happens to it.
: Swap, so the old result (2) is on top
and the sum (1) is on the bottom.
e= Pop the old result to re-redefine e as 1.
The stack now contains only 2
and so on.
-
1\$\begingroup\$ I guessed stack underflow is a hard error, but it's apparently not. Nice find! Also
e
being assignable lol. \$\endgroup\$Bubbler– Bubbler2023年10月18日 04:05:16 +00:00Commented Oct 18, 2023 at 4:05
Vyxal, no headers, no footers, just 3 bytes
!∆f
or a test suite of the first 1 to 9 repetitions
Hello to everyone who ports this in another stack language or independently discovers it. :p
Explained
!∆f
! # Push the length of the stack
∆f # Push the lengthth fibonacci number, starting from f(0) = 1
-
\$\begingroup\$ ...lengthth? What? \$\endgroup\$Bbrk24– Bbrk242023年08月24日 21:20:43 +00:00Commented Aug 24, 2023 at 21:20
-
\$\begingroup\$ @Bbrk24 (length)th \$\endgroup\$2023年08月24日 22:33:28 +00:00Commented Aug 24, 2023 at 22:33
Nekomata, 4 bytes (no header/footer)
x+1I
A port of @The Thonnu's Thunno 2 answer.
x+1I
x+ Add without popping; fails if length of stack is less than 2
1I Replace failure with 1
JavaScript (Node.js), 38 bytes, 0 Header/Footer
Creates a anonymous function
(a=>a?b=>b?f=c=>c?c(f)(a):a()+b():1:1)
Lambda Calculus go brr
-
\$\begingroup\$ This is what I was going for but I don't know Lambda Calculus :p \$\endgroup\$noodle person– noodle person2023年08月23日 10:58:49 +00:00Commented Aug 23, 2023 at 10:58
Ruby, (削除) 29 (削除ここまで) 23 bytes
Dug around for a bit until I found this magical function called at_exit
/END
that could be used to ensure the number is printed once at the end. $.
is 0 by default, which saves some bytes.
Someone else can port the Unary solution if they want to.
-6 bytes from dingledooper.
a=1;END{p$.}
$.=a+a=$.#
-
1\$\begingroup\$
at_exit
->END
\$\endgroup\$dingledooper– dingledooper2023年08月23日 00:58:14 +00:00Commented Aug 23, 2023 at 0:58 -
1\$\begingroup\$ And I believe the second line can be
$.=a+a=$.#
. \$\endgroup\$dingledooper– dingledooper2023年08月23日 01:06:32 +00:00Commented Aug 23, 2023 at 1:06
Python 3.8 (pre-release), 41 bytes, not unary solution
for i in(1,a:=0):
i,a=i+a,i or+print(a)#
vim, 10 bytes
0py$"_cla
Note that the above ends with an ASCII "escape" character which, at least in my browser, is invisible. Basically, this is 0py$"_cla<ESC>
.
This produces the letter "a" once when run once, once when run twice, twice when run 3 times, 3 times when run 4 times, 5 times when run 5 times, 8 times when run 6 times, and so on.
Here is an explanation, along with equivalent Python code, where L
and R
denote the text to the left and the right of the cursor, and Y
denotes the contents of the current register:
0
: move the cursor to the beginning of the line (Python:L, R = '', L + R
)p
: paste the contents of the default register, and put the cursor at the end of it (Python:L, R = L + R[:1] + Y[:-1], Y[-1:] + R[1:]
)y$
: set the default register to all the text after the cursor (Python:Y = R
)"_
: when running the following command, do not set the default register to the deleted textcla<ESC>
: delete the character after the cursor (if there is one) and insert an "a" (Python:R = 'a' + R[1:]
)
Near miss: the program y0ドルpcla<ESC>
would be an 8-byte solution, except that it generates the sequence at the wrong offset: running it twice produces "aa," running it three times produces "aaa," running it four times produces "aaaaa," and so on. The program 0py$cla<ESC>
doesn't work because the command cla<ESC>
sets the default register to the deleted text (unless it's preceded with "_
).
Backhand, 9 bytes
~+~:rO!:@
Backhand's IP jumps by three cells by default, bouncing off at edges. This code makes use of this property to the max. Since the length of the base program is a multiple of 3, any amount of repetition will run as follows:
~ : ! ~ : ! ... (forward) set up the initial stack
+ r : + r : ... (backward) compute Fibonacci
~ O @ (forward) print the number and halt
set up the initial stack:
~ pop off one element; no op on first iteration
: dup; pushes two zeros on first iteration
! not; change the top zero to one
~ pop off one
: dup 0
! not to make it 1
...
compute Fibonacci: [a b]
: dup [a b b]
r reverse stack [b b a]
+ add [b a+b]
print and halt:
~ pop off b
O print a as num
@ halt
Node.js, 36 bytes, no header/footer
Returns the result in the exit code.
The highest possible value depends on the operating system. I think that's 8-bit/unsigned on Linux and at least 32-bit/signed on (recent versions of) Windows.
a=0;b=1
process.exitCode=a=b+(b=a)//
See the exit code in the debug section:
Try it online! x1
Try it online! x10
-
\$\begingroup\$ TIO shows exit code as 0, it shouldn't it be 55? \$\endgroup\$mousetail– mousetail2023年08月23日 08:21:36 +00:00Commented Aug 23, 2023 at 8:21
-
\$\begingroup\$ Seems it should be
process.exitCode
,process.exit
is a method. nodejs.org/api/process.html#process_process_exit_code \$\endgroup\$mousetail– mousetail2023年08月23日 08:23:13 +00:00Commented Aug 23, 2023 at 8:23 -
\$\begingroup\$ @mousetail You're right. Thank you! \$\endgroup\$Arnauld– Arnauld2023年08月23日 08:32:33 +00:00Commented Aug 23, 2023 at 8:32
Labyrinth, 14 bytes
#
-(
:
=+{!@
The "repeat the source" requirement is extremely hard in Labyrinth (maybe easier than Hexagony, where it can be rightout impossible, but still), especially to golf it down, mainly due to the IP routing across source boundaries.
I experimented with many layouts for two full days, only to find that the second simplest layout can be golfed by 1 byte (the simplest being the one below).
# # push stack height [0 | ]
-( (- decrement and subtract from below [1 | ] (implicit zeros are used)
: :=+ duplicate, swap the tops of two stacks, add [1 | 1]
=+{!@ (one step of fibonacci)
# the top is not zero, so turn right at the junction
-( #(- push stack height(1), decrement, subtract (becomes noop) [1 | 1]
: so repeat running fibonacci until it hits the bottom row
=+{!@
#
-(
:
=+{!@ {!@ print the previous fibonacci and exit
Labyrinth, 15 bytes
:1
;
:
=
+
_=!@
The "simplest layout" that uses _
"push 0" at the junction to skip to the next iteration.
Headass, 56 bytes
U+O[+O[ORO[]]+E.U-):R-OU[UO]OUO[]]+E;U({])UU+O[]]E:?-(}.
Prints the word debug
fib(n) times, where n is the number of copies, separated by newlines. This relies on the fact that DSO doesn't actually provide any debug info when the debug operation is called. it just prints the word 'debug'. Pretty funny stuff!
Linking up to 6 copies because that's how many it took to convince myself:
Try It Online!, Try It Online!x2, Try It Online!x3, Try It Online!x4, Try It Online!x5, Try It Online!x6
Explanation:
Overall strategy: Each repeat of the code (we will call them "chunks") just aims to print enough 'debug's to reach the next fibonacci number. So the first chunk needs to print 1, since fib(1) = 1, but chunk two doesn't want to print anything, since chunk one already got us to fib(2) = 1, but then chunk three wants to print one more, since fib(3) = 2... etc etc etc. Luckily, there is a sequence which describes exactly this phenomenon:
1 0 1 1 2 3 5 8 13 21...
It is the fibonacci sequence.
Each chunk increments a global variable (called i
in the explanation below) and runs a short loop to get fib(i) but with fib starting with 1, 0. Then, it prints 'debug' that many times before moving on to the next chunk.
U+O[+O[ORO[]]+E. code block 0 (and 2, and 4...)
U+O if i exists, i++, else let i = 1
[+O let a = 1
[O let b = 0
RO dont worry about it
[]]+E go to code block 2*(i-1)+1
with these values
. end code block
U-):R-OU[UO]OUO[]]+E;U({])UU+O[]]E:?-(}. code block 1 (and 3, and 5...)
U-) ; if i == 0
U({]) : } while(a != r0){
? print('debug')
-( a-- (effectively)
}
U dont worry about it
U+O i++
[]]E go to code block 2*i
with the new i value
: }else{
R-O i--
U[ r2 = a
UO a = b
]O b = b + r2
UO dont worry about it
[]]+E go to code block 2*i+1
with these new values
. end code block
all those parts where i say not to worry about it are just things that have to do with the specific ways im shuffling values around on the queue, and it would take up too much space to explain while not adding much clarity. Like and subscribe
Headass, 27 bytes + 7 byte footer, outputs numerically
U+O[]]E.U)UP:R-OU^[U]ODONE.
Footer:
UODONOE
This one doesn't score as well, but overall it is much shorter and actually outputs numerically rather than with debug messages, so I thought it'd be worth including here.
Try It Online!, Try It Online!x2, Try It Online!x3, Try It Online!x4, Try It Online!x5, Try It Online!x6
Explanation:
Overall strategy: iterate through all of the even code blocks to count how many copies there are, ending up at the footer, which then uses this count to initialize a loop on code block 1 which just gets fib(n) in a normal way. I want to believe it's possible to have something similar to this with no header/footer, but I don't have any ideas for that yet.
U+O[]]E. code block 0 (and 2, and 4...)
U+O if i exists, i++, else let i = 1
[]]E go to code block 2*i
. end code block
UODONOE code block 2*(number of copies + 1)
UO save i on the queue
DO a=0
NO b=1
E go to code block b (1)
U)UP:R-OU^[U]ODONE. code block 1
U) : NE while(i){
R-O i--
U^ r1 = a
[ r2 = a
U]O a = b + r2
DO b = r1
NE } (go to code block 1)
UP print a
. end code block
Jelly, 5 bytes
ṛ$‘ÆḞ
A niladic link that as shown outputs 1 but when concatenated n times outputs the nth member of the Fibonacci sequence.
Explanation (example for n = 2)
ṛ$‘ÆḞṛ$‘ÆḞ
ṛ$ | Within a monadic chain, right argument (will be zero)
‘ | Increment by 1
$ | Following as a monad:
ÆḞ | - Member of the Fibonacci sequence
ṛ | - Right argument, restoring the original argument of this monadic chain
‘ | Increment by 1
ÆḞ | Member of the Fibonacci sequence
Fortran (GFortran), (削除) 77 (削除ここまで) 58 bytes body, (削除) 42 (削除ここまで) 13 bytes header and footer
!mandatory header
program x
!repeatable code
if(j<1)i=1;l=i;if(l>0)write(*,'(A)')('x',k=1,l);i=j;j=j+l
!mandatory footer
end
Output is unary and printed vertically. The code depends on the compiler to initialize local variables to zero, e.g. with -finit-local-zero
for GFortran.
Pyth, 10 bytes (no header/footer)
eaYfgTs>2Y
# implicitly assign Y=[]
f # find the lowest number starting at 1 for which lambda T is true
>2Y # the last two elements of Y
s # summed
gT # is less than or equal to T
aY # append to Y
e # return the last element of Y
Nim, 60 bytes (no header/footer, outputs in decimal)
var a,b=0;b=1;addQuitProc do{.noconv.}:echo a
(a,b)=(b,a+b)#
Without using comments, 82 bytes
Includes a trailing newline.
when not declared a:
var a,b=0;b=1;addQuitProc do{.noconv.}:echo a
(a,b)=(b,a+b)
Unary output, 46 bytes
Port of loopy wait's Python answer.
var a,b="x";b=""
stdout.write a
(a,b)=(b,a&b)#
><>, 13 bytes
\\n;
1:
0@
+
When repeated, it looks like this:
\\n;
1:
0@
+\\n;
1:
0@
+\\n;
1:
0@
+
The first column sets up the stack, the second runs the Fibonacci loop, and the horizontal path prints and exits. Extra copies of 1 0
s are ignored.
Explore related questions
See similar questions with these tags.
(a,b)=>a
outputted 1,(a,b)=>a(a,b)=>a
outputted 2, etc. would that be okay? Or should it be a full program \$\endgroup\$