Alan Turing, the legendary British computer engineer and codebreaker who killed himself two years after a 1952 conviction for homosexual activity for which he was punished by chemical castration, received a royal pardon on Monday from Queen Elizabeth II.

Most famous for helping crack the German's Enigma code during World War II, Turing is also considered a father of modern computer science and the man responsible for the Turing Test, which is used to judge artificial intelligence.

"His action saved countless lives," British Prime Minister David Cameron, whose government denied Turing a pardon last year, said after the announcement. "He also left a remarkable national legacy through his substantial scientific achievements, often being referred to as the 'father of modern computing.'" Prime Minster Gordon Brown issued a former apology to Turing in 2009.

Turing was convicted on charges of gross indecency in 1952 after he admitted having sex with a man. As an alternative to prison, Turing was sentenced to chemical castration by repeated injections of female hormones. He also lost his security clearance because of the conviction. Two years later, Turing ate an apple he had laced with cyanide. He was 41.

Justice Secretary Chris Grayling, who formally requested the pardon, announced the news on Monday:

"Dr Alan Turing was an exceptional man with a brilliant mind. His brilliance was put into practice at Bletchley Park during the second world war, where he was pivotal to breaking the Enigma code, helping to end the war and save thousands of lives.

"His later life was overshadowed by his conviction for homosexual activity, a sentence we would now consider unjust and discriminatory and which has now been repealed.

"Dr Turing deserves to be remembered and recognised for his fantastic contribution to the war effort and his legacy to science. A pardon from the Queen is a fitting tribute to an exceptional man."