30
\$\begingroup\$

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 print 1
  • print(x)print(x) should print 1
  • print(x)print(x)print(x) should print 2
  • print(x)print(x)print(x)print(x) should print 3
  • print(x)print(x)print(x)print(x)print(x) should print 5
  • 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

Attempt This Online!

Comments are entirely valid, but solutions that don't use comments are extra impressive.

\$\endgroup\$
5
  • \$\begingroup\$ Could a submission be an anonymous function which outputs the nth fibonacci number when its body is repeated n times? Like if (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\$
    – noodle man
    Aug 22, 2023 at 19:25
  • 3
    \$\begingroup\$ @noodleman that would be OK if any repeat of the string creates a single function that takes no input \$\endgroup\$
    – mousetail
    Aug 22, 2023 at 19:30
  • \$\begingroup\$ May the program return the result by exit code? \$\endgroup\$
    – Arnauld
    Aug 23, 2023 at 8:16
  • 1
    \$\begingroup\$ @Arnauld Yes, you can use any standard output method as long as you output exactly one unambiguous value \$\endgroup\$
    – mousetail
    Aug 23, 2023 at 8:17
  • \$\begingroup\$ Great challenge \$\endgroup\$ Aug 24, 2023 at 3:27

44 Answers 44

15
\$\begingroup\$

R, 12 bytes, no header/footer

+(F=+T)->T;F

Try it once online!

Try it twice

Try it three times

Try it five times

Try it \$n\$ times

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:

  1. F now holds the trailing value \$F_{n}\$, and T is updated on each loop as F+(F=T)->T to be \$F_{n+1}\$, on every iteration.
  2. We use F=+T so that the first copy prints out 1 instead of TRUE.
  3. Leading + and the infrequently-used right-hand assignment -> operator to satisfy the concatenation requirement.
  4. Trailing ;F to use implicit output to print out the required value.
\$\endgroup\$
14
\$\begingroup\$

Piet + ascii-piet, 19 bytes (12×4=48 codels)

_TLDJdAaAQIbcr_cc _

Try Piet online!

Try Piet online! (x6)

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

Thunno 2, 4 bytes (no header/footer)

Ɓ+1|

Try it online!
2x
3x
Nx

Explanation

Ɓ+    # Without popping, add top two values
  1|  # Replace a 0 with a 1
      # Implicit output
\$\endgroup\$
11
\$\begingroup\$

Python, 34 bytes

a='x';b=''
print(end=a);a,b=b,a+b#

Attempt This Online!

Outputs in unary.

How?

Streamlined version of OP's example code

\$\endgroup\$
10
\$\begingroup\$

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

Try it online (use MS SQL)

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.

\$\endgroup\$
3
  • \$\begingroup\$ Welcome to Code Golf, and nice answer! \$\endgroup\$ Aug 23, 2023 at 23:14
  • \$\begingroup\$ Thanks LizWeir for the sql comment inspiration! \$\endgroup\$ 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
    Aug 24, 2023 at 22:22
10
\$\begingroup\$

Itr, 5 4 bytes (no header or footer)

â+1w

online interpreter

2x

3x

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

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.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ repeated Cascade programs look like the weirdest Christmas tree \$\endgroup\$
    – Jo King
    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
    Aug 24, 2023 at 20:59
8
\$\begingroup\$

Piet + ascii-piet, 53 bytes (15×7=105 codels), no header/footer

ttlddtN ub?fnNssSkKCVrTtTLcfkdtlElt?l??T   lL sdD S??

Program as picture:

Output in unary.

Try Piet online!

Try Piet online! x2

Try Piet online! x3

Try Piet online! x5

Try Piet online! x10

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.

\$\endgroup\$
2
  • \$\begingroup\$ Your answer is still very impressive even if bubbler's is shorter \$\endgroup\$
    – mousetail
    Aug 23, 2023 at 5:13
  • \$\begingroup\$ @mousetail Lol shorter by nearly 3x, that's how much I overcomplicated it... \$\endgroup\$
    – Aiden Chow
    Aug 23, 2023 at 5:20
8
\$\begingroup\$

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

Try it online!

This really shows the power of the preprocessor 💪

\$\endgroup\$
5
  • 2
    \$\begingroup\$ I like abusing the #define At to delete the } from every iteration but the last \$\endgroup\$
    – mousetail
    Oct 17, 2023 at 15:03
  • \$\begingroup\$ Welcome to Code Golf and nice answer! \$\endgroup\$ 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
    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
    Oct 17, 2023 at 15:20
  • \$\begingroup\$ @Consindine Yes, please edit your answer to make it valid \$\endgroup\$
    – mousetail
    Oct 18, 2023 at 4:34
7
\$\begingroup\$

05AB1E, 4 bytes (no header/footer)

¼¾Åf

Try it online or verify the first ten iterations.

Explanation:

¼     # Increase counter variable `¾` by 1 (which is 0 by default)
 ¾    # Push counter variable `¾`
  Åf  # Pop and push the 0-based ¾'th Fibonacci value
      # (after which the top of the stack is output implicitly as result)
\$\endgroup\$
6
\$\begingroup\$

Brain-Flak, 16 bytes

(()[[]]({})<>{})

Try it online!

(                       # 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!

\$\endgroup\$
6
\$\begingroup\$

Ruby, 26 21 bytes

[a=$.+$.=a||1];p *[a]

Try it online!

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.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ You don't actually need to declare a global variable like $a, the local version a 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
    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
    Aug 24, 2023 at 0:41
  • 1
    \$\begingroup\$ [a=$.+$.=a||1];p *[a] for 21 \$\endgroup\$
    – Sisyphus
    Aug 24, 2023 at 2:06
6
+200
\$\begingroup\$

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!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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I guessed stack underflow is a hard error, but it's apparently not. Nice find! Also e being assignable lol. \$\endgroup\$
    – Bubbler
    Oct 18, 2023 at 4:05
5
\$\begingroup\$

Vyxal, no headers, no footers, just 3 bytes

!∆f

Try it Online!

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
\$\endgroup\$
2
  • \$\begingroup\$ …lengthth? What? \$\endgroup\$
    – Bbrk24
    Aug 24, 2023 at 21:20
  • \$\begingroup\$ @Bbrk24 (length)th \$\endgroup\$
    – lyxal
    Aug 24, 2023 at 22:33
5
\$\begingroup\$

Nekomata, 4 bytes (no header/footer)

ˣ+1I

A port of @The Thonnu's Thunno 2 answer.

ˣ+1I
ˣ+      Add without popping; fails if length of stack is less than 2
  1I    Replace failure with 1
\$\endgroup\$
5
\$\begingroup\$

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)

Attempt This Online!

Lambda Calculus go brr

\$\endgroup\$
1
  • \$\begingroup\$ This is what I was going for but I don't know Lambda Calculus :p \$\endgroup\$
    – noodle man
    Aug 23, 2023 at 10:58
5
\$\begingroup\$

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=$.#

Attempt This Online! x3 x5

\$\endgroup\$
2
  • 1
    \$\begingroup\$ at_exit -> END \$\endgroup\$ Aug 23, 2023 at 0:58
  • 1
    \$\begingroup\$ And I believe the second line can be $.=a+a=$.#. \$\endgroup\$ Aug 23, 2023 at 1:06
5
\$\begingroup\$

Python 3.8 (pre-release), 41 bytes, not unary solution

for i in(1,a:=0):
 i,a=i+a,i or+print(a)#

Try it online!

\$\endgroup\$
0
4
\$\begingroup\$

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.

Try it online!

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 text
  • cla<ESC>: delete the character after the cursor (if there is one) and insert an "a" (Python: R = 'a' + R[1:])

Near miss: the program y$0pcla<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 "_).

\$\endgroup\$
4
\$\begingroup\$

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,

\$\endgroup\$
4
\$\begingroup\$

Backhand, 9 bytes

~+~:rO!:@

Try it online!

Try it online! (x6)

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

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

\$\endgroup\$
3
  • \$\begingroup\$ TIO shows exit code as 0, it shouldn't it be 55? \$\endgroup\$
    – mousetail
    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
    Aug 23, 2023 at 8:23
  • \$\begingroup\$ @mousetail You're right. Thank you! \$\endgroup\$
    – Arnauld
    Aug 23, 2023 at 8:32
4
\$\begingroup\$

Haskell, 30 bytes, 0 bytes header, 0 bytes footer

q=0:scanl(+)1q;f=(q!!)$0
 +1--

Attempt This Online!

Haskell, 44 bytes, 0 bytes header, 0 bytes footer, no comments

f where q=(0,1);qf@(f,g)=(g,f+g)where(f,g)=q

Attempt This Online!

\$\endgroup\$
4
\$\begingroup\$

Labyrinth, 14 bytes

 #
-(
:
=+{!@

Try it online!

Try it online! (6x)

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
;
:
=
+
_=!@

Try it online!

Try it online! (6x)

The "simplest layout" that uses _ "push 0" at the junction to skip to the next iteration.

\$\endgroup\$
4
\$\begingroup\$

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

Jelly, 5 bytes

ṛ$‘ÆḞ

Try it online!

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

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.

Try it online: 1x, 2x, 4x, 8x

\$\endgroup\$
3
\$\begingroup\$

Pyth, 10 bytes (no header/footer)

eaYfgTs>2Y

Try it online!

2x 3x 4x 10x

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

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

Attempt This Online!

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)

Attempt This Online!

Unary output, 46 bytes

Port of loopy wait's Python answer.

var a,b="x";b=""
stdout.write a
(a,b)=(b,a&b)#

Attempt This Online!

\$\endgroup\$
3
\$\begingroup\$

><>, 13 bytes

\\n;
1:
0@
 +

Try it online!

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 0s are ignored.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.