3 ms·
The biggest problem with the 1^2...n^2 example is syntactic, honestly. If Java had native tuples and destructuring, the code would look quite clean: Stream
by Sharlin 8y ago
The biggest problem with the 1^2...n^2 example is syntactic, honestly. If Java had native tuples and destructuring, the code would look quite clean:
Stream.iterate((1,1), ((idx, _)) -> (idx+1, Math.pow(idx+1, 2)));
As an aside, as each term of this particular sequence only depends on the index, not the previous term, the cleanest way would be to just map an index sequence:
// Full overflow prevention left as an exercise to the reader
IntStream.rangeClosed(0, Integer.MAX_VALUE).map(n -> Math.pow(n, 2))