Fibonacci Sequenze without using loops!

-2

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
  • Name your function "fibonacci"
  • Call no other functions except "fibonacci"
  • No loops are allowed
  • Have fun ;)

Scoring

Popularity contest (Most upvotes minus downvotes)

End Date

June 17. 2014

No special language required

Tyralcori

Posted 2014-06-10T19:30:46.040

Reputation: 195

Question was closed 2014-06-10T20:39:47.390

2Two days for the contest? – Kyle Kanos – 2014-06-10T19:40:37.177

^ What do you think is okay? – Tyralcori – 2014-06-10T19:44:28.867

1Typically, 1 week is what is used. – Kyle Kanos – 2014-06-10T19:44:50.263

^ thx. Let's raise the end date – Tyralcori – 2014-06-10T19:48:49.777

When you say "Call no other functions", how do you define "function"? Is addition a function? – Ypnypn – 2014-06-10T19:53:46.387

Like what? Only recursive calls are allowed - that must be enough. – Tyralcori – 2014-06-10T19:54:46.807

@Tyralcori It's not. Recursion without any other functions can't do anything. – Ypnypn – 2014-06-10T19:59:37.080

@Ypnypn sorry, but i think, you are pretty wrong.. Look at MT0's answer ;) – Tyralcori – 2014-06-10T20:01:14.243

@Tyralcori That answer uses two functions: addition and subtraction. – Ypnypn – 2014-06-10T20:02:09.137

@Ypnypn okay, this "function" / addition is allowed and important. – Tyralcori – 2014-06-10T20:03:44.517

@Ypnypn: At least in JavaScript, + is an operator, not a function. – Dennis – 2014-06-10T20:09:45.297

@Dennis my words, but i wanna point this out for Ypnypn – Tyralcori – 2014-06-10T20:10:03.933

possible duplicate of Fibonacci function or sequence

– Digital Trauma – 2014-06-10T20:27:44.320

Recursion is a loop. What does "use only 1 int var" mean a) for use of non-int variables? b) for stack-based languages? c) for languages whose only primitives are combinators? – Peter Taylor – 2014-06-10T22:13:33.730

Answers

3

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.

Kyle Kanos

Posted 2014-06-10T19:30:46.040

Reputation: 4 270

Explanation for those unfamiliar with this language? :) – Jwosty – 2014-06-16T05:41:25.897

@Jwosty: updated – Kyle Kanos – 2014-06-16T15:30:48.340

3

Python

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

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.

n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳

Posted 2014-06-10T19:30:46.040

Reputation: 5 683

3

EXCEL / NUMBERS / $spreadsheetapp

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!

enter image description here

german_guy

Posted 2014-06-10T19:30:46.040

Reputation: 569

1http://codegolf.stackexchange.com/a/1844/9498 – Justin – 2014-06-10T20:40:42.510

Oh great, you found it :) Thank you, obviously I didn't look deep enough – german_guy – 2014-06-10T20:42:32.183

1I knew it. That's why it was easy for me. – Justin – 2014-06-10T20:46:15.193

1

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

MT0

Posted 2014-06-10T19:30:46.040

Reputation: 3 373

1The point is to return the first n Fibonacci numbers, though, not just the nth number. – n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ – 2014-06-10T20:04:06.690

1I wonder if array comprehension should count as a loop? – nderscore – 2014-06-10T20:28:14.680

@nderscore That's why I put in the ones without array comprehension – MT0 – 2014-06-10T20:47:45.633