this has been bugging me for a while.
Lets say you have a function f x y where x and y are integers and you know that f is strictly non-decreasing in its arguments,
i.e. f (x+1) y >= f x y and f x (y+1) >= f x y.
What would be the fastest way to find the largest f x y satisfying a property given that x and y are bounded.
I was thinking that this might be a variation of saddleback search and I was wondering if there was a name for this type of problem.
Also, more specifically I was wondering if there was a faster way to solve this problem if you knew that f was the multiplication operator.
Thanks!
Edit: Seeing the comments below, the property can be anything
Given a property g (where g takes a value and returns a boolean) I am simply looking for the largest f such that g(f) == True
For example, a naive implementation (in haskell) would be:
maximise :: (Int -> Int -> Int) -> (Int -> Bool) -> Int -> Int -> Int
maximise f g xLim yLim = head . filter g . reverse . sort $ results
where results = [f x y | x <- [1..xLim], y <- [1..yLim]]
Let’s draw an example grid for your problem to help think about it. Here’s an example plot of
ffor eachxandy. It is monotone in each argument, which is an interesting constraint we might be able to do something clever with.Since we don’t know anything about the property, we can’t really do better than to list the values in the range of
fin decreasing order. The question is how to do that efficiently.The first thing that comes to mind is to traverse it like a graph starting at the lower-right corner. Here is my attempt:
The signature is not as general as it could be (
Numshould not be necessary, but I needed it to negate the measure function because enumIncreasing returns an increasing rather than a decreasing list — I could have also done it with a newtype wrapper).Using this function, we can find the largest odd number which can be written as a product of two numbers
<=100:I wrote enumIncreasing using
meldable-heapon hackage to solve this problem, but it is pretty general. You could tweak the above to add additional constraints on the domain, etc.