The nominal rate is 7.12% if the real rate is 4%.
If we never made any withdrawals we'd have $1,000,000(1.0712)^30=$7,872,577 at the end of 30 years.
But we made withdrawals of $40,000, $40,000(1.03), ... , $40,000(1.03)^29 in years 1,2,...,30.
The accumulated value of that series of withdrawals is
40,000( (1.0712)^29 + (1.0712)^28(1.03) + (1.0712)^27(1.03)^2+...+(1.03)^29)
=40,000(1.0712)^29(1+(1.03/1.0712)+(1.03/1.0712)^2+...+(1.03/1.0712)^29)=$5,286,708
So the value of the account at the end of 30 years would be
$7,872,577-$5,286,708=$2,585,868
In fact you can pick any number of years you want and we'd never run out of money given these assumptions.