Consider the following series
0.5 1.5 4.5 13.5 40.5 121.5
We can generate this stream using tradional for loops. such as following
float l = 0.5;
for(int i=1;i<limit;i++)
{
if(i == 1 ) System.out.println(l+" ");
float n = l+Math.pow(3,i-1);
System.out.println(n+" ");
l = n;
}
This snippet works well. but java 8 streams has iterate functions to create such streams.
general syntax is Stream.iterate(0.5,f).limit().forEach(System.out::println);
But How will I access the previous element in the streams ? Also I need to track the i for the power of 3. Can anyone help ? Am I missing something?