Other answers already showed that you can solve the problem in \$O(log N)\$ compared to \$O(N)\$ when you don't actually count the numbers but sum up the numbers of possible combinations of 1, 7, and 9 of different length. When looking for a simplification of the other approaches, I found out that it helps a lot to transform the problem a bit so you can work with base 3 numbers.
I came up with thisalgorithmthis algorithm:
Find the maximum number \$m\$ with \$m \leq N\$ so that \$m\$ is a lucky number. Let \$d\$ denote the number of decimal digits of this number.
Transform \$m\$ by replacing its digits according to the following mapping:
\$ \{ \; 1 \rightarrow 0, \;\; 7 \rightarrow 1, \;\; 9 \rightarrow 2 \; \} \$
Treat the result as a base 3 number and add the base 3 number that consists of \$d\$ ones.
After step 3, you are already looking at the result. However, you might want to convert it back to base 10.
Please note, that the number may lose a digit in step 1 (e.g. when N is a power of 10). You can easily find \$m\$ by checking the digits of \$N\$ from left to right. When you encounter a digit that is not a lucky digit, reduce it to the next lucky digit and set the remaining digits (to the right) to 9. If the digit in question cannot be reduced (a zero) seek to the left until you find a digit you can reduce (7 or 9). If you find one, reduce this one instead and set all subsequent digits to 9. If you don't find one (apparently the first digit is a 1), omit the first digit and set all remaining digits to 9.
Explanation
Probably the easiest way to understand this is to look at an example: Let's take \$N = 775_{10}\$. The next lower lucky number is \$771_{10}\$ which according to our mapping translates to \$110_3\$. How does this help? Well, if we consider all ternary numbers lower than \$110_3\$ they give us all the other 3-digit lucky numbers lower than \$771_{10}\$ if we reverse the mapping again. Example: \$012_3\$ translates to \$179_{10}\$. Note that also \$000_3\$ stands for a 3-digit lucky number, namely \$111_{10}\$. This is why we have to correct our intermediate result of \$110_3\$ by adding one.
We are also missing the lucky numbers with only one and two digits. There are \$010_3\$ one-digit and \$100_3\$ two-digit lucky numbers. So in total we can just add \$111_3\$ to the intermediate result of \$110_3\$ to make up for all the numbers we missed in the first step . We obtain \$221_3 = 25_{10}\$.