开发者

Write a function based on given TestUnit

I need to write a function to satisfy this input -> out开发者_StackOverflow社区put list:

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

f(x) = ??


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.


Solved by Eureqa

round(exp(0.4807*input - 0.799938))


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜