/* Egg dropping problem in Picat. https://www.geeksforgeeks.org/egg-dropping-puzzle-dp-11/ """ The following is a description of the instance of this famous puzzle involving n=2 eggs and a building with k=36 floors. Suppose that we wish to know which stories in a 36-story building are safe to drop eggs from, and which will cause the eggs to break on landing. We make a few assumptions: …..An egg that survives a fall can be used again. …..A broken egg must be discarded. …..The effect of a fall is the same for all eggs. …..If an egg breaks when dropped, then it would break if dropped from a higher floor. …..If an egg survives a fall then it would survive a shorter fall. …..It is not ruled out that the first-floor windows break eggs, nor is it ruled out that the 36th-floor do not cause an egg to break. If only one egg is available and we wish to be sure of obtaining the right result, the experiment can be carried out in only one way. Drop the egg from the first-floor window; if it survives, drop it from the second-floor window. Continue upward until it breaks. In the worst case, this method may require 36 droppings. Suppose 2 eggs are available. What is the least number of egg-droppings that is guaranteed to work in all cases? The problem is not actually to find the critical floor, but merely to decide floors from which eggs should be dropped so that the total number of trials are minimized. Source: Wiki for Dynamic Programming """ Minimum number of trials in worst case with N eggs and K floors: For 2 eggs and 36 floors it's 8. For 2 eggs and 10 floors it's 4. This model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ main => go. go ?=> N=2, K=36, println([n=N,k=K,min=egg_drop(N,K)]), nl. go => true. % % Port of Python code: eddDrop/2 % go2 ?=> member(N,2..10), member(K,1..100), println([n=N,k=K,min=eggDrop(N,K)]), fail, nl. go2 => true. % % Functional approach, using egg_drop/2 % go3 ?=> member(N,2..10), member(K,1..100), println([n=N,k=K,min=egg_drop(N,K)]), fail, nl. go3 => true. % % Translation of the Python3 code % """ % Function to get minimum number of trials % needed in worst case with n eggs and k floors % """ table eggDrop(N,K) = T => % If there are no floors, then no trials % needed. OR if there is one floor, one % trial needed. if K == 1 ; K == 0 then T = K elseif N == 1 then % We need k trials for one egg % and k floors T = K else Min = maxint_small(), % Consider all droppings from 1st % floor to kth floor and return % the minimum of these values plus 1. foreach(X in 1..K) Res = max(eggDrop(N-1, X-1), eggDrop(N, K-X)), if Res < Min then Min := Res end end, T = Min + 1 end. % % Functional approach % table egg_drop(_,0) = 0. egg_drop(_,1) = 1. egg_drop(1,K) = K. egg_drop(N,K) = T => Min = maxint_small(), % Consider all droppings from 1st % floor to kth floor and return % the minimum of these values plus 1. foreach(X in 1..K) Res = max(egg_drop(N-1, X-1), egg_drop(N, K-X)), if Res < Min then Min := Res end end, T = Min + 1.