Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number distribution

Problem: We have x checkboxes and we want to check y of them evenly.

Example 1: select 50 checkboxes of 100 total.

[-]
[x]
[-]
[x]
...

Example 2: select 33 checkboxes of 100 total.

[-]
[-]
[x]
[-]
[-]
[x]
...

Example 3: select 66 checkboxes of 100 total:

[-]
[x]
[x]
[-]
[x]
[x]
...

But we're having trouble to come up with a formula to check them in code, especially once you go 11/111 or something similar. Anyone has an idea?

like image 648
Carra Avatar asked Nov 28 '11 16:11

Carra


People also ask

What is a distribution of numbers?

In algebra and number theory, a distribution is a function on a system of finite sets into an abelian group which is analogous to an integral: it is thus the algebraic analogue of a distribution in the sense of generalised function.

How do you define distribution?

Definition: Distribution means to spread the product throughout the marketplace such that a large number of people can buy it.

Why do 30% of numbers start with 1?

Benford's law (also called the first digit law) states that the leading digits in a collection of data sets are probably going to be small. For example, most numbers in a set (about 30%) will have a leading digit of 1, when the expected probability is 11.1% (i.e. one out of nine digits).

Is Benford's Law true?

Benford's Law holds true for a data set that grows exponentially (e.g., doubles, then doubles again in the same time span), but also appears to hold true for many cases in which an exponential growth pattern is not obvious (e.g., constant growth each month in the number of accounting transactions for a particular cycle ...


1 Answers

Let's first assume y is divisible by x. Then we denote p = y/x and the solution is simple. Go through the list, every p elements, mark 1 of them.

Now, let's say r = y%x is non zero. Still p = y/x where / is integer devision. So, you need to:

  • In the first p-r elements, mark 1 elements
  • In the last r elements, mark 2 elements

Note: This depends on how you define evenly distributed. You might want to spread the r sections withx+1 elements in between p-r sections with x elements, which indeed is again the same problem and could be solved recursively.

Alright so it wasn't actually correct. I think this would do though:

Regardless of divisibility:

  • if y > 2*x, then mark 1 element every p = y/x elements, x times.
  • if y < 2*x, then mark all, and do the previous step unmarking y-x out of y checkboxes (so like in the previous case, but x is replaced by y-x)

Note: This depends on how you define evenly distributed. You might want to change between p and p+1 elements for example to distribute them better.

like image 197
Shahbaz Avatar answered Oct 05 '22 23:10

Shahbaz