Helper
(defn diff [s] (map - (rest s) s))
Infer sequence based on method of finite differences
(defn infer-diff [s]
(->> (take (count s) (iterate diff s))
(map last) reverse
(drop-while zero?) ;optional
(iterate (partial reductions +))
(map last) rest
(concat s)))
Examples
(take 10 (infer-diff [1 3]))
;=> (1 3 5 7 9 11 13 15 17 19)
(take 10 (infer-diff [1 4 9]))
;=> (1 4 9 16 25 36 49 64 81 100)
Explanation
(def s (list 1 4 9))
(take (count s) (iterate diff s))
;=> ((1 4 9)
; ( 3 5 )
; ( 2 ))
(map last *1)
;=> (9 5 2)
(reverse *1)
;=> (2 5 9)
This (2 5 9)
slice of the pyramid of successive first differences is all you need to determine the rest of the sequence.
(reductions + *1)
;=> (2 7 16) and the next in the sequence is at the end: 16
(reductions + *1)
;=> (2 9 25) and the next in the sequence is at the end: 25
(reductions + *1)
;=> (2 11 36) and the next in the sequence is at the end: 36