How to approach a number guessing game (with a twist) algorithm?

We'll combine graph-theory and probability:

On the 1st day, build a set of all feasible solutions. Lets denote the solutions set as A1={a1(1), a1(2),...,a1(n)}.

On the second day you can again build the solutions set A2.

Now, for each element in A2, you'll need to check if it can be reached from each element of A1 (given x% tolerance). If so - connect A2(n) to A1(m). If it can't be reached from any node in A1(m) - you can delete this node.

Basically we are building a connected directed acyclic graph.

All paths in the graph are equally likely. You can find an exact solution only when there is a single edge from Am to Am+1 (from a node in Am to a node in Am+1).

Sure, some nodes appear in more paths than other nodes. The probability for each node can be directly deduced based on the number of paths that contains this node.

By assigning a weight to each node, which equals to the number of paths that leads to this node, there is no need to keep all history, but only the previous day.

Also, have a look at non-negative-values linear diphantine equations - A question I asked a while ago. The accepted answer is a great way to enumarte all combos in each step.


Disclaimer: I changed my answer dramatically after temporarily deleting my answer and re-reading the question carefully as I misread some critical parts of the question. While still referencing similar topics and algorithms, the answer was greatly improved after I attempted to solve some of the problem in C# myself.

Hollywood version

  • The problem is a Dynamic constraint satisfaction problem (DCSP), a variation on Constraint satisfaction problems (CSP.)
  • Use Monte Carlo to find potential solutions for a given day if values and quantity ranges are not tiny. Otherwise, use brute force to find every potential solutions.
  • Use Constraint Recording (related to DCSP), applied in cascade to previous days to restrict the potential solution set.
  • Cross your fingers, aim and shoot (Guess), based on probability.
  • (Optional) Bruce Willis wins.

Original version

First, I would like to state what I see two main problems here:

  1. The sheer number of possible solutions. Knowing only the number of items and the total value, lets say 3 and 143 for example, will yield a lot of possible solutions. Plus, it is not easy to have an algorithm picking valid solution without inevitably trying invalid solutions (total not equal to 143.)

  2. When possible solutions are found for a given day Di, one must find a way to eliminate potential solutions with the added information given by { Di+1 .. Di+n }.

Let's lay down some bases for the upcoming examples:

  • Lets keep the same item values, the whole game. It can either be random or chosen by the user.
  • The possible item values is bound to the very limited range of [1-10], where no two items can have the same value.
  • No item can have a quantity greater than 100. That means: [0-100].

In order to solve this more easily I took the liberty to change one constraint, which makes the algorithm converge faster:

  • The "total quantity" rule is overridden by this rule: You can add or remove any number of items within the [1-10] range, total, in one day. However, you cannot add or remove the same number of items, total, more than twice. This also gives the game a maximum lifecycle of 20 days.

This rule enables us to rule out solutions more easily. And, with non-tiny ranges, renders Backtracking algorithms still useless, just like your original problem and rules.

In my humble opinion, this rule is not the essence of the game but only a facilitator, enabling the computer to solve the problem.

Problem 1: Finding potential solutions

For starters, problem 1. can be solved using a Monte Carlo algorithm to find a set of potential solutions. The technique is simple: Generate random numbers for item values and quantities (within their respective accepted range). Repeat the process for the required number of items. Verify whether or not the solution is acceptable. That means verifying if items have distinct values and the total is equal to our target total (say, 143.)

While this technique has the advantage of being easy to implement it has some drawbacks:

  • The user's solution is not guaranteed to appear in our results.
  • There is a lot of "misses". For instance, it takes more or less 3,000,000 tries to find 1,000 potential solutions given our constraints.
  • It takes a lot of time: around 4 to 5 seconds on my lazy laptop.

How to get around these drawback? Well...

  • Limit the range to smaller values and
  • Find an adequate number of potential solutions so there is a good chance the user's solution appears in your solution set.
  • Use heuristics to find solutions more easily (more on that later.)

Note that the more you restrict the ranges, the less useful while be the Monte Carlo algorithm is, since there will be few enough valid solutions to iterate on them all in reasonable time. For constraints { 3, [1-10], [0-100] } there is around 741,000,000 valid solutions (not constrained to a target total value.) Monte Carlo is usable there. For { 3, [1-5], [0-10] }, there is only around 80,000. No need to use Monte Carlo; brute force for loops will do just fine.

I believe the problem 1 is what you would call a Constraint satisfaction problem (or CSP.)

Problem 2: Restrict the set of potential solutions

Given the fact that problem 1 is a CSP, I would go ahead and call problem 2, and the problem in general, a Dynamic CSP (or DCSP.)

[DCSPs] are useful when the original formulation of a problem is altered in some way, typically because the set of constraints to consider evolves because of the environment. DCSPs are viewed as a sequence of static CSPs, each one a transformation of the previous one in which variables and constraints can be added (restriction) or removed (relaxation).

One technique used with CSPs that might be useful to this problem is called Constraint Recording:

  • With each change in the environment (user entered values for Di+1), find information about the new constraint: What are the possibly "used" quantities for the add-remove constraint.
  • Apply the constraint to every preceding day in cascade. Rippling effects might significantly reduce possible solutions.

For this to work, you need to get a new set of possible solutions every day; Use either brute force or Monte Carlo. Then, compare solutions of Di to Di-1 and keep only solutions that can succeed to previous days' solutions without violating constraints.

You will probably have to keep an history of what solutions lead to what other solutions (probably in a directed graph.) Constraint recording enables you to remember possible add-remove quantities and rejects solutions based on that.

There is a lot of other steps that could be taken to further improve your solution. Here are some ideas:

  • Record constraints for item-value combinations found in previous days solutions. Reject other solutions immediately (as item values must not change.) You could even find a smaller solution sets for each existing solution using solution-specific constraints to reject invalid solutions earlier.
  • Generate some "mutant", full-history, solutions each day in order to "repair" the case where the D1 solution set doesn't contain the user's solution. You could use a genetic algorithm to find a mutant population based on an existing solution set.)
  • Use heuristics in order find solutions easily (e.g. when a valid solution is found, try and find variations of this solution by substituting quantities around.)
  • Use behavioral heuristics in order to predict some user actions (e.g. same quantity for every item, extreme patterns, etc.)
  • Keep making some computations while the user is entering new quantities.

Given all of this, try and figure out a ranking system based on occurrence of solutions and heuristics to determine a candidate solution.


This problem is impossible to solve.

Let's say that you know exactly for what ratio number of items was increased, not just what is the maximum ratio for this.

A user has N fruits and you have D days of guessing.

In each day you get N new variables and then you have in total D*N variables.

For each day you can generate only two equations. One equation is the sum of n_item*price and other is based on a known ratio. In total you have at most 2*D equations if they are all independent.

2*D < N*D for all N > 2