Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Given an integer x in the interval [1 .. 6], I am looking for two mathematical functions y1 and y2 so that:
y1(x) ∈ [1 .. 6], y2(x) ∈ [1 .. 6]
y1(x) ≠ y2(x) ≠ x
y1(x) and y2(x) are integers
I tried y1(x) = 7-x and y2(x) = (1+x)%6 where % is the remainder or modulo operation.
That solution does not work for x=6. I get y1(x) = y2(x) = 1, which does not fulfills the condition 2. Neither for x=3 and x=5.
Does anyone sees a working solution?
You can use for example:
y1=(x % 6) +1
y2=((x+1) % 6) +1
Functions as table:
x y1 y2
1 2 3
2 3 4
3 4 5
4 5 6
5 6 1
6 1 2
Technically, y1=1+((x+1) %6) and y2=(1+(x+2) %6) both satisfy your request.
I guess though you were thinking about something with a unified distribution of some sort (which is usually the motivation for such attempts...).
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
When I started solving this problem I came across this thought that multiples of three has this property sum of digits is a multiple of three. So for numbers less than 9. We can lay the condition that they must be numbers of a three digit numbers which is divisible by 3.
I'm assuming you mean 3 numbers are chosen from the range [0, 21] inclusive.
Look at the counts of numbers mod 3. You have 8 congruent to 0, 7 congruent to 1, and 7 congruent to 2.
Ways to get three that sum to a multiple of 3:
congruencies: count, example
0,0,0: choose(8, 3) = 56, e.g. 0 + 3 + 6 = 9
0,1,2: 8 * 7 * 7 = 392, e.g. 3 + 4 + 5 = 12
1,1,1: choose(7, 3) = 35, e.g. 1 + 4 + 7 = 12
2,2,2: choose(7, 3) = 35, e.g, 2 + 5 + 8 = 15
-------------------------
total: 518
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How to find the sum of this series
fib(0)^K + fib(C)^K + fib(2*C)^K + fib(3*C)^K + ... + fib(N*C)^K
where constraint are 0 < N < 10^15 , 0 < C < 11 and 0 < k < 11?
Here fib(i) is the i-th fibonacci number, where fib(0)=0 , fib(1)=1 and fib(n) = fib(n-1)+fib(n-2). We have to calculate the summation modulo 1000000007 (10^9+7).
This is an exercise in manipulating recurrence relations, basically. To understand this answer, you should be comfortable switching between matrix and system form.
First we get a recurrence for fib(i)^K. This actually involves a system of recurrences for fib(i)^K, fib(i)^(K-1) fib(i-1), fib(i)^(K-2) fib(i-1)^2, ..., fib(i-1)^K. I'll demonstrate for K = 3.
fib(i)^3 = (fib(i-1) + fib(i-2))^3
= fib(i-1)^3 + 3 fib(i-1)^2 fib(i-2) + 3 fib(i-1) fib(i-2)^2 + fib(i-2)^3
fib(i)^2 fib(i-1) = (fib(i-1) + fib(i-2))^2 fib(i-1)
= fib(i-1)^3 + 2 fib(i-1)^2 fib(i-2) + fib(i-1) fib(i-2)^2
fib(i) fib(i-1)^2 = (fib(i-1) + fib(i-2)) fib(i-1)^2
= fib(i-1)^3 + fib(i-1)^2 fib(i-2)
fib(i-1)^3 = fib(i-1)^3
These can be combined into a single matrix.
[fib(i)^3 fib(i)^2 fib(i-1) fib(i) fib(i-1)^2 fib(i-1)^3] =
i
= [fib(0)^3 fib(0)^2 fib(-1) fib(0) fib(-1)^2 fib(-1)^3] [1 0 0 0]
[3 1 0 0]
[3 2 1 0]
[1 1 1 1]
i
= [0 0 0 1] [1 0 0 0]
[3 1 0 0]
[3 2 1 0]
[1 1 1 1]
You may recognize Pascal's triangle over there.
Now, given a system of recurrences for a function f(i), we can compute recurrences for f(c i) by raising the matrix to the power c.
The final step is to go from a recurrence for f(i) to a recurrence for F(i) = f(0) + f(1) + ... + f(i-1). It's simple to add an equation
F(i) = F(i-1) + f(i-1)
to the system.
Having computed the matrix, which by my estimate will have at most 12^2 = 144 elements, we can compute the appropriate power using fast matrix exponentiation mod 10^9 + 7. Mind the lurking off-by-one error – that's F(n+1) that you want.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
We got two integers x and y, and we call an array is a perfect array if and only if it satisfies the following conditions:
The elements in the array are integers.
The length of the array is exactly y.
The product of all elements is exactly x.
So how to find the number of beautiful arrays for different x and y?
For example, if x is 3 and y is 2, there are four beautiful arrays:
[1,3],[3,1],[-1,-3],[-3,-1]
Let's do few examples first:
x = 30, y = 3, x's prime factors are 2, 3, 5. Possible combinations are 2 * 3 * 5, 6 * 5, 2 * 15, 3 * 10, 30. For every possible combination of length L we need to count the number of possible arrangement of its elements in 3 slots, that is y!/(y-L)!. We obtain 6 + 6 + 6 + 6 + 3 = 27.
x = 36, y = 5, x's prime factors are 2, 2, 3, 3. Writing in the same way as the previous example would be tedious, so let's think about y bins in which we want to put 2, 2 and 3, 3. The number of ways we can distribute 2, 2 into 5 bins can be computed with the help of stars and bars, we need to put 4 bars among 2, 2. There is 6!/(4!2!) = 15. The same for 3, 3. So, the answer is 225.
Factor x into prime divisors. Such factorization is unique. And takes up to sqrt(x).
Count the number of ways we can distribute every pi into y bins. Multiply.
Account for positive/negative multiplying by
First of all, you need to factor x. This can be done in O(√x) (googl it).
Let x=Πp_i^k_i (p_i is a prime number)
Count the number of distributing ∑k_i for y cells.
This is (∑k_i+x-1)!/(x-1)!/∑k_i
There is also the degree of freedom for positive and negative.
This is the number of way to make pairs.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
We have the test image with M rows and N columns as
f(x,y), for x∈ [1,M] and y∈ [1,N ]. The horizontal absolute
difference value of a pixel is defined by
D (x, y) = |f (x, y +1) − f (x, y −1)|.
need help in how to implement it in matlab
This will generate same size matrix, that you need:
mat1 = [zeros(2,size(f,2)); f];% adds 2 rows of zeros to begining
mat2 = [f;zeros(2,size(f,2))]; %adds 2 row of zeros to the end
Dd = mat1-mat2;
D = Dd(2:((size(Dd,1)-1)),:);%crop Dd matrix to size(f)
D = abs( f(1:end-1,:) - f(2:end,:) );
check out diff command as well. Note that D has 1 row less than f.
aux = abs(diff(f,[],2));
D = max(aux(:,1:end-1), aux(:,2:end));
For example: given
f = [3 5 6 4
2 5 4 3
8 9 3 1];
the result is
>> D
D =
2 2
3 1
6 6
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
We have this matrix of 4x4:
a b c d
e f g h
1 2 3 4
5 6 7 8
By transposing the matrix we get:
a e 1 5
b f 2 6
c g 3 7
d h 4 8
My question is:
What matrix do we get by "transposing column 2 with row 4?"
I need to understand the operation in itself, what does it imply/mean? I never thought of "transposing a column with a line".
AFAIK, It means you are to swap column 2 and row 4, instead of column 1 with row 1 and column2 with row 2 etc.
The code is basically the same as a full transposition, except you only have one column/row
Matrix transposition is a mathematical operation in which a matrix's rows become its columns. From a mathematical perspective, there's no real benefit to transposing only one row in a M x N matrix, but the code to transpose one row is not much different than transposing an entire matrix.
The matrix you get after the transposition would be:
a b 1 d
e f 2 h
c g 3 7
5 6 4 8