Julian Richards meets a maths professor who started out as a carnival magician

Persi Diaconis is no ordinary magician. Come to that, he is no ordinary professor of mathematics and statistics. He begins his lecture with a card trick, shuffling a 32-card pack. He then asks five people to cut in turn and each take a card from the top.

"This is going to sound funny in an academic environment, but would you all look at your cards and concentrate on them," he tells the volunteers. Then comes the showmanship. "You're doing pretty well. There's a mist coming in, but you could help me by standing up if you have a red card."

Diaconis correctly identifies the mystery five cards and announces: "That's the trick. I want to talk about it, and magicians hate that I do this."

He explains that the trick of divining the cards involves mathematical calculations based on de Bruijn sequences. There is a way of arranging a 32-card deck according to a de Bruijn sequence, so that each pattern of five red and black cards appears only once. By knowing which pattern of red and black cards the people hold, a magician is able to work backwards to decide what the cards are - a tough mental workout by any standards.

Asking the reds to stand up helps his calculations. The trick, which he used to perform in carnivals, "fools magicians badly", Diaconis says. "They think that there must be some kind of X-ray marking on the cards. The notion that the information in this casual question can actually code the answer is not that easy to grasp."

After dealing with the mathematics of the card trick, he explains how de Bruijn sequences can give insights into robot vision, cryptography, Indian music and even cracking the code of digital door locks.

Diaconis specialises in probability theory and combinatorics at Stanford University. A member of the United States National Academy of Sciences and winner of the MacArthur Prize, he is lecturing on mathematics and magic tricks to an audience of academics and researchers at Hewlett-Packard Laboratories in Bristol. It is a far cry from the 1960s, when the young Diaconis had no interest in mathematics. He was, however, fascinated by magic and at 14 ran away from his New York home to perform in carnivals and travelling shows. During his ten years on the road, Diaconis visited England, played the pier at Brighton and performed in working men's clubs up and down the country.

He became interested in gamblers' techniques and wanted to know more about their tactics. So he found a textbook on probability - and, discovering a previously unknown aptitude for mathematics, he was hooked.

Despite having received no real schooling in his decade of travelling, he made it to university. In most countries such a start would have barred him from progressing far in formal education, but the flexibility of the United States system, he says, meant that at 24 he was able to enter undergraduate college.

Today, Diaconis still teaches his students from that same textbook on probability, but he tends to keep the magician element out of his student lectures. "The relationship between student and teacher is complex," he says. "I've found that mixing entertainment with university lectures never works well."

He does, though, stay in touch with magician friends and continues to keep his hand in with the tricks. He also helps to write magic shows and invents tricks that illustrate mathematical and scientific principles.

Despite his mathematical explanations, though, when it comes to his tricks, Diaconis keeps his cards close to his chest, in the best tradition of the magic circle. And, even after years of taking the stage, Diaconis still gets "a little nervous" before each performance.