From 1-9, the numbers excluding 4 and 7 are
45-4-7. That's right
How to calculate 1-99?
4950-(700+45)-(400+45) - 45 ?
Obviously, only 7, 4, 70-79 and 40-49 are subtracted. What about the rest 14, 17, 24, 27 ...
Should be subtracted
(4 + 94)* 10/2
(7 + 97)* 10/2
(40 + 49)* 10/2
(70 + 79)* 10/2
Add 47 and 74, these two numbers are repeated three times, and then subtract them twice.
Finally = 2992.
When there are many digits, this addition and subtraction involves the arrangement and combination of digits, which is particularly complicated. Do you want to write law?
The program calculation can be optimized.
We know the sum of 1 to 9999999999.
= 1000000000*999999999/2
= 500000000*999999999
With high precision or long shaping, subtract the number including 4 and 7 in the cycle and calculate how many results are 999999999.
The algorithm is described as follows:
N = 0 (calculate how much is 500000000)
SUM = 0 (sum of numbers)
I: From 1 to 999999999, the cycle begins.
If the number contains 4 or 7, then sum = sum+I.
If SUM ≥ 500000000, then N = N+ 1, SUM = SUM-50000000, until the SUM is less than 500000000.
End of cycle
Number searched =(999999999-N)* 50000000-SUM