A hard Question? [closed] - discrete-mathematics

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
I try To find a solution to a question ....
we have a number , example : 20 ...
and we have 6 number :{ a ,b , c , d , e , f} < 20 ,
t try to find all values of these numbers , but only if we can combinate (whit + or -) whit 2 of this numbers and getting all the value below to 20 : for example
we choose 31 :
a = 22 b = 21 c = 14 d = 11 e = 9 f = 5
we have :
22 - 21 = 1 ;
11 - 9 = 2 ;
14 - 11 = 3 ;
9 - 5 = 4 ;
f = 5 ;
11 - 5 = 6 ;
21 - 14 = 7 ;
....
....
....
....
....
21 + 9 = 30 ;
9 + 22 = 31 ;

This appears like homework, so I'll try to just give you some hints.
You want to loop over all combinations of two numbers from the array. To do that, you can use an outer loop that loops over all the numbers and then inside that loop, an inner loop that also loops over all the numbers.
Depending on exactly what you want to do, you need to decide if you want to handle x, y separately from y, x

Given a natural number n, find all combinations of 6 positive integers a, b, c, d, e, and f such that {a, b, c, d, e, f} ∪ {a + b, a + c, ..., a + f, b + c, b + d, ..., b + f, ..., e + f} ∪ {a - b, a - c, ..., a - f, b - c, b - d, ..., b - f, ..., e - f, b - a, c - a, ..., f - a, c - b, d - b, ..., f - b, ..., f - e} contains 1, 2, 3, 4, ..., n - 1, and n.
I haven't thought about this problem very much, but it appears to me to be a hard problem, indeed. Maybe there is a trick, I am not sure, but if I had to solve this problem, I would first try to find the least n for which there are not infinitely-many solutions (if n is 1, for example, then any combination of 6 natural numbers containing two consecutive natural numbers will work).
I think that you will have better luck with finding a solution to this problem in a mathematics discussion forum.

void FindCombinations(int n, int [] values)
{
for(int i = 0; i < values.length; i++)
{
int left = values[i];
for(int j = 0; k < values.length; j++)
{
int right = values[j];
if (left == right)
continue;
if (left + right < n)
Console.Write(string.Format("{0} + {1} = {2} < {3}", left, right, left+right, n);
if (left - right < n)
Console.Write(string.Format("{0} - {1} = {2} < {3}", left, right, left-right, n);
}
}
}
While this question stinks of homework it's not really that hard (although arguably hard to interpret).
Note: This solution is O(n^2) and is not efficient, please keep this in mind.

Related

How to determine the result of a code without running it

If I am given the code below, how can I tell what the resulting y-value would be. I apologize if this is a simple question but I find these types of questions very difficult.
For foo(-1,10)
function y = foo(x, a)
for k=-1:0
b=x-k;
while (x > -2) && (x < 2)
x=x+a+1;
end
end
y = b + x;
end
When running the programme I can see that b=10 but I don't understand how you get that. I would appreciate if someone could make this clearer for me.
Thank you!
Start from the top:
foo(x, a) has two parameters: x and a
foo(-1, 10) would mean that x = -1 and a = 10.
From there go down each line.
b = x - k would start out as b = -1 + (the value of k on that current iteration of the loop
Then you would do the same for the while loop.
x = -1 + 10 + 1
So,
x = 10
Now take that value and plug it into the while loop condition:
(10 > -2) and (10 < 2)
Is this condition true? No. So you move on to the next iteration of the for loop
At the end you set y equal to the value you got for b + the value you got for x

Need help in matrix dimension while converting from fortran to matlab

I have been working on a fortran code to convert it into the matlab. I am facing some issues with dimensioning! Following is the code which is giving me error
do 10 p = 1,m
d(p) = 0.d0
d(p) = x - x1(i,p) - x2(i,p) -
& double_sum(i,p,n,m,str,mot)
10 continue
double_sum = 0.d0
do 10 j = 1,m
do 20 k = 1,n
if (k .eq. i) then
else
double_sum = double_sum + mot(k,j,i,p)*str(k,j)
endif
20 continue
10 continue
to which I converted it into matlab as:
for p=1:m
d(p)=0;
double_sum = 0;
for j=1:m
for k=1:n
if k==i
else
double_sum = double_sum + mot(k,j,i,p)*str(k,j);
end
end
end
d(p)=x - x1(i,p) - x2(i,p)-double_sum(i,p,n,m,str,mot);
end
I am getting error of "index exceeding matrix".
The error line is for this part of my code:
d(p)=x - x1(i,p) - x2(i,p)-double_sum(i,p,n,m,str,mot);
So if I ignore double_sum(i,p,n,m,str,mot); this part, code runs perfectly.
I know the double_sum matrix is of 6D which looks suspicious to me, but I would like to have your support to successfully port this piece of fortran code.
Note:Asked the same question on matlab forum. But stackoverflow have more chances of people worked on fortran 77. Hence asking it here.
If the Fortran code in the Question is really everything, it may be a very rough snippet that explains how to calculate array d(:)
do 10 p = 1, m
d( p ) = x - x1( i, p ) - x2( i, p ) - double_sum( i, p, n, m, str, mot )
10 continue
with a function double_sum() defined by
double precision function double_sum( i, p, n, m, str, mot )
implicit none
integer, intent(in) :: i, p, n, m
double precision, intent(in) :: str( n, m ), mot( n, m, ?, ? )
integer j, k
double_sum = 0.d0
do 10 j = 1, m
do 20 k = 1, n
if (k .eq. i) then
else
double_sum = double_sum + mot( k, j, i, p ) * str( k, j )
endif
20 continue
10 continue
end
though it is definitely better to find the original Fortran source to check the context...(including how i and d(:) are used outside this code). Nevertheless, if we use the above interpretation, the corresponding Matlab code may look like this:
for p = 1:m
double_sum = 0;
for j = 1:m
for k = 1:n
if k == i
else
double_sum = double_sum + mot( k, j, i, p ) * str( k, j );
end
end
end
d( p ) = x - x1( i, p ) - x2( i, p ) - double_sum; % <--- no indices for double_sum
end
There is also a possibility that double_sum() is a recursive function, but because we cannot use the function name as the result variable (e.g. this page), it may be OK to exclude that possibility (so the Fortran code has two scopes, as suggested by redundant labels 10).
There is an error in your loops. The fortran code runs one loop over p=1:m, whose end is marked by the continue statement. Then, two nested loops over j and k follow.
Assuming, you know the size of all your arrays beforehand and have initialized them to the correct size (which may not be the case given your error statement) this is more along the lines of the fortran example you posted.
d = zeros(size(d));
for p=1:m
d(p)=x - x1(i,p) - x2(i,p)-double_sum(i,p,n,m,str,mot);
end
% add a statement here to set all entries of double sum to zero
double_sum = zeros(size(double_sum))
for j=1:m
for k=1:n
if k==i
else
double_sum = double_sum + mot(k,j,i,p)*str(k,j);
end
end
end
It is a little hard to give advice without knowledge of more parts of the code. are mot and str and double_sum functions? Arrays? The ambiguous choice of brackets in those two languages are hardly OPs fault, but make it necessary to provide further input.

Go Golang : Merge Sort Stack Overflow

http://play.golang.org/p/rRccL6YHtQ
I just implement the same code as in CLRS
Pseudocode from CLRS
Merge-Sort(A, p, r)
if p < r
q = [(q+r)/2]
Merge-Sort(A, p, q)
Merge-Sort(A, q+1, r)
Merge(A, p, q, r)
Merge(A, p, q, r)
n_1 = q - p + 1
n_2 = r - q
let L[1 .. n_1 + 1] and R[1 .. n_2 + 1] be new arrays
for i = 1 to n_1
L[i] = A[p+i-1]
for j = 1 to n_2
R[j] = A[q+j]
L[n_1 + 1] = INFINITE
R[n_2 + 1] = INFINITE
i = 1
j = 1
for k = p to r
if L[i] <= R[j]
A[k] = L[i]
i = i + 1
else A[k] = R[j]
j = j + 1
But I am getting the stack overflow in the merge sort.
[9 -13 4 -2 3 1 -10 21 12]
runtime: goroutine stack exceeds 250000000-byte limit
fatal error: stack overflow
runtime stack:
runtime.throw(0x1b4980, 0x20280)
How do I make this work?
func MergeSort(slice []int, first, last int) {
if len(slice) < 2 {
return
}
if first < last {
mid := len(slice) / 2
MergeSort(slice, first, mid)
MergeSort(slice, mid+1, last)
Merge(slice, first, mid, last)
}
}
thanks a lot!
mid := len(slice) / 2
That's not where the middle should go. The middle is supposed to be halfway between first and last, which define the region of the slice you're sorting, not halfway through the slice. Alternatively, you can slice the slice to make new slices and drop first and last.
You don't seem to be re-slicing the slice you're merge-sorting, so there is no termination of the recursion.
You could either re-slice the slice before recursion, or you can do something like length := last - first (this may have an off-by-one, depending if "last" is the last index or one past the last index) and then go from there.

(a mod 2*x)-(a mod x) [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I am trying to find the possible values of this expression.
(a mod 2*x)-(a mod x)
I suspect they might be 0 or x, but I'm really not sure. I can't seem to be able to write down a proper argument.
You are correct that the possible values are 0 and x, assuming that both a and x are positive. The logic is as follows.
Let a have the form
a = p*x + b
Then it is easy to see that a mod x = b.
For a mod 2*x, if p = 2*r (p is even) then
a = 2*r*x + b = (2*x)*r + b
so that a mod 2*x = b and p = 2*r + 1 (p is odd) then
a = (2*r + 1)*x + b = 2*r*x + x + b = (2*x)*r + x + b
so that a mod 2*x = x + b. Combining these results, the difference is either b - b = 0 (when p is even) or (x + b) - b = x (when p is odd).

Modulo and remainder (Chinese remainder theorem) in MATLAB

How do I find the least possible value in Matlab, given the modulo values and its remainder values in an array? for example:
A=[ 23 90 56 36] %# the modulo values
B=[ 1 3 37 21] %# the remainder values
which leads to the answer 93; which is the least possible value.
EDIT:
Here is my code but it only seems to display the last value of the remainder array as the least value:
z = input('z=');
r = input('r=');
c = 0;
m = max(z);
[x, y] = find(z == m);
r = r(y);
f = find(z);
q = max(f);
p = z(1:q);
n = m * c + r;
if (mod(n, p) == r)
c = c + 1;
end
fprintf('The lowest value is %0.f\n', n)
Okay, so your goal is to find the smallest x that satisfies B(i) = mod(x, A(i)) for each i.
This page contains a very simple (yet thorough) explanation of how to solve your set of equations using the Chinese Remainder Theorem. So, here's an implementation of the described method in MATLAB:
A = [23, 90]; %# Moduli
B = [1, 3]; %# Remainders
%# Find the smallest x that satisfies B(i) = mod(x, A(i)) for each i
AA = meshgrid(A);
assert(~sum(sum(gcd(AA, AA') - diag(A) > 1))) %# Check that moduli are coprime
M = prod(AA' - diag(A - 1));
[G, U, V] = gcd(A, M);
x = mod(sum(B .* M .* V), prod(A))
x =
93
You should note that this algorithm works only for moduli (the values of A) which are coprime!
In your example, they are not, so this will not work for your example (I've put an assert command to break the script if the moduli are not coprime). You should try to implement yourself the full solution for non-comprime moduli!
P.S
Also note that the gcd command uses Euclid's algorithm. If you are required to implement it yourself, this and this might serve you as good references.