#Groovy, 26 bytes This answer was basically stolen from [@Geobits][1]. a={n->n--?a(n)*~-a(n)+1:2} Took that answer and, using groovy shortcuts, optimized it further. [1]: http://codegolf.stackexchange.com/a/91164/59376