Simon said:
On the contrary, it is a beautifully simple piece of recursion. In a
proper grown up language
If one is to ignore minor details of syntax, then I don't think the recursive
expression in Java is one whit less elegant than the Lisp version. If we /are/
to take such details into account then they both suck (IMO). Worse, they both
hide the underlying recurrence relation. Daniel's formulation shows how a real
functional programming language can expose the stucture transparently.
But the OP might prefer a formulation less taxing to the beginner's brain -- so
I'll append one.
BTW; for anyone interested. I should probably have used a table lookup -- even
though that means that there has to be code to populate the table. Javac
converts the switch statement into the obvious tableswitch bytecode. But then,
using JSK 1.5.0 on this 32-bit Intel box, the java -client JITer compiles that
into a sequence of 92 cmp/je instuction pairs, comparing n against 1..92 in
order ! Java -server is doing something much weirder, and I haven't yet
figured it out (my IA32 assember skillz are sub-embrionic). It produces an
almost equally long sequence of test-and-jump pairs, plus some arithmetic, but
the values are less than obvious -- I'm guessing it's some sort of hashing
scheme. Neither of them use a jump table, which I would consider to the be
obvious technique for a big switch like this. I know that the unguessable
indirection involved in an indirect jump is very pipeline unfriendly, but
compared with 50-odd compare-and-branches, which is likely to include an
unpredicted jump anyway ?!?
-- chris
========= Fibonacci.java =========
public class Fibonacci
{
public static long
value(int n)
{
// <shrug/>
switch (n)
{
case 1: return 1L;
case 2: return 1L;
case 3: return 2L;
case 4: return 3L;
case 5: return 5L;
case 6: return 8L;
case 7: return 13L;
case 8: return 21L;
case 9: return 34L;
case 10: return 55L;
case 11: return 89L;
case 12: return 144L;
case 13: return 233L;
case 14: return 377L;
case 15: return 610L;
case 16: return 987L;
case 17: return 1597L;
case 18: return 2584L;
case 19: return 4181L;
case 20: return 6765L;
case 21: return 10946L;
case 22: return 17711L;
case 23: return 28657L;
case 24: return 46368L;
case 25: return 75025L;
case 26: return 121393L;
case 27: return 196418L;
case 28: return 317811L;
case 29: return 514229L;
case 30: return 832040L;
case 31: return 1346269L;
case 32: return 2178309L;
case 33: return 3524578L;
case 34: return 5702887L;
case 35: return 9227465L;
case 36: return 14930352L;
case 37: return 24157817L;
case 38: return 39088169L;
case 39: return 63245986L;
case 40: return 102334155L;
case 41: return 165580141L;
case 42: return 267914296L;
case 43: return 433494437L;
case 44: return 701408733L;
case 45: return 1134903170L;
case 46: return 1836311903L;
case 47: return 2971215073L;
case 48: return 4807526976L;
case 49: return 7778742049L;
case 50: return 12586269025L;
case 51: return 20365011074L;
case 52: return 32951280099L;
case 53: return 53316291173L;
case 54: return 86267571272L;
case 55: return 139583862445L;
case 56: return 225851433717L;
case 57: return 365435296162L;
case 58: return 591286729879L;
case 59: return 956722026041L;
case 60: return 1548008755920L;
case 61: return 2504730781961L;
case 62: return 4052739537881L;
case 63: return 6557470319842L;
case 64: return 10610209857723L;
case 65: return 17167680177565L;
case 66: return 27777890035288L;
case 67: return 44945570212853L;
case 68: return 72723460248141L;
case 69: return 117669030460994L;
case 70: return 190392490709135L;
case 71: return 308061521170129L;
case 72: return 498454011879264L;
case 73: return 806515533049393L;
case 74: return 1304969544928657L;
case 75: return 2111485077978050L;
case 76: return 3416454622906707L;
case 77: return 5527939700884757L;
case 78: return 8944394323791464L;
case 79: return 14472334024676221L;
case 80: return 23416728348467685L;
case 81: return 37889062373143906L;
case 82: return 61305790721611591L;
case 83: return 99194853094755497L;
case 84: return 160500643816367088L;
case 85: return 259695496911122585L;
case 86: return 420196140727489673L;
case 87: return 679891637638612258L;
case 88: return 1100087778366101931L;
case 89: return 1779979416004714189L;
case 90: return 2880067194370816120L;
case 91: return 4660046610375530309L;
case 92: return 7540113804746346429L;
}
if (n < 1)
throw new IllegalArgumentException("N must be >= 1");
else
throw new IllegalArgumentException("answer cannot be expressed as a
long");
}
}
==================