Today I need a way to quickly generate the n-th square, iteratively. I'm wondering about several naive approaches, all involving at least one multiplication. Maybe we can try an alternative approach based on these results: 1 = 1 (the square of 1) 1 + 3 = 4 (the square of 2) 1 + 3 + 5 = 4 + 5 = 9 (the square of 3) 1 + 3 + 5 + 7 = 9 + 7 = 16 (the square of 4) It is clear now that the sum of the first n odd itegers is n2. The proof is left to thes an exercise for the reader. I used this simple observation to write an iterator that uses no multiplications, only additions: public class SquaresIterator implements Iterator<Integer>, Iterable<Integer> {the testcase: for (int square : new SquaresIterator()) {print: 1 4 9 16 25 36 49 64 81 100 121 |

