Lynn has a calculator with only two buttons that perform + 1 and ÷ 2 .
She also has a screen with 9 significant figures, and it displays 0 when she gets the calculator.
If she wants to display π up to the eighth decimal ( 3 . 1 4 1 5 9 2 6 5 ) , what is the fewest number of taps she needs to do?
Hint : The first 64 bits of π in binary are given below:
π
≈
11.00100100001111110110101010001000100001011010001100001000110100
2
Note : You may want to use the code environment below.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
As pointed out in the comments, this approach only shows that we can achieve 41 button presses. However, it has yet to explain why no fewer number of steps is possible, which is necessary to establish a minimum.
If you're interested in exploring this, try answering the question for the minimum number of taps to reach 1.0156 .
Playing devil's advocate, you have shown that it is doable in 41 button presses. How do we know that it cannot be doable in fewer, in order to conclude that this is indeed the minimum?
Log in to reply
Unfortunately, I do not know how to prove that this is the minimum.
Log in to reply
This is a slight technicality, but you have the tools and understanding to figure out how to deal with it.
Hint:
Supposed we wanted to display 1.0156 as the first 5 digits (possibly with trailing digits), how many steps will we need?
I can do it with less than 10
, whereas your approach will require ~15.
Log in to reply
@Calvin Lin – 1 . 0 1 5 6 is approximately 1 . 0 0 0 0 0 1 in binary, so reading this from right to left we have a 1 which is + 1 , ÷ 2 , then 5 0 s which is ÷ 2 each, and then another 1 in front of the decimal which is + 1 .
Therefore, 1 . 0 1 5 6 can be obtained on Lynn's calculator using my approach by entering + 1 , ÷ 2 , ÷ 2 , ÷ 2 , ÷ 2 , ÷ 2 , ÷ 2 , + 1 , which is 8 button presses.
Log in to reply
@David Vreken – Great observation! We do not want to use 1.00000001111..... . Instead, by using 1 . 0 1 5 6 2 5 = 1 . 0 0 0 0 0 1 2 , we can (greatly) cut down the number of steps. because the ending 1's are not "needed".
Now, figure out how to deal with the technicality.
Log in to reply
@Calvin Lin – Oh, I misunderstood your question. I see what you are asking now:
2 1 1 = 0 . 5 , 2 2 1 = 0 . 2 5 , 2 3 1 = 0 . 1 2 5 have an affect on the number immediately after the decimal because their decimal representations have a number in the tenth spot, but 2 4 1 = 0 . 0 6 2 5 (and all powers of 2 after this) do not, because 2 3 < 1 0 1 < 2 4 . Likewise, exponents 2 4 1 through 2 6 1 have a number in the hundredth spot, but 2 7 1 (and all powers of 2 after this) do not, because 2 6 < 1 0 2 < 2 7 . For a decimal representation to be accurate to 1 decimal place, in most cases we must be sure that that the 2 n d number is also accurate to deal with rounding, and so at most 6 binary decimals are needed, because 2 6 < 1 0 1 + 1 < 2 7 . Using this logic, in general, to be accurate to n decimal places, in most cases at most ⌊ lo g 2 n + 1 − 1 ⌋ binary decimals are needed.
However, the number of binary decimals needed can be shortened if the last few numbers in the ⌊ lo g 2 n + 1 − 1 ⌋ binary decimals are all 0 s or all 1 s. If the last few numbers are 0 s, then the binary representation can be truncated because the 0 s are unnecessary. If the last few numbers are 1 s, then 1 can be added to the ⌊ lo g 2 n + 1 − 1 ⌋ spot without changing the decimal representation value, and the addition will change all the last 1 s to 0 s and the last 0 to 1 , allowing another truncation because of unnecessary 0 s.
So for 1 . 0 1 5 6 , accurate to 4 decimal places, we would at most need ⌊ lo g 2 4 + 1 − 1 ⌋ = 1 5 binary decimals. However, since the first 1 5 decimal places of the binary of 1 . 0 1 5 6 is 1 . 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 , which ends in a sequence of 1 s, we can add 0 . 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 to get a shorter binary of 1 . 0 0 0 0 0 1 .
Likewise, for 3 . 1 4 1 5 9 2 6 5 , accurate to 8 decimal places, we would at most need ⌊ lo g 2 8 + 1 − 1 ⌋ = 2 8 binary decimals. However, since the the last 3 digits of the first 2 8 decimal places of the binary are 0 s, they can be truncated, which means only the first 2 5 binary digits are necessary.
Log in to reply
@David Vreken – Great (I didn't check the full details, but) it looks like you are on the right track. The idea is to consider all possible binary representations for values between 1.0156 and 1.0157, so that we can determine a lower bound for the minimum number of steps. Conversely, for this value, it is achieved. Hence we are done.
@Calvin Lin – With this problem, I was relieved to see 0's in the 26, 27, 28 position because it meant I wouldn't have to worry about rounding.
since to get a 11 requires 3 presses on +1, and press /2 will result 1.1, hence 4 presses. But the minimum press to get 1.1 is 3 presses, which is +1, /2, +1, hence the minimum press is always +1 then /1
or moving 1 to front from 0 with +1 requires 3 presses, but moving 1 from 0 to back only requires 2 presses (+1 then /2)
How do we come to conclude what binary representation is sufficient from the given 64 bit? Is there any trick to this or do I have to work it out?
Log in to reply
If you can figure out how many bits are needed (on average) to represent a decimal digit, then you can multiply that with the number of decimal digits and round up to get how many bits are needed. So, I don't know how to show this, but I think that you need log2(10) bits per decimal digit. So for the decimals after the decimal point I get round_up(8*log2(10)) = 27. Looking at the first 27 bits after the radix point we see that the last couple of bits are 0 and thus would make no difference if we ignored them so we end up needing the first 25 bits after the radix point. This is how I did it, it is far from a proof, so maybe it's wrong.
Why am I getting a different result on my calculator? On mine, tap #38 results in 0.14159266
Log in to reply
Count 38 ( Count 41 minus the last three 1s, here's what I got. Remember the last bit in the binary string has to be a '1'
Count: 41 Decimal: 3.14159265161 ( 3.14159265 ) Binary: 11.0010010000111111011010101 Buttons: +1 /2 /2 +1 /2 /2 +1 /2 /2 +1 /2 +1 /2 /2 +1 /2 +1 /2 +1 /2 +1 /2 +1 /2 +1 /2 /2 /2 /2 /2 +1 /2 /2 /2 +1 /2 /2 /2 +1 +1 +1
Why to use binary? In decimals, 3=1+1+1 == 3 taps; 1= (3+1)/2/2 == 3 taps; 4=1+1+1+1 == 3 taps; 1=4/2/4 == 2 taps; 5=1+1+1+1+1 == 4 taps; 9=5+1+1+1+1 == 4 taps; 2=(((9+1)/2+1)/2+1)/2 == 6 taps; 6=2+1+1+1+1 == 4 taps; 5=(6+1+1+1+1)/2 == 5 taps, total 34 taps
Log in to reply
Note: You are supposed to type out 3.14159265, not 3, 1, 4, 1, 5, 9, 2, 6, 5.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
Log in to reply
Can you run your code for 1.0156 ? What do you get?
Log in to reply
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Last bit in binary string has to be '1'
The two available functions on the calculator have simple effects when applied to binary numbers: the + 1 function adds 1 to the number before the point, and the ÷ 2 moves all the bits one to the right. For example, 0 . 1 0 1 + 1 = 1 . 1 0 1 and 1 . 1 0 1 ÷ 2 = 0 . 1 1 0 1 .
This means it is possible to produce such a number by considering the bits from the right end, pressing the + 1 button to produce a 1 , then pressing the ÷ 2 button to continue progressing along the bits towards the left. We can now calculate the total number of button presses to make such a number:
The binary number 1 1 . 0 0 1 0 0 1 0 0 0 0 1 1 1 1 1 1 0 1 1 0 1 0 1 0 1 is the shortest number that will display π to 8 decimal places.
There are 2 5 bits after the point, 1 3 individual 1 's after the point, and 3 + 1 presses needed to count to the 1 1 before the point, meaning a total of 2 5 + 1 3 + 3 = 4 1 button presses are required to produce the number.
Edit: To determine the shortest possible binary number, I used a brute force approach, taking a number known to produce π to 8 decimal places and then rounding it down/up to the next smallest bit until any further changes would no longer produce the correct decimal solution. In this case, 1 1 . 0 . . . 1 0 1 can be rounded either down to 1 1 . 0 . . . 1 ( 0 0 ) or up to 1 1 . 0 . . . 1 1 ( 0 ) , but these both produce π accurate to only 7 decimal places.
You have the right ideas here, but unfortunately are still missing a minor part.
Hint:
Supposed we wanted to display 1.0156 as the first 5 digits (possibly with trailing digits), how many steps will we need?
I can do it with less than 10
, whereas your approach will require ~15.
Log in to reply
Thanks for pointing this out to me. My approach would still work, I just neglected to state how I determined what the shortest possible solution would be.
A possibly more elegant additive method can also be used to generate the binary number. Some pseudocode: a=0; b=1/2; loop{ If (a+b ≥ 3.14159265 && a+b< 3.14159266){ return a+b; }else if(a+b<pi){ a=a+b; } b=b/2; }
I don't understand (and don't like) the binary solution, but knowing its around 38 steps to get the desired decimal part, by brute force i would do a combinatoric of 37 places, each with 3 options (+1, /2, and nothing)(the first step must be a +1 anyways; maybe more heuristics can be added) to see if we can have at least one positive, if not, 41 (38+3) would be the minimum.
Only the bits between 2 1 and 2 − 2 5 are necessary to get the needed precision. Starting from the last bit and working backwards, one needs 1 3 presses of the + 1 key, because that’s how many 1 ’s there are in the binary presentation of π with the desired accuracy, and 2 5 presses of ÷ 2 , to get to 0 . 1 4 1 5 9 2 6 5 1 6 0 5 6 1 … . Adding 1 three more times to get to π makes the answer 2 5 + 1 3 + 3 = 4 1 .
You are right to say that only those bits are necessary. It is true for a general number (to 8 decimals), but we might have additional redundency for a specific number.
Hint: Supposed we wanted to display 1.0156 as the first 5 digits (possibly with trailing digits). Must we use 2 − 1 0 ≈ 0 . 0 0 0 9 7 7 ? If no, then perhaps we could do better.
A truncated binary representation of π to the n th corresponds to a sequence of + 1 and ÷ 2 in the following way:
1 1 . 0 0 1 0 0 1 (in binary) = 2 1 ⋅ 1 + 2 0 ⋅ 1 + 2 − 1 ⋅ 0 + 2 − 2 ⋅ 0 + 2 − 3 ⋅ 1 + 2 − 4 ⋅ 0 + 2 − 5 ⋅ 0 + 2 − 6 ⋅ 1 (in decimal) = 1 + 1 + 1 + 2 1 ( 0 + 2 1 ( 0 + 2 1 ( 1 + 2 1 ( 0 + 2 1 ( 0 + 2 1 ( 1 + 0 ) ) ) ) ) ) = ( ( ( ( ( ( ( ( ( ( ( ( 0 ) + 1 ) ÷ 2 ) ÷ 2 ) ÷ 2 ) + 1 ) ÷ 2 ) ÷ 2 ) ÷ 2 ) + 1 ) + 1 ) + 1
Using a program, we can iterate through the i th truncation of π in binary, evaluate the corresponding decimal expression rounded to the eighth decimal, until we find one that matches 3 . 1 4 1 5 9 2 6 5 . An example of a program written in Elm is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
Remember that to show something is the minimum, you have to show that 1) It is attainable, 2) No smaller value is attainable.
Your program has done 1, but not 2.
Log in to reply
Ah, you are right. I have shown that assuming I'm using the algorithm of converting a truncated version of π in binary to a sequence of add1 and divBy2 operations, 4 1 is the minimum (since I'm iterating from 0 up, and outputting the first number n such that that the n -th binary truncation of π gives 3 . 1 4 1 5 9 2 6 in decimal). However, I have not shown that with another algorithm I cannot do it with fewer steps.
Needing ceiling log(10**9,2) binary digits to hold 8+1 decimal digits, the gross number of binary digits comes to ~29.9 (or 30 whole digits).
In the string the first 30 binary digits are .....10101000 (note the three trailing zeroes). The position of the rightmost 1 binary digit is at 25 fractional binary digits (30 - 2 (whole digits) - 3 (trailing zeros)). Since the next three (rightmost) are zeros, they do not need to be included in the button operations.
Counting the number of 1s in the fractional portion to be 13. The fractional binary 1s map to a +1 operation. Their position and each zero position all represent a division by 2 operation.
So we are at 13 + 25 operations for the fractional part.
For the whole portion, three +1s are needed (without any further divisions).
So total sum in this case are 13+25+3 = 41
C#/LINQ
// one-liner
Enumerable.Range(1,62).Select(n=>"00100100001111110110101010001000100001011010001100001000110100".Take(n).Reverse().Select(b=>b-'0').Aggregate((c:3,a:0.0),(s, b)=>(c:s.c+b+1,a:(s.a+b)/2))).Where(s=>s.a>0.14159265).Select(s=>s.c).First()
41
// to expand
Enumerable.Range(1,62) // for n = 1 to 62
.Select(n =>
"00100100001111110110101010001000100001011010001100001000110100" // pi minus 3
.Take(n) // take first n bits
.Reverse() // smallest bits first
.Select(bit => bit-'0') // turn char into 0/1
.Aggregate( // run through all bits
(clicks:3, acc:0.0), // calculator accumulator initially 0; 3 clicks represent the final 3 [+1] for integer part of pi
(state, bit) => (clicks:state.clicks+bit+1, acc:(state.acc+bit)/2) // if bit = 1 [+1][/2] else [/2]
)
) // result is 62 (clicks,acc) pairs
.Where(state => state.acc > 0.14159265) // filter those where acc is first 8 digits
.Select(state => state.clicks) // take #clicks
.First() // filter on first value - least as #clicks increases with n
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
I used round(num,8) == target first, so I got 42. I think this would be better, as calculators usually round the value instead of showing the first x digits. But I understood how the problem was meant, so got it for the second try.
Remember that to show something is the minimum, you have to show that 1) It is attainable, 2) No smaller value is attainable.
Your program has done 1, but not 2.
Hint: Run the program for 1.0156, and then try to do better than it .
Log in to reply
Calvin, can my reply be posted as a solution please?
Problem Loading...
Note Loading...
Set Loading...
The following sequence of 4 1 button presses will produce 3 . 1 4 1 5 9 2 6 5 on Lynn's calculator:
+ 1 , ÷ 2 , ÷ 2 , + 1 , ÷ 2 , ÷ 2 , + 1 , ÷ 2 , ÷ 2 , + 1 , ÷ 2 , + 1 , ÷ 2 , ÷ 2 , + 1 , ÷ 2 , + 1 , ÷ 2 , + 1 , ÷ 2 , + 1 , ÷ 2 , + 1 , ÷ 2 , + 1 , ÷ 2 , ÷ 2 , ÷ 2 , ÷ 2 , ÷ 2 , + 1 , ÷ 2 , ÷ 2 , ÷ 2 , + 1 , ÷ 2 , ÷ 2 , ÷ 2 , + 1 , + 1 , + 1
The binary representation 1 1 . 0 0 1 0 0 1 0 0 0 0 1 1 1 1 1 1 0 1 1 0 1 0 1 0 1 is sufficient to find π to 8 decimal places. The most efficient way to represent the numbers after the decimal on Lynn's calculator is to read from right to left and use + 1 , ÷ 2 for each 1 and ÷ 2 for each 0 . Since there are 1 3 1 s after the decimal at 2 button presses each, and 1 2 0 s after the decimal at 1 button press each, there are 1 3 ⋅ 2 + 1 2 ⋅ 1 = 3 8 total button presses to obtain the correct numbers after the decimal ( 0 . 1 4 1 5 9 2 6 5 ). Then an additional 3 + 1 buttons are necessary to add 3 more to obtain the correct answer of 3 . 1 4 1 5 9 2 6 5 , for a total of 3 8 + 3 = 4 1 button presses.