Вы находитесь на странице: 1из 3

Here are some powers of two: 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024.

We want to find patterns that allow us to predict digits. Starting at the beginning, we have the units digits. 2, 4, 8, 6, 2, 4, 8, 6, 2, 4 our pattern is established. Now the tens digits: 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024. We know that when we get a tens digit, for every next step, it is going to be multiplied by two. But sometimes it gets x2+1, where the 1 comes from the units digits, which we know and have already computed. When do we get a +1? From these: 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024. The numbers in bold represent when the tens digit does not just double, but gets a +1 from the units digit. Lets look at the units: 2, 4, 8, 6, 2, 4, 8, 6, 2, 4. The signal we observe is that the units give a +1 whenever the digit in the sequence decreases. So for instance, to go from 8 to 6, we went from 8 to 16. Then 6->2 came from 16*2 = 10*2 + 6*2 = (20+10)+2 = 32. But the next step, 32 to 64, did not involve a units digit decreasing (2 to 4), and so the tens digit only doubled. We can easily verify that units digits will decrease iff they provide a +1 to the tens digit. We can find the units digits by creating an array where the first position is 2, and the next position is (prior position * 2) mod 10. We do this with an array of size n (for this problem it would be 1000).
def twos(n): array=[]; array.append(2); for i in range(1,n): array.append(array[i-1]*2%10) return array;

So this would give us the sequence of units digits of 2^k, k=1, 2, , n. We then build the tens digits by creating an array of 0s of the same size. We run through the units digits and build an indicator array telling us whether the units digits contribute a +1 or not that term. Then, because the tens digit array is initialized with zeros, we can run through it like this: tens digit in position n = (value in position n-1, multiplied by two, plus the value of the indicator array of the units digit, all mod 10). The array would have all zeros until the first hit on the indicator array, when it would then take value 1 (8->16). Then 16->32 produces another hit, because 2<6, so the tens digit becomes prior*2+1 mod 10 = 1*2+1 mod 10 = 3. 32->64 doesnt produce a hit, so the tens digit just becomes the prior value doubled. The real magic is that this process that builds the tens digits from the units digits can be used to build the hundreds digits from the tens digits, at which point you can scale it to solve the problem. The

hundreds digits are initialized at zero, double at each turn, and receive a +1 whenever the tens digits decrease. Actually, there is a nuance to it: you also get a +1 if the prior digit goes 9->9. It isnt an issue for units digits, but it matters in all other cases. E.g. 4096->8192. The tens digit doesnt decrease, but it still produces a +1. So the +1 condition becomes prior digit either decreases or stays at value 9.
def next(prior): array=[0]*len(prior); add=[0]; for x in range(1,len(prior)): add.append(int(prior[x]<prior[x-1] or prior[x]==prior[x-1]==9)) for y in range(1,len(array)): array[y]=(array[y-1]*2+add[y])%10 return array;

At this point you just build arrays of length n for 2^n, with one array for each digit. If you consider the final product expressed in scientific notation, you will see that floor(base ten logarithm)+1 will give you the number of digits (this resolves to 1000*log(2)+1). So make a big array of all these digit arrays, then when youve finished building the tens digits you can replace the units digit array with its final value (the final units digit) and build the hundreds digit, after which you replace the tens digit array with its final value, etc. Then finish off the final array and reverse the big array and sum.
def number(n): digits=int(n*math.log(2,10))+1; bigarray=[0]*digits; bigarray[0]=twos(n); for x in range(1,digits): bigarray[x]=next(bigarray[x-1]); bigarray[x-1]=bigarray[x-1][n-1]; bigarray[x]=bigarray[x][n-1]; bigarray.reverse(); return bigarray

All of the arrays in bigarray if I kept the digit arrays as they were:

Вам также может понравиться