This function returns the decimal value of a number multiplied with log(2) Since the problem is on powers of two, finding the powers of two with large exponents is time consuming. Hence we use log to reduce compute time. We can find out that the first power of 2 with starting digit
(number: int)
| 18 | |
| 19 | |
| 20 | def log_difference(number: int) -> float: |
| 21 | """ |
| 22 | This function returns the decimal value of a number multiplied with log(2) |
| 23 | Since the problem is on powers of two, finding the powers of two with |
| 24 | large exponents is time consuming. Hence we use log to reduce compute time. |
| 25 | |
| 26 | We can find out that the first power of 2 with starting digits 123 is 90. |
| 27 | Computing 2^90 is time consuming. |
| 28 | Hence we find log(2^90) = 90*log(2) = 27.092699609758302 |
| 29 | But we require only the decimal part to determine whether the power starts with 123. |
| 30 | So we just return the decimal part of the log product. |
| 31 | Therefore we return 0.092699609758302 |
| 32 | |
| 33 | >>> log_difference(90) |
| 34 | 0.092699609758302 |
| 35 | >>> log_difference(379) |
| 36 | 0.090368356648852 |
| 37 | |
| 38 | """ |
| 39 | |
| 40 | log_number = math.log(2, 10) * number |
| 41 | difference = round((log_number - int(log_number)), 15) |
| 42 | |
| 43 | return difference |
| 44 | |
| 45 | |
| 46 | def solution(number: int = 678910) -> int: |