Math: Five numbers with unique sums - math

So I need a way to figure out how to get 5 numbers, and when you add any 2 of them, it will result in a sum that you can only get by adding those specific two numbers.
Here's an example of what I'm talking about, but with 3 numbers:
1
3
5
1 + 3 = 4
1 + 5 = 6
3 + 5 = 8
Adding any two of those numbers will end up with a unique sum that cannot be found by adding any other pair of the numbers. I need to do this, but with 5 different numbers. And if you have a method of figuring out how to do this with any amount of numbers, sharing that would be appreciated as well.
Thank you

1, 10, 100, 10000, 100000 gives you five numbers like you desire.
In general, 1, 10, 100, 1000, ..., 10^k where k is the number of numbers that you need.
And even more general, you can say b^0, b^1, ..., b^k, where b >= 2. Note that you have the special property that not only are all the pairwise sums unique, but all the subset sums are unique (just look at representations in base b).

The set {1, 2, 5, 11, 21} also works.
You can start with a set of two or three elements that fit that property (any addition operation on two elements from the set {1,2,5} gives you an unique sum) and only include the next number being considered if additions of current elements and this new element also give you unique sums.
An example run-through:
Suppose our starting set S is S={1,2,5}. Let U be the set of all sums between two elements in S.
Elements in S give us unique sums 1+2=3, 1+5=6, 2+5=7, so U={3,6,7}.
Consider adding 11 to this set. We need to check that 1+11, 2+11, and 5+11 all give us sums that are not seen in U and are all unique among themselves.
1+11=12, 2+11=13, 5+11=17.
Since 12, 13, and 17 are all unique sums among themselves, and are not found in U, we can update S and U to be:
S1 = {1,2,5,11}
U1 = {3,6,7,12,13,17}.
You can do the same procedure for 21, and you should (hopefully) get:
S2 = {1,2,5,11,21}
U2 = {3,6,7,12,13,17,22,23,26,32}.
If all you need is a quick set though, the solution that Jason posted is a lot faster to produce.

1
2
4
8
16
1
3
9
27
81
suggests x ^ n where n is a member of a subset of Natural numbers

Related

Picking random elements from a vector but excluding certain numbers each time

I have this vector
K=c(1,2,3,4,5,6,8,10,12,14)
I want to pick 2 random elements from K such that my output never includes 6 or 14 or both each time. How can i do this for it to have output like if i used
S=c(1,2,3,4,5,8,10,12)
sample(S,2)
You may take 6 and 14 out of the vector of candidates to sample from, as in
sample(setdiff(K, c(6, 14)), 2)

Random number generation - constrained sequence

I'm trying to produce a set of 480 random integers between 1-9. However there are some constraints:
the sequence cannot contain 2 duplicate digits in a row.
the sequence must include exactly 4 sequences of odd numbers and 4 sequences of even numbers (in any order) within every 80 digit sequence (e.g. 6 4 5 2 4 8 3 4 6 9 1 5 4 6 1).
I have been able to produce a set of random numbers hat allows repeated digits, using:
NumRep <- sample(1:9, 480, replace=T)
but not been able to work out how to allow digits to be repeated over the entire set, but to not allow sequential repeats (e.g. 2 5 3 would be okay, 2 5 5 would not). I have got nowhere with the odd/even constraint.
For context, this is not homework! I am a researcher, and this is part of a psychological experiment that I am creating.
Any help would be greatly appreciated!
First, the problem loses the "random" way of simulating with that conditions. Anyway this code corresponds with the first constraint:
# Build a vector
C<-vector()
# Length of the vector
n<-480
# The first element
C<-sample(1:9,1)
# Complete the rest
for (i in 2:n){
# Take a random number not equal to the previous one
C[i] <- sample(c(1:9)[1:9!=C[i-1]],1)
}
# It is an odd number?
C %% 2 == 0
# How many consecutive odd numbers are in the sequence?
# Build a table with this information
TAB <- rle( C %% 2 == 0)
# Maximum of consecutive odd numbers
max(TAB$lengths[TAB$values==T])
# Maximum of consecutive even numbers
max(TAB$lengths[TAB$values==F])
I don't understand the second constraint, but I hope the final part of the code helps. You should use that information in order to interchange some values.

How many distinct combinations can be made from a complex group?

By complex group I mean a group where not all values are distinct. That is, if an ordinary group would be 1,2,3,4,5,6,7 (In which the amount of different combinations is 7C0+7C1+7C2...=2^7), then an example for a complex group is 1,1,1,3,3,5,7. How to calculate how many different combinations (where order does not matter) can be generated from such groups?
EDIT: to clarify this. If for example we take 7C1=7, then we find that it cannot be applied to complex groups. That's because we get 7 different groups, but some of them are equal (1=1=1 and 3=3), so actually there are only 4 different groups (1,3,5,7).
In other words, in the simple case of 1,1,2, simple 2^3 would consider these groups:
{},{1},{1},{2},{1,1},{1,2},{1,2},{1,1,2} = 8
What I need, is a way to calculate the amount of different groups (I consider {1,2}={2,1}). That would consider these:
{},{1},{2},{1,1},{1,2},{1,1,2} = 6
It is the product of the (counts+1) of unique elements in the set.
Explanation : For each unique number it can occur from zero to k times where k is the number of repetition of the number. So there are [0..k] i.e. total (k+1) options for each unique number. So the it is the product of the (counts+1) of unique elements in the set.
For {1,1,2}: count+1 for 1 = 2+1 = 3 and count+1 for 2 = 1+1 = 2
So the answer is 3*2 = 6.
For {1,1,1,3,3,5,7} it is (3+1)*(2+1)*(1+1)*(1+1) = 4*3*2*2 = 48
A python3 code:
>>> import collections
>>> A = [1,1,1,3,3,5,7]
>>> def countComplexGroups(A):
... count = collections.Counter(A)
... rt = 1
... for i in count: rt*=count[i]+1
... return rt
...
>>> print(countComplexGroups(A))
48

Determine how different are some vectors

I want to differentiate data vectors to find those that are similar. For example:
A=[4,5,6,7,8];
B=[4,5,6,6,8];
C=[4,5,6,7,7];
D=[1,2,3,9,9];
E=[1,2,3,9,8];
In the previous example I want to distinguish that A,B,C vectors are similar (not the same) to each other and D,E are similiar to each other. The result should be something like: A,B,C are similar and D,E are similar, but the group A,B,C is not similar to the group of D,E. Matlab can do this?
I was thinking using some classification algorithm or Kmeans,ROC,etc.. but I'm not sure which one will be the best one.
Any suggestion? Thanks in advance
One of my new favourite methods for this sort of thing is agglomerate clustering.
First, concatenate all your vectors into a matrix, where each row is a separate vector. This makes such methods much easier to use:
F = [A; B; C; D; E];
Then the linkages can be found:
Z = linkage(F, 'ward', 'euclidean');
This can be plotted using:
dendrogram(Z);
This shows a tree, where each leaf at the bottom is one of the original vectors. Lengths of the branches show similarities and dissimilarities.
As you can see, 1, 2 and 3 are shown to be very close, as are 4 and 5. This even gives a measure of closeness, and shows that vectors 1 and 3 are deemed to be closer than vectors 2 and 3 (in the sense that, percentagewise, 7 is closer to 8 than 6 is to 7).
If all the vectors you are comparing are of the same length, a suitable norm on pairwise differences may well be enough. The norm to choose will depend on your particular criteria of closeness, of course, but with the examples you show, simply summing the absolute values of the components of the pairwise differences gives:
A B C D E
A 0 1 1 12 11
B 0 2 13 12
C 0 13 12
D 0 1
E 0
which doesn't need a particularly well-tuned threshold to work.
You can use pdist(), this function gives you the pairwise distances.
Various distance (opposite of similarity) metrics are already implemented, 'euclidean' seems appropriate for your situation, although you may want to try out the effect of different metrics.
Here it goes the solution I propose based on your results:
Z = [A;B;C;D;E];
Y = pdist(Z);
matrix = SQUAREFORM(Y);
matrix_round = round(matrix);
Now that we have the vector we can set the threshold based on the maximun value and decide with which theshold is the most appropriate.
It would be nice to create some cluster plot showing the differences between them.
Best regards

Geometrical progression with any number row

I can have any number row which consists from 2 to 10 numbers. And from this row, I have to get geometrical progression.
For example:
Given number row: 125 5 625 I have to get answer 5. Row: 128 8 512 I have to get answer 4.
Can you give me a hand? I don't ask for a program, just a hint, I want to understand it by myself and write a code by myself, but damn, I have been thinking the whole day and couldn't figure this out.
Thank you.
DON'T WRITE THE WHOLE PROGRAM!
Guys, you don't get it, I can't just simple make a division. I actually have to get geometrical progression + show all numbers. In 128 8 512 row all numbers would be: 8 32 128 512
Seth's answer is the right one. I'm leaving this answer here to help elaborate on why the answer to 128 8 512 is 4 because people seem to be having trouble with that.
A geometric progression's elements can be written in the form c*b^n where b is the number you're looking for (b is also necessarily greater than 1), c is a constant and n is some arbritrary number.
So the best bet is to start with the smallest number, factorize it and look at all possible solutions to writing it in the c*b^n form, then using that b on the remaining numbers. Return the largest result that works.
So for your examples:
125 5 625
Start with 5. 5 is prime, so it can be written in only one way: 5 = 1*5^1. So your b is 5. You can stop now, assuming you know the row is in fact geometric. If you need to determine whether it's geometric then test that b on the remaining numbers.
128 8 512
8 can be written in more than one way: 8 = 1*8^1, 8 = 2*2^2, 8 = 2*4^1, 8 = 4*2^1. So you have three possible values for b, with a few different options for c. Try the biggest first. 8 doesn't work. Try 4. It works! 128 = 2*4^3 and 512 = 2*4^4. So b is 4 and c is 2.
3 15 375
This one is a bit mean because the first number is prime but isn't b, it's c. So you'll need to make sure that if your first b-candidate doesn't work on the remaining numbers you have to look at the next smallest number and decompose it. So here you'd decompose 15: 15 = 15*?^0 (degenerate case), 15 = 3*5^1, 15 = 5*3^1, 15 = 1*15^1. The answer is 5, and 3 = 3*5^0, so it works out.
Edit: I think this should be correct now.
This algorithm does not rely on factoring, only on the Euclidean Algorithm, and a close variant thereof. This makes it slightly more mathematically sophisticated then a solution that uses factoring, but it will be MUCH faster. If you understand the Euclidean Algorithm and logarithms, the math should not be a problem.
(1) Sort the set of numbers. You have numbers of the form ab^{n1} < .. < ab^{nk}.
Example: (3 * 2, 3*2^5, 3*2^7, 3*2^13)
(2) Form a new list whose nth element of the (n+1)st element of the sorted list divided by the (n)th. You now have b^{n2 - n1}, b^{n3 - n2}, ..., b^{nk - n(k-1)}.
(Continued) Example: (2^4, 2^2, 2^6)
Define d_i = n_(i+1) - n_i (do not program this -- you couldn't even if you wanted to, since the n_i are unknown -- this is just to explain how the program works).
(Continued) Example: d_1 = 4, d_2 = 2, d_3 = 6
Note that in our example problem, we're free to take either (a = 3, b = 2) or (a = 3/2, b = 4). The bottom line is any power of the "real" b that divides all entries in the list from step (2) is a correct answer. It follows that we can raise b to any power that divides all the d_i (in this case any power that divides 4, 2, and 6). The problem is we know neither b nor the d_i. But if we let m = gcd(d_1, ... d_(k-1)), then we CAN find b^m, which is sufficient.
NOTE: Given b^i and b^j, we can find b^gcd(i, j) using:
log(b^i) / log(b^j) = (i log b) / (j log b) = i/j
This permits us to use a modified version of the Euclidean Algorithm to find b^gcd(i, j). The "action" is all in the exponents: addition has been replaced by multiplication, multiplication with exponentiation, and (consequently) quotients with logarithms:
import math
def power_remainder(a, b):
q = int(math.log(a) / math.log(b))
return a / (b ** q)
def power_gcd(a, b):
while b != 1:
a, b = b, power_remainder(a, b)
return a
(3) Since all the elements of the original set differ by powers of r = b^gcd(d_1, ..., d_(k-1)), they are all of the form cr^n, as desired. However, c may not be an integer. Let me know if this is a problem.
The simplest approach would be to factorize the numbers and find the greatest number they have in common. But be careful, factorization has an exponential complexity so it might stop working if you get big numbers in the row.
What you want is to know the Greatest Common Divisor of all numbers in a row.
One method is to check if they all can be divided by the smaller number in the row.
If not, try half the smaller number in the row.
Then keep going down until you find a number that divides them all or your divisor equals 1.
Seth Answer is not correct, applyin that solution does not solves 128 8 2048 row for example (2*4^x), you get:
8 128 2048 =>
16 16 =>
GCD = 16
It is true that the solution is a factor of this result but you will need to factor it and check one by one what is the correct answer, in this case you will need to check the solutions factors in reverse order 16, 8, 4, 2 until you see 4 matches all the conditions.

Resources