Gordon Brown has said he is sorry for the "appalling" way World War II code-breaker Alan Turing was treated for being gay.
A petition on the No 10 website had called for a posthumous government apology to the computer pioneer.
In 1952 Turing was prosecuted for gross indecency after admitting a sexual relationship with a man. Two years later he killed himself.
The campaign was the idea of computer scientist John Graham-Cumming.
He was seeking an apology for the way the mathematician was treated after his conviction. He also wrote to the Queen to ask for Turing to be awarded a posthumous knighthood.
The campaign was backed by author Ian McEwan, scientist Richard Dawkins and gay-rights campaigner Peter Tatchell. The petition posted on the Downing Street website attracted thousands of signatures.
Mr Brown, writing in the Telegraph newspaper, said: "While Mr Turing was dealt with under the law of the time and we can't put the clock back, his treatment was of course utterly unfair and I am pleased to have the chance to say how deeply sorry I and we all are for what happened to him."
He said Mr Turing deserved recognition for his contribution to humankind.
In the statement he said: "So on behalf of the British government, and all those who live freely thanks to Alan's work I am very proud to say: we're sorry, you deserved so much better."
A niece of Mr Turing, Inagh Payne, said that at the time she had no idea about his contribution to the war effort because he kept his work "hush-hush".
She was also unaware of his sexuality and his prosecution as the family "kept mum about that sort of thing". She said she was "very grateful" for the apology.
"We realise now that he was gay and we think he was treated abominably," she said.
Welcoming Mr Brown's move, Peter Tatchell of gay rights group Outrage! said a similar apology was also due to the estimated 100,000 British men who suffered similar treatment.
"Singling out Turing just because he is famous is wrong," he said.
Alan Turing was given experimental chemical castration as a "treatment" and his security privileges were removed, meaning he could not continue to work for the UK Government Communications Headquarters (GCHQ).
He is most famous for his code-breaking work at Bletchley Park during WWII, helping to create the Bombe that cracked messages enciphered with the German Enigma machines.
However, he also made significant contributions to the emerging fields of artificial intelligence and computing.
In 1936 he established the conceptual and philosophical basis for the rise of computers in a seminal paper called On Computable Numbers and in 1950 he devised a test to measure the intelligence of a machine. Today it is known as the Turing Test.
After the war he worked at many institutions including the University of Manchester, where he worked on the Manchester Mark 1, one of the first recognisable modern computers.
There is a memorial statue of him in Manchester's Sackville Gardens which was unveiled in 2001.