Prime Minister Gordon Brown apologized Friday to the mathematician Alan Turing, who committed suicide in 1954 after he was tried and convicted of “gross indecency” for being homosexual. Mr. Brown said that Turing had been treated “terribly” and that the outcome of World War II could have been quite different had it not been for Turing’s efforts in cracking German codes, notably the Enigma coding machine. Turing is often hailed for his influence on modern computing. Thousands of people have signed a petition calling for a formal apology from the government, though Turing has no known surviving family, The Daily Telegraph reported. Writing in the newspaper, Mr. Brown said: “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.” Turing was sentenced, as an alternative to prison, to chemical castration by a series of injections of female hormones, the prime minister noted.