Mitjitsu
He/Him
Banned User
Joined: 4/24/2006
Posts: 2997
In the game of golf what is the fastest speed a ball can travel over the hole with still ending up in the hole? Assume the ball has been putted, with no elevation applied and is travelling directly over the middle of the hole
Editor, Expert player (2078)
Joined: 6/15/2005
Posts: 3282
I'll post my solution to Riddler Classic here. The latest Riddler post also has a solution. To recall, the question is:
The Riddler wrote:
You have a gate that requires a passcode to open. There are 10 buttons: the digits 0 to 9. You have forgotten the passcode, but you remember it has four digits. You have no choice but to try them all. Since there are \(10^4\) = 10,000 four-digit passcodes, you might think this would take you 40,000 button presses to guarantee an opened gate. However, this gate’s keypad never resets: The gate opens as soon as the last four buttons you’ve pressed are the correct code, so you can be more efficient. For example, you can try two different codes by pressing just five buttons: The combination “12345” tries both “1234” and “2345.” Of course, pressed for time, you want to press as few buttons as possible while still trying different codes and eventually opening the gate. So the question is: What’s the smallest number of buttons you need to press to make sure you open the gate — i.e., that you’ve tried every possible four-digit combination?
You would expect that the minimum is 10003; we just need every 4-digit number to be a unique subsequence of a 10003-length sequence of digits. Indeed, it is possible to do so. It turns out that this problem has been well-studied before. Such a sequence is known a De Bruijn sequence. For example, the sequence 0000110100101111(000) is a De Bruijn sequence for n=4 and k=2; it contains all 16 4-digit binary sequences. Such a sequence always exists for given n and k; for n=1 it is trivial and for n>1 you take an Eulerian cycle on an (n-1)-dimensional De Bruijn graph (an Eulerian cycle exists on a strongly connected directed graph iff every vertex has equal indegree and outdegree). So for general n and k, the minimum number of button presses required is kn+(n-1). For the Riddler Classic problem, n=4 and k=10, and this gives 10003. Now unfortunately such a sequence isn't easy to memorize (such as the one given by the latest Riddler post); I thought I had such a sequence but it turns out I made an error. So about whether there is such an easily memorizable sequence, I will leave this one unanswered.
Mitjitsu wrote:
In the game of golf what is the fastest speed a ball can travel over the hole with still ending up in the hole? Assume the ball has been putted, with no elevation applied and is travelling directly over the middle of the hole
This seems like a physics question; did you mean to post it on the physics questions thread? In any case I think we'd need more details.
Banned User
Joined: 3/10/2004
Posts: 7698
Location: Finland
I was wondering if there exists any pattern or formula for all square numbers that can not be represented in these two forms, where a, m and n are natural numbers, and n>1: a2 = m2n Rather obviously there are square numbers for which that formula holds (such as 162=28). There are also square numbers for which it doesn't (the smallest one being 4.)
Amaraticando
It/Its
Editor, Player (159)
Joined: 1/10/2012
Posts: 673
Location: Brazil
Warp wrote:
I was wondering if there exists any pattern or formula for all square numbers that can not be represented in these two forms, where a, m and n are natural numbers, and n>1: a2 = m2n Rather obviously there are square numbers for which that formula holds (such as 162=28). There are also square numbers for which it doesn't (the smallest one being 4.)
If I understood well the problem, hint: a2 = m2n = (mn)2 Edit: what two forms?
Player (36)
Joined: 9/11/2004
Posts: 2630
@Warp, essentially the criteria for a is that it must be squarefree. A list of these numbers may be found in the OEIS. https://oeis.org/A005117
Build a man a fire, warm him for a day, Set a man on fire, warm him for the rest of his life.
Banned User
Joined: 3/10/2004
Posts: 7698
Location: Finland
Are you sure it's the squarefree numbers? 12 is not squarefree (because it's divisible by 4), but I can't find any whole numbers m and n>1 for which m2n = 122 = 144
Player (36)
Joined: 9/11/2004
Posts: 2630
Ah, this is true. Forgive me. If we look at Amaraticando's solution, (all numbers in the form of m^n), then: 4 = 2^2, 8 = 2^3, 9 = 3^2, 16 = 2^4 = 4^2, 25 = 5^2, 27 =3^3 https://oeis.org/A001597 So the squares of these numbers can be represented, so the complement can not. https://oeis.org/A007916 And then simply square those: https://oeis.org/A153158
Build a man a fire, warm him for a day, Set a man on fire, warm him for the rest of his life.
Editor
Joined: 11/3/2013
Posts: 506
On the golf ball question: Model the hole as a square well. Ignore any rotational motion the ball has (assume it is simply sliding). Assume the collision with the far side of the hole is perfectly elastic. Under these conditions, the balls drops into the hole if the angle between the ball's radius and the side of the hole is less than 135o. This happens when the ball drops at least a height h before hitting the back wall, where h = d*(1-1/sqrt(2))/2. It must drop this distance before the ball hits the brim on the far side of the hole - to a first-order approximation the ball travels a distance D but for full accuracy it travels a distance x = D - d/2 + h. (Difficult to explain this formula without being able to draw a diagram, so I leave this as an exercise for the reader.) To accelerate a distance h, requires a time t = sqrt(2h/g). And therefore a speed v = x/t = (D - d/2 + h)*sqrt(g/2h). Now substituting in our expression for h, the critical speed v = (D-d/2+d*(1-1/sqrt(2))/2)*sqrt(g/2d*(1-1/sqrt(2))/2). Putting in typical values (D = 0.05m, d = 0.03m, g = 9.8ms-2) gives a speed of 0.57ms-1. Seems a bit on the slow side to me, but that's where a bunch of assumptions come in (the bounce will not be perfectly elastic, which will allow for a higher approach speed, for example).
Player (42)
Joined: 12/27/2008
Posts: 873
Location: Germany
Find all positive integers a and b, with a > b, such that ab=ba.
Banned User
Joined: 3/10/2004
Posts: 7698
Location: Finland
p4wn3r wrote:
Find all positive integers a and b, with a > b, such that ab=ba.
ab/b = ba/b a = ba/b a1/a = ba/(a*b) a1/a = b1/b Can that equality hold for any a≠b?
Editor, Skilled player (1344)
Joined: 12/28/2013
Posts: 396
Location: Rio de Janeiro, Brasil
Warp wrote:
p4wn3r wrote:
Find all positive integers a and b, with a > b, such that ab=ba.
ab/b = ba/b a = ba/b a1/a = ba/(a*b) a1/a = b1/b Can that equality hold for any a≠b?
Yes, it can: limit of x^(1/x) when x -> 0 is 0 limit of x^(1/x) when x -> inf is 1 and maximum of x^(1/x) is e^(1/e) at x = e. So for every 1<y<e^(1/e), x^(1/x) = y has two solutions for x.
My YouTube channel: https://www.youtube.com/channel/UCVoUfT49xN9TU-gDMHv57sw Projects: SMW 96 exit. SDW any%, with Amaraticando. SMA2 SMW small only Kaizo Mario World 3
Banned User
Joined: 3/10/2004
Posts: 7698
Location: Finland
BrunoVisnadi wrote:
So for every 1<y<e^(1/e), x^(1/x) = y has two solutions for x.
So one would need to find all pairs of integers for which a1/a = b1/b, a≠b, or prove that the equality never holds for integers?
Editor, Expert player (2078)
Joined: 6/15/2005
Posts: 3282
p4wn3r wrote:
Find all positive integers a and b, with a > b, such that ab=ba.
Taking logarithms of both sides and rearranging, we get ln(a)/a=ln(b)/b (seen this before?). Taking the derivative of f(x)=ln(x)/x, we see that f(x) increases on the interval (0,e), decreases on (e,∞), and has maximum at x=e. Since f(x) is positive exactly when x>1, it follows that 1<b<e<a. Since b is an integer, this gives b=2. There is only one possible a corresponding to b, and a=4 (since 42=24). So the only solution is a=4 and b=2.
Player (42)
Joined: 12/27/2008
Posts: 873
Location: Germany
I have a solution for that problem without any use of derivatives, only number theory! Take ln of both sides and rearrange to get a/b = ln a/ln b = loga b (*) Since a and b are integers, that means the logarithm of a on the base b is a rational number. That means that, for some integers n and m (assume WLOG they are coprime): an/m=b => an = bm Now apply unique factorization to both a and b. The previous identity requires that, for every prime factor p, there is an exponent k such that pkn | a, pkm | b By merging all prime factors in the unique factorization, we can write a and b in function of some integer d as: a = dn, b = dm Substituting into (*): dn-m = n/m Now, since a > b, then n>m, and the LHS is an integer, so the RHS must also be integral, so m | n. But gcd(n,m)=1, therefore m=1: dn-1 = n (**) I'll show this equation has no solution for n >= 3. If d=1, then a=b, so d >= 2. That implies dn-1 >= 2n-1 > n The last step can be proven with induction. For n=3, it's true, since 4 > 3. Suppose it holds for n, then it holds for n+1: 2n-1 > n => 2*2n-1 = 2n > 2n = n + n > n + 1. So, there are no solutions for n >=3. If n=1, then a=b, so the only possibility is n=2. Plugging it into (**), we have d=2, and it follows that a=4 and b=2 is the only solution!
Banned User
Joined: 3/10/2004
Posts: 7698
Location: Finland
For some reason there seem to exist two slightly different versions of the Fundamental Theorem of Arithmetic. For example, this numberphile video gives the following definition: "Every positive whole number can be written as a unique product of primes." This website gives a similar definition: "Every natural number is either prime or can be factored as a product of primes in a unique way." On the other hand, most other websites have a slightly altered definition. For example: "For every integer n≥2, it can be expressed as a product of prime numbers." Or: "Every integer n≥2 can be factored into a product of primes in exactly one way (aside from rearranging the factors)." Some definitions include the requirement that the number be greater than 1, others don't. Which one is correct? The host of the numberphile video points out that the definition of the theorem seems to imply that 1 isn't a whole number (because surely it can't be written as a product of primes). The answer given to this is that 1 is the result of the empty product. I suppose this makes sense when you consider that every natural number can be expressed as: n = 2p1⋅3p2⋅5p3⋅7p4⋅11p5⋅13p6⋅... as a combination of non-negative integer values of p1, p2, p3, etc. Nothing stops all of the powers from being zero (which is, thus, the unique prime factorization of 1).
Amaraticando
It/Its
Editor, Player (159)
Joined: 1/10/2012
Posts: 673
Location: Brazil
1 is not prime and should not be by definition, because it would mess a lot of theorems about prime numbers. 1 is not divisible by any prime number. Therefore, the 1st theorem is wrong, they simply forgot to mention >= 2. "The answer given to this is that 1 is the result of the empty product" There's no such a thing. The FTA must also include the uniqueness of the product: https://en.wikipedia.org/wiki/Fundamental_theorem_of_arithmetic#Canonical_representation_of_a_positive_integer
Editor, Expert player (2078)
Joined: 6/15/2005
Posts: 3282
Warp wrote:
The answer given to this is that 1 is the result of the empty product. I suppose this makes sense when you consider that every natural number can be expressed as: n = 2p1⋅3p2⋅5p3⋅7p4⋅11p5⋅13p6⋅... as a combination of non-negative integer values of p1, p2, p3, etc. Nothing stops all of the powers from being zero (which is, thus, the unique prime factorization of 1).
Yes, that's right. The convention that an empty product equals 1 is explained on Wikipedia here. All the statements you gave are equally correct (if not completely precise). It's just that the first two use the convention of empty product equal to 1, and the others choose not to use that convention.
Editor, Expert player (2078)
Joined: 6/15/2005
Posts: 3282
I'm working on the Riddler Classic problem this week, worded as follows: ---- An enemy submarine determined to sink your ship is sitting, torpedoes armed, exactly halfway between you and your home port. The submarine submerges to some fixed depth, and your ship now has no further information about its position. The enemy sub has to be directly underneath your ship to sink it — but the sub can track your moves with precision and respond efficiently. If your ship is fast enough, though, you will be able to set a wide course around the sub and reach port safely. How much faster than the sub does your ship have to be to guarantee you can avoid the sub and get home? ---- Because your ship has no information about the sub's position except its starting point, the only way to guarantee that your ship reaches home port safely is to take a patch to home that avoids an increasing circle (centered on the sub's initial position) representing where the sub could be. So your ship has to be at least twice as fast (it has to reach home port before the increasing circle does), and your ship can easily reach home port if it is pi times as fast (by going along a 180-degree circular path to home port). So the true answer is somewhere in between. I'm currently trying to figure out what the path looks like (it depends on how much faster your ship is) but the problem seems somewhat difficult. Perhaps it can be solved like the fox and duck problem that we figured out before.
Player (42)
Joined: 12/27/2008
Posts: 873
Location: Germany
Thinking about this problem, I worked out an approach to construct the path, but I'm not sure if it leads to a differential equation for the curve. Consider the half-circle with a ship that's pi times faster. It works because, as the ship passes through the circle, the path is always at some distance d from the submarine's initial spot. No matter on which direction the submarine starts to move, it can only reach the edge of the circle when you already have reached the port. However, you can see that this is suboptimal. For example, suppose the ship takes a counter-clockwise turn, and the submarine decides to travel above. When the ship is at the highest point in the trajectory, the sub is only halfway through the circle radius. Clearly, the ship could have taken a path that passes lower and still not be reached by the submarine. If this path is shorter, it does not need to be so fast. That suggests a condition for the curve. Take any angle theta made between the ship-sub-port line and a ray shooting out of the sub's initial location. The optimal path should be the one where, if the sub decides to travel on the angle theta, it will meet the path at exactly the point in time where the ship is passing through it. Obviously this cannot hold for the entire range [0,pi] of the angle theta. When the angle is 0, the distance between ship and sub should be 0, and it's not. So, we probably need to splice the curve with this property with a straight line that's tangent to it. Now, consider that the angle theta where the ship is at a given time t (you can normalize the time interval to [0,1]), this function might not be linear When the ship passes through the curve at a constant velocity, its angular velocity is not constant if the shape is too weird. Now, at every angle theta, the distance from the sub's initial spot to the curve should be: r(theta) = R*theta(t). It looks to me the problem can be reduced to this: 1) Take a curve defined in arc-length parametrization (call the length parameter s). 2) Compute the angle swept by the curve in terms of s. 3) Impose the restriction that the distance between the sub's initial position is proportional to that angle. 4) That will reduce the problem to a differential equation, solve it. 5) The resulting curve cannot be the answer, since it starts at the sub's initial position. Splice it with a straight line that's tangent to the path when it intersects it. It seems that polar coordinates is the most suitable system to try to solve this, but I haven't got the time to do the calculations yet. I'll update this post when I do it. EDIT: The calculations are complicated, and the splicing procedure is more complicated thant I thought before. I still haven't found the optimal solution, but I at least managed to prove that the ship can dodge the sub if it is 3 times faster. It turns out that the curve satisfying the differential equation is rather simple. It is just r(theta) = R*Exp[k*theta], for some arbitrary constants R and k. To solve the problem you need to 1) Choose an angle Theta. 2) Draw a straight line from the sub's starting point making having this angle Theta, going to the side of the ship's starting position. 3) Draw the straight line from the ship's starting point that makes a right angle with the line you drew on step (2). 4) Now trace the curve satisfying r(theta) = R*Exp[k*theta] that begins at the intersections of lines (2) and (3) and ends at the port. 5) Find the angle theta that gives the optimal speed ratio. I haven't done (5) yet, but I managed to get the speed ratio under 3. To illustrate, I have done the case where Theta = pi/4, in this widget: http://www.wolframalpha.com/widget/widgetPopup.jsp?p=v&id=8d8e2c27bcaa121d6ee0de4b98774bb4&title=Polar%20Graphs&theme=blue&i0=Exp[%284%28Theta-Pi%29%29*Log[Sqrt[2]]%2F%283Pi%29]&i1=Pi%2F4&i2=Pi&podSelect=&showAssumptions=1&showWarnings=1 Assume the graph above is in meters. By following the straight line to the point where the curve begins, the ship needs to move 0.707 m. If the ship is at 3 m/s, it can do this at 0.236 s. The distance from this straight line to the sub's initial point is 0.707 m, so the sub cannot reach it within this time. Wolfram Alpha tells us the curve length is 2.013 m. At 3 m/s, the ship will complete it within 0.671 s. That gives the sub 0.907 s to reach the curve. By construction, the time it takes the sub to reach the ship at any point in the curve is the same it takes for it to reach the port, which is 1 s. Therefore, the sub cannot catch the ship as it is moving along the curve, and it suffices that the ship is 3 times faster.
Player (42)
Joined: 12/27/2008
Posts: 873
Location: Germany
All right, I solved the problem. I'm making another post to make a cleaner solution. The ship's path consists of two parts: a straight line followed by a curve. We first discuss the properties of this curve. Name the submarine's starting position O, and the ship's starting position P. Suppose the ship travels from P to a point A in a straight line, and then follows a curve to the port's location, at point F. The curve we want has the property that, for every point B in the curve AF, we have OB = OA + c*AB (*) where c is a given constant. Obviously, c<1. Or else, not even a triangle could satisfy the identity. Why? This essentially says that, no matter which direction the submarine takes, it will always meet the ship at the exact point, provided that it's traveling at a constant velocity 1/c relative to the sub. Now, the curve that has these properties is (I've found it using differential equations, but you can just use clairvoyance, throw the formula and show that it works), in polar coordinates centered at O: r(theta) = R*Exp[k*theta]. To prove that it does have property (*), simply use the arc length formula, being careful to place the differentials in polar coordinates, and to start integrating from an initial angle theta_i, which corresponds to the line OA. The result of this integration is essentially AB = sqrt(1+k²)*(OB-OA)/k, rearranging we get to (*), where we find c=k/sqrt(1+k²), and the ship velocity that works with this curve is simply sqrt(1+1/k²). Now, we want to relate k to the angle POA. Call this angle a. Now, OA = OP*cos(a), and AP=OP*sin(a). So, in this situation, our curve would begin at theta=a at a distance from O given by OP*sin(a) and finish at theta=pi with a distance given by OP. That means ek(a-pi)=cos a => k = ln(cos(a))/(a-pi). (**) Furthermore, before getting to point A, the ship needs to move OP*sin(a), while the sub needs to move OP*cos(a). So, the ship needs a relative velocity of at least tan(a) to make the course. Substituting (**), we find that the minimum velocity in terms of a to make the curve is sqrt(1+((a-pi)/ln(cos(a)))²). So, when a given a is chosen, the ship's relative velocity must be higher than this function and also tan(a) to successfully make the path. Finally, if you plot/derive the complicated function, you'll see it's decreasing between 0 and pi/2, while tan(a) is increasing in this range. So, the absolute minimum is when the graphs of these functions cross each other. Solving numerically, that happens at around a=1.167, which gives a relative velocity of approximately v=2.340 EDIT: As I expected, the curve that helps solve the problem is well-known and has a cute name. It's called the logarithmic spiral, apparently.
Editor, Expert player (2078)
Joined: 6/15/2005
Posts: 3282
Looks pretty good, p4wn3r. You got a numerical value that is very close to what I got. I figured out the exact value of the speed that your ship has to go compared to the sub (relative velocity). It is sqrt((1/x2) + 1), where x is the only solution to -ln(x)/x=pi/2. WolframAlpha gives x≈0.474541 and so that gives an answer of sqrt((1/x2) + 1 ≈ 2.332533. How I did this: Model the problem such that your ship starts at (-1,0), the sub starts at (0,0), and the port is at (1,0). Assume the sub has speed 1 and your ship has speed b. Note that b is the answer to this problem. The ship's path, as p4wn3r guessed, is indeed a line from (-1,0) meeting the sub's circle expanding away from (0,0), followed by a logarithmic spiral to the port at (1,0). Now the transition point where the line and the expanding circle meet can't just be anywhere, but must be such that your ship's radial speed at that point must be 1 outward, since the circle is expanding outward at a speed of 1. I claim that this point must lie on the y-axis. Indeed, the following image shows why: At the transition point C, your ship's radial speed is 1 and, with the ship's velocity vector having a magnitude of b, we form right triangle EDC. However, if we call distance BC as r, then the distance of AC is br. Since points A,C,E are collinear as well as B,C,D, the two triangles ABC and EDC are similar, so ABC is a right triangle with legs of 1 and r and hypotenuse br. Note that as your ship travels along AC, the sub's circle grows along BC with the same y-coordinate, so point C is indeed the first point where they meet. By using the Pythagorean Theorem and rearranging, we get our first identity 1/r = sqrt(b2-1). (*) Now after your ship meets the edge of the sub's circle, your ship must always maintain a radial speed of 1 while having a speed of b. Thus the tangential speed is sqrt(b2-1), which is 1/r from the identity (*), which depends only on b. (Note that constant radial and tangential velocities at every point is a characteristic of a logarithmic spiral.) Since the tangential speed is distance times angular speed, we get 1/r = x*θ'(x), where θ'(x) is the angular speed and x is the distance travelled by the sub (alternatively, the radius of the sub's circle). So θ'(x) = 1/r * 1/x, so then by integration, θ2 - θ1 = 1/r * (ln(x2) - ln(x1)). From the above image, we know that your ship must travel this spiral through the angle CBF, which has an angle of θ2 - θ1 = pi/2. Now the initial distance of the spiral is x1=r and the final distance is x2=1, so this simply gives pi/2 = 1/r * (-ln(r)). Then we simply solve for the variables r and b. WolframAlpha gives r≈0.474541, and plugging into (*) and solving gives b≈2.332533, which is our answer. The following image (created from desmos.com) shows the path your ship must take at speed b≈2.332533 that allows you to reach port safely. The blue part is the straight line and the red part is the logarithmic spiral. Note that the transition between blue and red occurs at the point (0,r) ≈ (0,0.474541).
Player (42)
Joined: 12/27/2008
Posts: 873
Location: Germany
Nice work! I think I can explain the small difference. I have wrongly assumed that when the ship was traveling in a straight line, it would always be better for the sub to try to catch it at the direction of the height of your triangle ABC. With your solution, I can see that I was not that careful :) That mistake caused a small inaccuracy in the estimation of the velocity for the line segment. I am amazed by how simple the solution looks in terms of the velocity. At the first quadrant, use 1 unit of your higher velocity to outrun the ship at the perpendicular axis. At the second quadrant, do the same thing, but now turn the velocity vector radially! That's a much more elegant way to arrive at the logarithmic spiral!
Player (42)
Joined: 12/27/2008
Posts: 873
Location: Germany
I'm now trying the other problem:
Riddler wrote:
I was recently traveling in Europe and struck by the number of coins the euro uses. They have 2 euro, 1 euro, 50 cent, 20 cent, 10 cent, 5 cent, 2 cent and 1 cent coins. This got me thinking: If Riddler Nation needed to make change (anywhere from 0.01 to 0.99) and was establishing its own mint, what values of coins would be ideal to yield the smallest number of coins in any transaction? When picking values, let’s say we’re ditching the Europeans and limiting our mint to four different coin denominations — replacing the current common American ones of penny, nickel, dime and quarter.
The most immediate idea would be to use as coin denominations the powers of an integer. Within this method, the powers of 3 (1,3,9,27) are the optimal ones. The numbers that take the largest amount of coins are 80 and 98: 80 = 2*27 + 2*9 + 2*3 + 2*1 -> 8 coins 98 = 3*27 + 1*9 + 2*3 + 2*1 -> 8 coins I was trying to prove that this one is optimal, but it turns out that it's not. I found another one that manages to make any change with a total of seven coins. It is: 1,4,5,21 That happens because 1 = 1 2 = 1+1 3 = 1+1+1 4 = 4 5 = 5 6 = 5 + 1 7 = 5 + 1 + 1 8 = 4 + 4 9 = 5 + 4 10 = 5 + 5 11 = 5 + 5 + 1 12 = 4 + 4 + 4 13 = 5 + 4 + 4 14 = 5 + 5 + 4 15 = 5 + 5 + 5 16 = 4 + 4 + 4 + 4 17 = 5 + 4 + 4 + 4 18 = 5 + 5 + 4 + 4 19 = 5 + 5 + 5 + 4 20 = 5 + 5 + 5 + 5 It turns out that you can write any number n from 1 to 99 using the division algorithm as n = k*21 + m. If k <=3, then 0 <= m <= 20, and from the previous table we can get the value of m using at most 4 coins. With the other k coins with value 21, we write any n using at most 7 coins. If k=4, then 0 <= m <= 15, and since any number up to 15 in the previous table can be represented with at most 3 coins, that also works to represent n with at most 7. I've been trying to prove that it's impossible to represent any value from 1 to 99 with 4 coin types using only 6 coins or less (which would prove the optimality of this solution), but haven't had success yet. EDIT: It looks like not even 7 is the optimal. It's possible to get 6! To do this I used a computer program, though. The quick and dirty code below gets the job done. There are many solutions that get 6. The first one that appears is (1,4,13,29). When target is set to 5, no solutions appear:
Language: cpp

#include <stdio> #include <algorithm> using namespace std; int v[4][100]; int main() { int target = 6; for (int i=0; i<=99; i++) { v[0][i] = i; } v[1][0] = v[2][0] = v[3][0] = 0; for (int c1=2; c1<=99; c1++) { for (int i=1; i<99>= 0) { v[1][i] = min(v[0][i],v[1][i-c1]+1); } } for (int c2=c1+1; c2<=99; c2++) { for (int i=1; i<99>= 0) { v[2][i] = min(v[1][i],v[2][i-c2]+1); } } for (int c3=c2+1; c3<=99; c3++) { bool found = true; for (int i=1; i<99>= 0) { v[3][i] = min(v[2][i],v[3][i-c3]+1); } if (v[3][i] > target) { found = false; break; } } if (found) { printf("1 %d %d %d\n",c1,c2,c3); } } } } return 0; }
Editor, Expert player (2078)
Joined: 6/15/2005
Posts: 3282
p4wn3r wrote:
Language: cpp

for (int i=1; i<99>= 0)
I have never seen this type of code before in a for statement. Specifically the i<99>= 0 part. I was unable to get it to compile. The Riddler doesn't always specify things clearly, so I did the version where you minimize the average, as opposed to minimizing the worst case (maximum), as p4wn3r did. The code is based on p4wn3r's code and is as follows:
Language: cpp

#include <stdio.h> #include <algorithm> using namespace std; int v[4][100]; int main() { for (int i=0; i<=99; i++) { v[0][i] = i; } v[1][0] = v[2][0] = v[3][0] = 0; for (int c1=2; c1<=99; c1++) { for (int i=1; i<=99; i++) { if(i<c1) v[1][i] = v[0][i]; else v[1][i] = min(v[0][i],v[1][i-c1]+1); } for (int c2=c1+1; c2<=99; c2++) { for (int i=1; i<=99; i++) { if(i<c2) v[2][i] = v[1][i]; else v[2][i] = min(v[1][i],v[2][i-c2]+1); } for (int c3=c2+1; c3<=99; c3++) { for (int i=1; i<=99; i++) { if(i<c3) v[3][i] = v[2][i]; else v[3][i] = min(v[2][i],v[3][i-c3]+1); } int score=0, max=0; for(int i=1; i<=99; i++) { score=score+v[3][i]; if(v[3][i]>max) max=v[3][i]; } if(score<=389) { printf("1 %d %d %d max:%d sum:%d\n",c1,c2,c3,max,score); } } } } return 0; }
(To solve for minimizing the worst case, replace if(score<=389) with if(max<=6).) The result is:
1 5 18 25  max:6  sum:389
1 5 18 29  max:6  sum:389
So both (1,5,18,25) and (1,5,18,29) give the minimum average of 3.89 coins. Note that both of these also minimize the worst case of 6 coins.
Tub
Joined: 6/25/2005
Posts: 1377
p4wn3r wrote:
EDIT: It looks like not even 7 is the optimal. It's possible to get 6! To do this I used a computer program, though. The quick and dirty code below gets the job done. There are many solutions that get 6. The first one that appears is (1,4,13,29). When target is set to 5, no solutions appear:
Your code got mangled there, but it's easy enough to see the dynamic programming invariant. It's worth noting that these solutions don't allow people to use the greedy strategy when giving change: just keep tossing the highest-valued coin that's smaller than the remaining change. 98 = 29 + 29 + 29 + 4 + 4 + 1 + 1 + 1 (8 coins) as opposed to 98 = 29 + 29 + 13 + 13 + 13 + 1 (6 coins) The riddle specifically asks to optimize without regard to anything else, but any actual currency I know does make sure that the greedy strategy is optimal. The best solutions appear to be (1,5,18,25) or (1,5,18,29); of all the solutions with a maximum of 6 coins, these have the lowest average coins (3.89). /edit: I got a bit ninja'd there. A bit disappointing, but not entirely unexpected: allowing coins with negative values does not yield any better solutions, though (-1,2,11,28), (-55,8,13,15) and several others work with 7 coins.
m00