gregory was born in aberdeen on 13 april 1813, the youngest son of isabella macleod (1772–1847) and james gregory (1753–1821). he was taught initially by his mother, and in october 1825 he was sent to the edinburgh academy, and after two years of study spent a winter at a private academy in geneva. while there his mathematical talent attracted attention, specifically geometry. on his return to scotland, he attended classes at the university of edinburgh, working at chemistry, making experiments in polarised light, and advancing in the higher parts of mathematics, guided by professor wallace, his mentor. in october 1833 he commenced residence at trinity college, cambridge. he earned fifth wrangler in the tripos of 1837. he took the degrees of ba in 1838 and ma in 1841. he was elected fellow of trinity college in october 1840.