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

Function solution

project_euler/problem_686/sol1.py:46–152  ·  view source on GitHub ↗

This function calculates the power of two which is nth (n = number) smallest value of power of 2 such that the starting digits of the 2^power is 123. For example the powers of 2 for which starting digits is 123 are: 90, 379, 575, 864, 1060, 1545, 1741, 2030, 2226, 2515 and so o

(number: int = 678910)

Source from the content-addressed store, hash-verified

44
45
46def solution(number: int = 678910) -> int:
47 """
48 This function calculates the power of two which is nth (n = number)
49 smallest value of power of 2
50 such that the starting digits of the 2^power is 123.
51
52 For example the powers of 2 for which starting digits is 123 are:
53 90, 379, 575, 864, 1060, 1545, 1741, 2030, 2226, 2515 and so on.
54 90 is the first power of 2 whose starting digits are 123,
55 379 is second power of 2 whose starting digits are 123,
56 and so on.
57
58 So if number = 10, then solution returns 2515 as we observe from above series.
59
60 We will define a lowerbound and upperbound.
61 lowerbound = log(1.23), upperbound = log(1.24)
62 because we need to find the powers that yield 123 as starting digits.
63
64 log(1.23) = 0.08990511143939792, log(1,24) = 0.09342168516223506.
65 We use 1.23 and not 12.3 or 123, because log(1.23) yields only decimal value
66 which is less than 1.
67 log(12.3) will be same decimal value but 1 added to it
68 which is log(12.3) = 1.093421685162235.
69 We observe that decimal value remains same no matter 1.23 or 12.3
70 Since we use the function log_difference(),
71 which returns the value that is only decimal part, using 1.23 is logical.
72
73 If we see, 90*log(2) = 27.092699609758302,
74 decimal part = 0.092699609758302, which is inside the range of lowerbound
75 and upperbound.
76
77 If we compute the difference between all the powers which lead to 123
78 starting digits is as follows:
79
80 379 - 90 = 289
81 575 - 379 = 196
82 864 - 575 = 289
83 1060 - 864 = 196
84
85 We see a pattern here. The difference is either 196 or 289 = 196 + 93.
86
87 Hence to optimize the algorithm we will increment by 196 or 93 depending upon the
88 log_difference() value.
89
90 Let's take for example 90.
91 Since 90 is the first power leading to staring digits as 123,
92 we will increment iterator by 196.
93 Because the difference between any two powers leading to 123
94 as staring digits is greater than or equal to 196.
95 After incrementing by 196 we get 286.
96
97 log_difference(286) = 0.09457875989861 which is greater than upperbound.
98 The next power is 379, and we need to add 93 to get there.
99 The iterator will now become 379,
100 which is the next power leading to 123 as starting digits.
101
102 Let's take 1060. We increment by 196, we get 1256.
103 log_difference(1256) = 0.09367455396034,

Callers 1

sol1.pyFile · 0.70

Calls 1

log_differenceFunction · 0.85

Tested by

no test coverage detected