On Thursday, February 9, 2012 11:47:30 PM UTC+1, KWright wrote:
>
> Bringing this back to the real world, did someone say imperative?  Lets 
> try declarative for a change :)
>
> import math._
> val phi = (1+sqrt(5))/2.0d
> def fib(n: Int) = round(pow(phi,n)/sqrt(5))
>
> (0 to 10) map (fib)
> // = Vector(0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55)
>

That also looks like a completely different algorithm. 
 

> I would have used BigDecimal for accuracy, but that would need me to use 
> JScience or equivalent or roll my own pow, sqrt, and round functions - a 
> bit overkill for such a simple example!
>
> Rest assured that +, /, etc. work just fine for BigDecimals in Scala, and 
> therein lies the real power of the language; the stuff that should be 
> simple remains so, unhindered by boilerplate.
>

Java is horribly stuck in legacy, it should've had a decimal (base 10) type 
ages ago, but I digress. What I don't understand is, if Scala suffers from 
runtime penalties approaching an order of magnitude for this simple 
problem, under which conditions does it start to pay off 
automagically? Perhaps the culprit here is that Fibonacci is inherently a 
linear/serial problem, so prime number factorization might be a better 
example?!

/Casper

-- 
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/javaposse/-/31_gKZErXs4J.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en.

Reply via email to