Mathematics, combinatorics. What is the maximum number of variations? - math

There's this line.
X1_X2_X3_X4_X5_X6
It is known that each variable X* can take values from 0 to 100. The sum of all X* variables is always equal to 100. How many possible string variants can be created?

Suppose F(n,s) is the number of strings with n variables, and the variables sum to s, where each variable is between 0 and 100, and suppose s<=100. You want F(6,100).
Clearly
F(1,s) = 1
If the first variable is t, then it can be followed by strings of n-1 variables that sum to s-t. Thus
F(n,s) = Sum{ 0<=t<=s | F(n-1, s-t) }
So its easy to write a wee function to compute the answer.

Related

R, mixOmics, nearZeroVar description uniqueCut does not make sense

The nearZeroVar() function from the mixOmics R package is simply the following code:
nearZeroVar(x, freqCut=95/5, uniqueCut=15) # default values shown
Here is the description of what this function does, straight from the source.
For example, an example of near zero variance predictor is one that,
for 1000 samples, has two distinct values and 999 of them are a single
value.
To be flagged, first the frequency of the most prevalent value over
the second most frequent value (called the “frequency ratio”) must be
above freqCut. Secondly, the “percent of unique values,” the number of
unique values divided by the total number of samples (times 100), must
also be below uniqueCut.
In the above example, the frequency ratio is 999 and the unique value
percentage is 0.0001.
I understand that the frequency ratio would be 999/1 (because there are 999 single values and 1 other value) so it would be 999. But shouldn't the unique value percentage be = 2/1000*100 = 0.2, since it would be 2 unique values over the number of samples. How does one obtain 0.0001 as the answer?

Problem similar to Coin change problem with finite coins, but with intervallic coin values and fixed number of coins

I have been struggling to create an algorithm, for a problem where we get an array of n coins with their max values. A coin in the array can have a value between 0 and this max value. We want to determine ALL permutations of possible order of coins given a sum target x. For example the array {2,2} with x = 3 has 2 permutations: 1+2, 2+1. I know this problem is related to coin change/knapsack/ sub-set problem. But haven't been able to make an algorithm based on these known solutions. I've tried doing dynamic programing, but all I could conclude that the array of possible targets is some kind of binomial. For example the array of coins with max values {3,3,3}, (n=3) can be any sum value from 0-9, where the number of permutations are:
sum value : permutations
0:1
1:3
2:6
3:10
4:12
5:12
6:10
7:6
8:3
9:1
From doing this I could atleast conclude that the the number of permutations for value 0 and max sum is always 1 and for value 1 and value max-1 is always n (length of the array). But I could not for the life of me figure out a recursion for the rest of the numbers, which is why I need help. Thanks in advance.

How to save values in Vector using R

I am supposed to find the mean and standard deviation at each given sample size (N), using the "FOR LOOP". I started writing the code as below, I am required to save all the means into vector "p". How do I save all the means into one vector?
sample.sizes =c(3,10,50,100,500,1000)
mean.sds = numeric(0)
for ( N in sample.sizes ){
x <- rnorm(3,mean=0,sd=1)
mean.sds[i]
}
mean(x)
Actually you are doing many thing wrong?
If you are using variable N in for loop, you are not using it anywhere
for (N in 'some_vector') actually means N will take that value one by one. So N in sample sizes will first take, 3 then 10 then 50 and so on.
Now where does i come into picture?
You are calculating x for each iteration of N. In fact you are not using N anywhere in the loop?
first x will return 3 values. In the next line you intend to store these three values in just ith value of mean.sds where i is unknown and storing three values into one value, as it is, is not logically possible.
Do you want this?
sample.sizes =c(3,10,50,100,500,1000)
mean.sds = numeric(0)
for ( i in seq_along(sample.sizes )){
x <- rnorm(sample.sizes[i], mean=0, sd=1)
mean.sds[i] <- mean(x)
}
mean.sds
[1] 0.6085489531 -0.1547286299 0.0052106559 -0.0452804986 -0.0374094936 0.0005667246
I replaced N with seq_along(sample.sizes) which will give iterations equal to the number of that vector. Six in this example.
I passed each ith element to first argument of rnorm to generate these many random values.
Stored each random value into single vector. calculated its mean (one value only) and stored in ith value of your empty vector.

Is there an R function to limit sum of numbers to 100 maintaining the original ratio

I am new to R and math. I want to know if there is a function which will scale numeric vector to 100. I have a vector of percentages as numbers, I have to make sure the sum is 100. If the sum is not 100, the numbers need to be scaled up or down such that the ratio is maintained and the sum is 100
Sum the numbers; call that T
compute C = 100/T
Multiply every number by C.
The new numbers then sum to 100 and are in the same ratios as the original numbers.

Find sum of factors

Why does this code return the sum of factors of a number?
In several Project Euler problems, you are asked to compute the sum of factors as a part of the problem. On one of the forums there, someone posted the following Java code as the best way of finding that sum, since you don't actually have to find the individual factors, just the prime ones (you don't need to know Java, you can skip to my summary below):
public int sumOfDivisors(int n)
{
int prod=1;
for(int k=2;k*k<=n;k++){
int p=1;
while(n%k==0){
p=p*k+1;
n/=k;
}
prod*=p;
}
if(n>1)
prod*=1+n;
return prod;
}
Now, I've tried it many times and I see that it works. The question is, why?
Say you factor 100: 1,2,4,5,10,20,25,50,100. The sum is 217. The prime factorization is 2*2*5*5. This function gives you [5*(5+1)+1]*[2*(2+1)+1] = [25+5+1]*[4+2+1] = 217
Factoring 8: 1,2,4,8. The sum is 15. The prime factorization is 2*2*2. This function gives you [2*(2*(2+1)+1)+1]=15
The algorithm boils down to (using Fi to mean the ith index of the factor F or F sub i):
return product(sum(Fi^k, k from 0 to Ni), i from 1 to m)
where m is number of unique prime factors, Ni is the number of times each unique factor occurs in the prime factorization.
Why is this formula equal to the sum of the factors? My guess is that it equals the sum of every unique combination of prime factors (i.e. every unique factor) via the distributive property, but I don't see how.
Let's look at the simplest case: when n is a power of a prime number.
The factors of k^m are 1, k, k^2, k^3 ... k^m-1.
Now let's look at the inner loop of the algorithm:
After the first iteration, we have k + 1.
After the second iteration, we have k(k+1) + 1, or k^2 + k + 1
After the third iteration, we have k^3 + k^2 + k + 1
And so on...
That's how it works for numbers that are powers of a single prime. I might sit down and generalize this to all numbers, but you might want to give it a go yourself first.
EDIT: Now that this is the accepted answer, I'll elaborate a bit more by showing how the algorithm works on numbers with two distinct prime factors. It is then straightforward to generalize that to numbers with an arbitrary amount of distinct prime factors.
The factors of x^i.y^j are x^0.y^0, x^0.y^1 ... x^0.y^j, x^1.y^0...
The inner loops for each distinct prime factor generate x^i + x^i-1 + ... + x^0 (and similarly for y). Then we just multiply them together and we have our sum of factors.
The algorithm is essentially looking at the set of all ordered subsets of the prime factors of n, which is analogous to the set of factors of n.

Resources