# Fibonacci Sequenze without using loops! [closed]

Here is one more "Challenge"! Today i had a meeting with a friend, and mostly we are a little bit crazy.. He bets, that it is not possible to create a Fibonacci Sequence without using loops.

No static output like:

return "1, 2, 3, 5, 8, 13, ..";


GOAL / Requirements

• Use only 1 int var
• Return the first 5 (or more) numbers
• Call no other functions except "fibonacci"
• No loops are allowed
• Have fun ;)

Scoring

End Date

June 17. 2014

No special language required

• Two days for the contest? – Kyle Kanos Jun 10 '14 at 19:40
• ^ What do you think is okay? – Tyralcori Jun 10 '14 at 19:44
• Typically, 1 week is what is used. – Kyle Kanos Jun 10 '14 at 19:44
• ^ thx. Let's raise the end date – Tyralcori Jun 10 '14 at 19:48
• When you say "Call no other functions", how do you define "function"? Is addition a function? – Ypnypn Jun 10 '14 at 19:53

# Python

def fibonacci(n):
if n > 1:
return fibonacci(n-1) + [(1.618033988749895**n/2.23606797749979 + 0.2) // 1]
else:
return 


Call fibonacci(n) to generate a list up to the nth element of the Fibonacci series. Sample output of fibonacci(20):

>>> fibonacci(20)
[1, 1.0, 2.0, 3.0, 5.0, 8.0, 13.0, 21.0, 34.0, 55.0, 89.0, 144.0, 233.0, 377.0, 610.0, 987.0, 1597.0, 2584.0, 4181.0, 6765.0]


The correctness is limited by the precision of the double precision floating point.

I think I saw this somewhere here, but I can't remember. Nevertheless, credits go to unknown author. Just Fill in the first 2 fields, do a simple addition and then it's time to drag, baby! • codegolf.stackexchange.com/a/1844/9498 – Justin Jun 10 '14 at 20:40
• Oh great, you found it :) Thank you, obviously I didn't look deep enough – german_guy Jun 10 '14 at 20:42
• I knew it. That's why it was easy for me. – Justin Jun 10 '14 at 20:46

# Kona

fibonacci:{((x-1)(|+\)\1 1)[;1]}


Execute in shell as fibonacci 12 to get the first 12 Fibonacci numbers.

Explanation:

• 1 1 is a vector
• (|+\)\ generates a vector of vectors by summing the elements of the vector
• (x-1) reduces 12 to 11 (due to 0 indexing) which is applied to the summation to repeat x-1 times
• [;1] returns/prints the second element of each vector in the larger vectors.

If I neglected the [;1] portion, you can see what happens to the vector:

> fib 1
,1 1
> fib 2
(1 1
2 1)
> fib 3
(1 1
2 1
3 2)


And so on.

• Explanation for those unfamiliar with this language? :) – Jwosty Jun 16 '14 at 5:41
• @Jwosty: updated – Kyle Kanos Jun 16 '14 at 15:30

# JavaScript (ECMAScript 6)

fibonacci=n=>n<2?[0,1]:[...fibonacci(n-1),fibonacci(n-1)[n-1]+fibonacci(n-2)[n-2]]


or more efficiently

fibonacci=n=>n<2?[0,1]:(i=fibonacci(n-1),[...i,i[n-1]+i[n-2]])


or even more efficently (but with a loop instead of recursion)

fibonacci=n=>[i[m]=m<2?+m:i[m-1]+i[m-2]for(m in i=[...Array(n)])]

• The point is to return the first n Fibonacci numbers, though, not just the nth number. – n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ Jun 10 '14 at 20:04
• I wonder if array comprehension should count as a loop? – nderscore Jun 10 '14 at 20:28
• @nderscore That's why I put in the ones without array comprehension – MT0 Jun 10 '14 at 20:47