views:

49

answers:

3

I need to write a function to satisfy this input -> output list:

0 -> 0
1 -> 1
3 -> 2
4 -> 3
5 -> 5
7 -> 13
9 -> 34

f(x) = ??

+2  A: 

Well, that is incredibly easy... if you aren't concerned about over-fitting, then you can do:

switch(input)
     case 0: report 0
     case 1: report 1
     case 3: report 2
     ...
     default: report whatever

You probably need more constraints on the problem if you want a good solution. You also might consider graphing the function to see if there is any obvious pattern, or maybe show the bits involved. It would also be useful to know if the inputs and outputs are integer-valued or real-valued (is the function supposed to be continuous or discrete?). Without this information, it's a little bit hard to help.

Edit
Showing the missing numbers helps:

0 -> 0
1 -> 1
2 -> 1
3 -> 2
4 -> 3
5 -> 5
6 -> 8
7 -> 13
8 -> 21
9 -> 34

(It's the Fibonnaci numbers: f(x) = f(x-1)+f(x-2), where f(0)=0 and f(1)=1).

PS
This is a function for which dynamic programming or memoization is particularly useful.

Michael Aaron Safyan
Dynamic programming? Why?
Robert Harvey
Michael Mrozek
@Robert, if you want to invoke the function many times or if you want to return the result for a wide range of values, then dynamic programming will save you from redundant computation. Even if you invoke the function once, the use of the recursive formula f(x-1)+f(x-2) causes both forks to repeat computation unnecessarily. With dynamic programming, prior results are kept such that f(i) is not computed more than once for any given value i during the computation of f(x).
Michael Aaron Safyan
This answer has been accepted, one presumes, on the basis of [YAGNI][1]. It would not work very well for `x` > 9. [1]: http://en.wikipedia.org/wiki/YAGNI
Johnsyweb
@Johnsyweb, what? I don't think you got my meaning... it was a joke... there are an infinite number of possible functions that have the given output for the given example inputs (because, for the unspecified inputs, you could output any possible value). You need to assume that there is an UNDERLYING PATTERN; without this assumption, you can just memorize the example input/output pairs and return whatever the hell you want for the remaining inputs.
Michael Aaron Safyan
@Michael Aaron Safyan: I certainly got your joke. I don't think that @TiuTalk did, though :-)
Johnsyweb
@Johnsyweb, in that case, I'm confused... what did you mean by your comment about YAGNI?
Michael Aaron Safyan
@Michael Aaron Safyan: [Before the edit] you provided just enough code to satisfy the requirements and no more.
Johnsyweb
LOL! Thanks every one for the laugh!
Gutzofter
+2  A: 

I don't know if this is homework or not, but this is an extremely well-known sequence. A (rather large) hint is that f(n) is dependent on f(n-1) and f(n-2). If you're not concerned with just being told the answer, click here. Implementing the sequence is pretty trivially done recursively, but edit your post if you're having trouble with it and specify which part you're stuck on

Michael Mrozek
+1  A: 

Solved by Eureqa

round(exp(0.4807*input - 0.799938))

Kendall Hopkins
Actually, the sequence is a http://en.wikipedia.org/wiki/Fibonacci_sequence
Robert Harvey
@Robert That's probably true, but it doesn't make his answer wrong
Michael Mrozek