Gordon Brown issued an unequivocal apology last night on behalf of the government to Alan Turing, the second world war codebreaker who took his own life 55 years ago after being sentenced to chemical castration for being gay.

Describing Turing's treatment as "horrifying" and "utterly unfair", Brown said the country owed the brilliant mathematician a huge debt. He was proud, he said, to offer an official apology. "We're sorry, you deserved so much better," Brown writes in a statement posted on the No 10 website.

Turing is most famous for his work in helping create the "bombe" that cracked messages enciphered with the German Enigma machines. He was convicted of gross indecency in 1952 after admitting a sexual relationship with a man.

He was given experimental chemical castration as a "treatment". His criminal record meant he was unable to continue his work for the UK Government Communications Headquarters (GCHQ) because his security privileges were withdrawn. Two years later he killed himself, aged 41.

Thousands have signed a Downing Street petition calling for an official apology, among them the novelist Ian McEwan, scientist Richard Dawkins, and gay rights campaigner Peter Tatchell.

Paying tribute to Turing's contribution to "Britain's fight against the darkness of dictatorship", Brown described him as "a quite brilliant mathematician".

"Without his outstanding contribution, the history of world war two could well have been very different," he writes.

"The debt of gratitude he is owed makes it all the more horrifying, therefore, that he was treated so inhumanely. In 1952, he was convicted of gross indecency – in effect, tried for being gay.

"His sentence – and he was faced with the miserable choice of this or prison – was chemical castration by a series of injections of female hormones."

The petition, which yesterday had 30,805 signatures, was the idea of computer scientist John Graham-Cumming, who has also written to the Queen to request Turing be awarded a posthumous knighthood. Although an official apology is unusual, the act is seen as symbolic. Alan Turing is survived by three neices – Inagh, Shuna and Janet, from his brother's first marriage – and a nephew, John Dermot Turing, from his brother's second marriage, along with their associated family members.

Acknowledging the strength of feeling, Brown wrote: "Thousands of people have come together to demand justice for Alan Turing and recognition of the appalling way he was treated. While 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.

"Alan and the many thousands of other gay men who were convicted as he was convicted under homophobic laws were treated terribly. Over the years millions more lived in fear of conviction.

"This recognition of Alan's status as one of Britain's most famous victims of homophobia is another step towards equality and long overdue."

"But even more than that, Alan deserves recognition for his contribution to humankind … It is thanks to men and women who were totally committed to fighting fascism, people like Alan Turing, that the horrors of the Holocaust and of total war are part of Europe's history and not Europe's present.

"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."

Though most famous for his codebreaking, Turing is often considered to be the father of modern computer science, having made highly significant contributions to the emerging field of artificial intelligence and computing. 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.

In 1999 Time Magazine named him as one of the 100 Most Important People of the 20th Century.

• This article was amended on Sunday 13 September 2009. We said that Alan Turing, the man often considered the father of modern computer science had no surviving family. In fact, his family includes three nieces, a nephew and his mother, and several children and spouses of this group. This has been corrected.