-2
\$\begingroup\$

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

\$\endgroup\$

closed as unclear what you're asking by Dennis, John Dvorak, Ypnypn, Digital Trauma, Justin Jun 10 '14 at 20:39

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

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

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.

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

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

\$\endgroup\$
  • 1
    \$\begingroup\$ codegolf.stackexchange.com/a/1844/9498 \$\endgroup\$ – Justin Jun 10 '14 at 20:40
  • \$\begingroup\$ Oh great, you found it :) Thank you, obviously I didn't look deep enough \$\endgroup\$ – german_guy Jun 10 '14 at 20:42
  • 1
    \$\begingroup\$ I knew it. That's why it was easy for me. \$\endgroup\$ – Justin Jun 10 '14 at 20:46
3
\$\begingroup\$

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.

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

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)])]
\$\endgroup\$
  • 1
    \$\begingroup\$ The point is to return the first n Fibonacci numbers, though, not just the nth number. \$\endgroup\$ – n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ Jun 10 '14 at 20:04
  • 1
    \$\begingroup\$ I wonder if array comprehension should count as a loop? \$\endgroup\$ – nderscore Jun 10 '14 at 20:28
  • \$\begingroup\$ @nderscore That's why I put in the ones without array comprehension \$\endgroup\$ – MT0 Jun 10 '14 at 20:47

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