If someone figured out how to lose 40 lbs safely in 3 weeks, they would be a millionaire.
You can probably do it safely, but I would imagine you would have to exercise several hours a day like they do on The Biggest Loser. I'm talking around 8 hours a day and, if you have a job, that would be almost impossible. Plus, it's depending on how much you weigh. If you are 300+, then you could do it. Any weight below that is way too dangerous. But, no matter what you weight is, you do not try to lose that kind of weight in such a short period of time unless you are being very closely medically supervised.