I am a beginner in using Lambda expression feature in Java 8. Lambda expressions are pretty well useful in solving programs like Prime number check, factorial etc.
However can they be utilized effectively in solving problems like Fibonacci where the current value depends on sum of previous two values. I have pretty well solved prime number check problem effectively using Lambda expressions. The code for the same is given below.
boolean checkPrime=n>1 && LongStream.range(2, (long) Math.sqrt(n)).parallel().noneMatch(e->(n)%e==0);
In the above code in the noneMatch
method we are evaluating with the current value(e
) in the range. But for the Fibonacci problem, we requires previous two values.
How can we make it happen?
Stream.iterate(Fibonacci.SEED, Fibonacci::next).limit(MAX_NUMS).forEach(System.out::println)
Complete code example that shows how to generate Fibonacci using Java 8. Last one. – Swanherd