Exercising for half an hour at least twice a week during midlife can significantly cut a person's risk of dementia later, say researchers.
The exercise should be enough to make you sweaty and breathless
People in their late 40s and early 50s who do this could reduce their risk of dementia by about 50%, according to a study reported in Lancet Neurology.
Those who are genetically prone to Alzheimer's disease could see a reduction of about 60%, it adds.
The Swedish team said the findings had large disease prevention implications.
"If an individual adopts an active lifestyle in youth and at midlife, this may increase their probability of enjoying both physically and cognitively vital years in later life," they said.
Past studies have also suggested regular exercise might guard against dementia, however, this is one of the first to look at the effects over a long time scale - about two decades.
The authors say this is important because dementia takes many years to develop and is typically quite advanced when it is diagnosed.
The study involved nearly 1,500 men and women, of whom nearly 200 developed dementia or Alzheimer's disease between the ages of 65 and 79.
The researchers looked back at how physically active the study participants had been up to 21 years earlier, when they would have been in their late 40s and early 50s.
Those who developed Alzheimer's disease or another form of dementia were far less likely to have been active when they were middle-aged than those who remained free of dementia.
Good for the brain
The amount of exercise that appeared to be necessary to be protective was physical activity which lasted 20-30 minutes at least twice a week and which was enough to cause breathlessness and sweating.
People are generally recommended to take moderate aerobic exercise for 20-30 minutes three to five times a week for a healthy heart and lungs.
Dr Miia Kivipelto and colleagues said there were many reasons why exercise might be good for the brain as well as the rest of the body.
For example, regular exercise could help keep the small blood vessels of the brain healthy as well as protecting against other conditions that might make dementia more likely, such as high blood pressure and diabetes.
Exercise might also reduce the amount of the protein amyloid that builds up in the brain in Alzheimer's disease.
Physical activity also affects genes and compounds important for maintaining good cognition and memory.
It might be that people who exercise tend to live healthier lifestyles in general, such as drinking less alcohol and refraining from smoking, they said.
However, when they took into account such health risk factors, the findings remained the same, suggesting that exercise per se is beneficial for the brain.
A spokeswoman from the Alzheimer's Research Trust said: "This study backs up the evidence so far.
"Studies seem to suggest that leading a healthy lifestyle - exercising regularly and eating a balanced diet - helps protect against dementia."
She said more research was needed, particularly as the condition was becoming increasingly common since the proportion of older people in society was increasing.