Who invented the computer? This turns out to be a far more complicated question than you might imagine. There is no doubt that it was mathematicians who first conceived of a universally programmable machine, but which mathematicians?

Conceptually, the computer might have been invented at any time in the last 400 years. Sir Francis Bacon is the first on record (in 1623) to suggest that a one and a zero could code for everything. With hindsight the computer could have become a practical proposition at any time after the invention of the radio valve in 1905. Why did it actually happen in the 1940s? The necessities of war drove its development but first someone had to show how it might be constructed.

Alan Turing, whose centenary falls this year, has several claims to fame, any one of which would have sufficed. He belonged to perhaps the most rarefied elite on earth: the first-rank creative mathematicians. That, in itself, might have conferred a limited cachet but in 1937 he wrote the definitive paper, "On Computable Numbers", that provided the essential touch-paper for the explosion that followed.

He was a key code-breaker at Bletchley Park in the Second Worold War. After the war he was, for a time, at the National Physical Laboratory (NPL), Teddington, in charge of building the first peacetime computer. And in his last creative phase he turned to biology, pioneering studies on how an organism grows from a single cell – research that was only recognised decades after his death.

But if Turing's fame now mostly stems from the Enigma code-breaking, it is the computer that has had the widest long-term repercussions. The story of its origins is not widely known but two very fine books can put that right. George Dyson's new Turing's Cathedral and Andrew Hodges's classic Alan Turing: The Enigma. Hodges wrote the latter in 1983 and it is justly still in print, because it is surely one of the greatest biographies of the 20th century. It sympathetically and meticulously reveals the whole Turing, insofar as that is possible for such a complex and tormented man.

The title of George Dyson's book refers to the vast edifice of computing that has grown from Turing's pioneering work. Dyson brings out many philosophical implications of the growth of computing power and the parallels between life's codes and computer codes. But this is essentially the American side of the story, with John von Neumann as the central figure and the Princeton Institute for Advanced Studies as the backdrop. Von Neumann was a Hungarian Jewish mathematician who after a precocious early career came to Princeton in 1930.

Von Neumann and Turing were probably the two most gifted mathematicians of their age and their paths crossed several times. Turing had been given von Neumann's Mathematical Foundations of Quantum Theory as a school prize in 1932. In 1937, Turing's epochal paper showed that just a few logical operations could actually perform all of mathematics. And not only mathematics: all symbolic operations, such as language, music and visual imagery, could be encoded and manipulated by such a system. A few months before the paper was published, Turing took up a temporary fellowship at Princeton, where he met von Neumann and completed a PhD. Von Neumann offered him a job as his assistant but this was now 1939, and Turing was needed in Britain.

Turing's war was one of two halves. Until 1942 he was intensely involved in cracking the German naval Enigma codes at Bletchley Park, devising an electro-mechanical machine – the Bombe – that could search for contradictions in the German ciphers. This narrowed the search for the key to the point where decipherment became possible. But from 1942, as the war machine gathered impetus, he was switched to more long-term research. In the winter of 1942-3 he spent time in America where he worked on speech encryption at Bell laboratories.

Strangely, a significant development at Bletchley for the future of computing did not involve Turing directly. His Bombes used old-fashioned electrical relays, similar to those in telephone exchanges. But in 1943 the Post Office Research Station produced Colossus, a 1500-valve electronic machine that would inspire Turing's efforts after the war.

As Dyson relates, similar developments were taking place in the US, but here the spur was plotting the trajectories of large shells and developing the atomic and hydrogen bombs rather than decipherment. Von Neumann and his team worked from Turing's plan for a digital computing machine, but how to realise this in terms of practical engineering was not at all obvious. A computer could in principle be made out of any system of switches or valves but, to be able work at a useful speed and to handle the vast computations required, it had to be electronic. With the war over, Turing began to put his plan for a digital computer into action at the National Physical Laboratory. This was intended to produce "the National Computer" but events unfolded differently. The contrast with the US is stark. Dyson has von Neumann pulling strings on every important committee, straddling the worlds of the military, academia and business (in the form of IBM).

Alan Turing was not meant for institutions. The NPL project lost momentum through the British diseases of stretched resources, bureaucracy and academic snobbery. Although Turing himself loved benchwork and taught himself how to make electrical circuitry, his computer project was hindered by the British class-fuelled demarcation between intellectual and manual work. Better hand-brain coordination was achieved by a rival project at Manchester University, which produced the world's first working stored-program, general-purpose computer on 21 June 1948. Turing joined the Manchester unit, but to use the machine for his mathematical and biological ends rather than develop it.

Despite the muddles and rivalry, the world's first commercial computer, the Ferranti Mark 1, based on the Manchester model, was produced in Britain in February 1951, one month before the American Univac 1. But that was the last time Britain was ahead in this race.

Dyson vividly evokes the dazzling array of mathematical and engineering brainpower assembled at the Princeton Institute of Advanced Studies. As the 1950s began, they were increasingly involved with the creation of the H-bomb, and their computer was developed to design the detonation mechanism for this weapon.

There was a special atmosphere of single-mindedness about the goal and a lack of most of the usual impediments to creative sharing. As one of the engineers said: "nobody worried about patents or priorities". This inattention built up great trouble for the future. The stored program computer was never patented and, when it became big business, litigation and bad feeling ensued, culminating in a major US legal battle in 1973.

Of course, now we know who were the winners: IBM, Intel, Microsoft, Apple, Google, Amazon, eBay, Facebook etc – all American companies. Turing's Cathedral is a wise and meticulously researched account of a vital period in our technological history, peopled by remarkable characters painted in the round.

Turing's life, multifaceted as it was, does expose at every turn the fault lines in 20th-century Britain. Although he was a Fellow of King's College, Cambridge, and of the Royal Society, and was awarded the OBE for his wartime work, the British establishment didn't really know what to do with a man who went his own way and broke so many social codes. In 1952 he was convicted for homosexual acts and subjected to chemical castration as an alternative to prison. He committed suicide on 7 June 1954.

Turing was an unhappy and humiliated man, but could there have been other reasons for taking his own life when he did? The most likely contributing factor was the climate of McCarthyite Cold War paranoia (Turing was keeper of the Bletchley secrets), involving the supposed connection between Cambridge's intellectual elite and communist sympathies and, above all, the notional vulnerability of homosexuals in high places to blackmail by nefarious foreign powers. Turing ticked too many boxes for his own good, both as a scientist and as a public man.

Britain's societal failings are still painfully on show in the Establishment's cack-handed efforts to make amends for the persecution Turing suffered. In 2009 Gordon Brown issued an apology on behalf of the government but a campaign for legal exoneration has foundered. On 7 February, justice minister Lord McNally stated a pardon for Turing was inappropriate because he was "properly convicted of what at the time was a criminal offence".