MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / log_difference

Function log_difference

project_euler/problem_686/sol1.py:20–43  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

18
19
20def 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
46def solution(number: int = 678910) -> int:

Callers 1

solutionFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected