Find the all possible multiples( size of n ) of a positive integer k - algorithm

Input 1 :
64
Output:( size of 3 )
1 x 1 x 64 =64
1 x 2 x 32 =64
1 x 4 x 16 =64
1 x 8 x 8 =64
2 x 2 x 16 =64
2 x 4 x 8 =64
4 x 4 x 4 =64
Input 2 :
6
Output:( size of 2 )
1 x 6 =6
2 x 3 =6
I tried Using Complete Binary Tree but I didn't get all possible Combination
.
Here is :
64
32 2
16 2 2 1
8 2 1 2 1 2 1 1
If Your trace level by level elements only some combinations are available
64 x 1 X 1
32 X 2 X 1
16 x 2 x 2
8 x 2 x 2 x 2( limit > 3 )
Question is I need all possible combinations

You can use recursion method. Consider the following PHP code (I guess you can convert for the idea to each language you need):
function comb($num, $cnt, $prefix, $minDiv) {
if ($cnt == 0)
{
if ($num == 1)
return rtrim($prefix,",");
else return false;
}
$arrs = array();
for ($i=$minDiv; $i <= $num; $i++) {
if ($num % $i == 0) { // if num modulo i equal 0
$ans = comb($num/$i, $cnt-1, $prefix . $i . ",", $i );
if ($ans) // if valid combination add it
$arrs[] = $ans;
}
}
return $arrs;
}
$ans = comb(64,3, "",1);
echo "ANSWER:\n";
echo print_r($ans);
This code will generate the following answer for comb(6,2, "", 1):
1,6
2,3

Related

How to Write an Readable and Understandable Bitwise Operator Formula?

I am Learning the bitwise operator from a course downloaded from udemy but It is very confusing.
They teach by this example which is very confusing to read and understand, is there a course or example which will teach bitwise operator in simple way? and also what is the use of bitwise operator? is it important to take headache from it or not?
The example:
#include <iostream>
using namespace std;
main()
{
/*
Operator Symbol Form Operation
left shift << x << y all bits in x shifted left y bits
right shift >> x >> y all bits in x shifted right y bits
bitwise NOT ~ ~x all bits in x flipped
bitwise AND & x & y each bit in x AND each bit in y
bitwise OR | x | y each bit in x OR each bit in y
bitwise XOR ^ x ^ y each bit in x XOR each bit in y
0
1
0101 0110
for example 126
1 * 10 ^ 2 + 2 * 10 ^ 1 + 6 * 10 ^ 0 = ?
1 * 10 ^ 2 = 100
2 * 10 ^ 1 = 20
6 * 10 ^ 0 = 6 why? because write 10 about 0 times = 0
we have the answer 126
1 2 6
3 2 1 // this is the position of 126. it's in -1 formula we can use
3-1=2
2-1=1
1-1=0
1 2 6 = 1 * 10 ^ 2 + 2 * 10 ^ 1 + 6 * 10 ^ 0
1 0 1 0 = 1 * 2 + 0 * 2 + 1 * 2 + 0 * 2
1 0 1 0 = 1 * 2 ^ 3 + 0 * 2 ^ 2 + 1 * 2 ^ 1 + 0 * 2 ^ 0 = 10
1 * 2 ^ 3 = 8
0 * 2 ^ 2 = 0
1 * 2 ^ 1 = 2
0 * 2 ^ 0 = 0
8 + 0 + 2 + 0 = 10
// Decimal notation for 1 0 1 0 is 10
1 0 1 1 0 0 = 0*0^0 + 0*0^1 + 1*2^2 + 1*2^3 + 0*0^4 + 1*2^5 = 44
0*0^0 = 0
0*0^1 = 0
1*2^2 = 4
1*2^3 = 8
0*0^4 = 0
1*2^5 = 32
0+0+4+8+0+32 = 44
*/
cout << (10 >> 1) << endl;
}

Algorithms - Time complexity of recursive multiplication function

I had this as a test question to evaluate the time complexity of the below recursive method.
def multiply(x,y)
if y = 0:
return 0
z = multiply(x,y/2)
if y is even:
return 2z
else :
return x + 2z
I had written log(n) as the number y keeps decreasing by 2 and the recursive call will soon end as it meets the condition.
If the function is meant to have a zero check as stated above in the question, this function will never terminate.
Here is a working python version:
def multiply(x,y):
if y == 0:
return 0
z = multiply(x,y/2)
if y % 2 == 0:
return 2 * z
else :
return x + 2 * z
Whatever you feed into it: this will end in a recursion error. So complexity is infinite. Perhaps this is a trick question.
If however what your teacher meant was something like this:
def multiply(x, y):
if 0.01 > y > -0.01:
return 0
z = multiply(x, y / 2)
if y % 2 == 0:
return 2 * z
else:
return x + 2 * z
then complexity looks indeed like log(n).
Here is some complexity benchmarking code which counts the number of operations:
counter = 0
def multiply(x, y):
global counter
counter += 1
if 0.01 > y > -0.01:
return 0
z = multiply(x, y / 2)
if y % 2 == 0:
return 2 * z
else:
return x + 2 * z
for i in range(1000):
counter = 0
multiply(1, i)
print(i, counter)
It prints a sequence of numbers which is logarithmic in nature:
0 1
1 8
2 9
3 10
4 10
5 10
6 11
7 11
8 11
9 11
10 11
11 12
12 12
13 12
14 12
15 12
16 12
17 12
18 12
19 12
20 12
21 13
22 13
23 13
24 13
25 13
26 13
27 13
28 13
29 13
30 13
...

Selecting neighbours on a circle

Consider we have N points on a circle. To each point an index is assigned i = (1,2,...,N). Now, for a randomly selected point, I want to have a vector including the indices of 5 points, [two left neighbors, the point itself, two right neighbors].
See the figure below.
Some sxamples are as follows:
N = 18;
selectedPointIdx = 4;
sequence = [2 3 4 5 6];
selectedPointIdx = 1
sequence = [17 18 1 2 3]
selectedPointIdx = 17
sequence = [15 16 17 18 1];
The conventional way to code this is considering the exceptions as if-else statements, as I did:
if ii == 1
lseq = [N-1 N ii ii+1 ii+2];
elseif ii == 2
lseq = [N ii-1 ii ii+1 ii+2];
elseif ii == N-1
lseq=[ii-2 ii-1 ii N 1];
elseif ii == N
lseq=[ii-2 ii-1 ii 1 2];
else
lseq=[ii-2 ii-1 ii ii+1 ii+2];
end
where ii is selectedPointIdx.
It is not efficient if I consider for instance 7 points instead of 5. What is a more efficient way?
How about this -
off = -2:2
out = mod((off + selectedPointIdx) + 17,18) + 1
For a window size of 7, edit off to -3:3.
It uses the strategy of subtracting 1 + modding + adding back 1 as also discussed here.
Sample run -
>> off = -2:2;
for selectedPointIdx = 1:18
disp(['For selectedPointIdx =',num2str(selectedPointIdx),' :'])
disp(mod((off + selectedPointIdx) + 17,18) + 1)
end
For selectedPointIdx =1 :
17 18 1 2 3
For selectedPointIdx =2 :
18 1 2 3 4
For selectedPointIdx =3 :
1 2 3 4 5
For selectedPointIdx =4 :
2 3 4 5 6
For selectedPointIdx =5 :
3 4 5 6 7
For selectedPointIdx =6 :
4 5 6 7 8
....
For selectedPointIdx =11 :
9 10 11 12 13
For selectedPointIdx =12 :
10 11 12 13 14
For selectedPointIdx =13 :
11 12 13 14 15
For selectedPointIdx =14 :
12 13 14 15 16
For selectedPointIdx =15 :
13 14 15 16 17
For selectedPointIdx =16 :
14 15 16 17 18
For selectedPointIdx =17 :
15 16 17 18 1
For selectedPointIdx =18 :
16 17 18 1 2
You can use modular arithmetic instead: Let p be the point among N points numbered 1 to N. Say you want m neighbors on each side, you can get them as follows:
(p - m - 1) mod N + 1
...
(p - 4) mod N + 1
(p - 3) mod N + 1
(p - 2) mod N + 1
p
(p + 1) mod N + 1
(p + 2) mod N + 1
(p + 3) mod N + 1
...
(p + m - 1) mod N + 1
Code:
N = 18;
p = 2;
m = 3;
for i = p - m : p + m
nb = mod((i - 1) , N) + 1;
disp(nb);
end
Run code here
I would like you to note that you might not necessarily improve performance by avoiding a if statement. A benchmark might be necessary to figure this out. However, this will only be significant if you are treating tens of thousands of numbers.

Matrix manipulation in Octave

I want to map a mX1 matrix X into mXp matrix Y where each row in the new matrix is as follows:
Y = [ X X.^2 X.^3 ..... X.^p]
I tried to use the following code:
Y = zeros(m, p);
for i=1:m
Y(i,:) = X(i);
for c=2:p
Y(i,:) = [Y(i,:) X(i).^p];
end
end
What you want do is called brodcasting. If you are using Octave 3.8 or later, the following will work fine:
octave> X = (1:5)'
X =
1
2
3
4
5
octave> P = (1:5)
P =
1 2 3 4 5
octave> X .^ P
ans =
1 1 1 1 1
2 4 8 16 32
3 9 27 81 243
4 16 64 256 1024
5 25 125 625 3125
The important thing to note is how X and P are a column and row vector respectively. See the octave manual on the topic.
For older of versions of Octave (without automatic broadcasting), the same can be accomplished with bsxfun (#power, X, P)

Date 'wrap' in subtracting months

What is a mathematical way of of saying 1 - 1 = 12 for a month calculation? Adding is easy, 12 + 1 % 12 = 1, but subtraction introduces 0, stuffing things up.
My actual requirement is x = x + d, where x must always be between 1 and 12 before and after the summing, and d any unsigned integer.
Assuming x and y are both in the range 1-12:
((x - y + 11) % 12) + 1
To break this down:
// Range = [0, 22]
x - y + 11
// Range = [0, 11]
(x - y + 11) % 12
// Range = [1, 12]
((x - y + 11) % 12) + 1
I'd work internally with a 0 based month (0-11), summing one for external consumption only (output, another calling method expecting 1-12, etc.), that way you can wrap around backwards just as easily as wrapping around forward.
>>> for i in range(15):
... print '%d + 1 => %d' % (i, (i+1)%12)
...
0 + 1 => 1
1 + 1 => 2
2 + 1 => 3
3 + 1 => 4
4 + 1 => 5
5 + 1 => 6
6 + 1 => 7
7 + 1 => 8
8 + 1 => 9
9 + 1 => 10
10 + 1 => 11
11 + 1 => 0
12 + 1 => 1
13 + 1 => 2
14 + 1 => 3
>>> for i in range(15):
... print '%d - 1 => %d' % (i, (i-1)%12)
...
0 - 1 => 11
1 - 1 => 0
2 - 1 => 1
3 - 1 => 2
4 - 1 => 3
5 - 1 => 4
6 - 1 => 5
7 - 1 => 6
8 - 1 => 7
9 - 1 => 8
10 - 1 => 9
11 - 1 => 10
12 - 1 => 11
13 - 1 => 0
14 - 1 => 1
You have to be careful with addition, too, since (11 + 1) % 12 = 0. Try this:
x % 12 + 1
This comes from using a normalisation function:
norm(x) = ((x - 1) % 12) + 1
Substituting,
norm(x + 1) = (((x + 1) - 1) % 12 + 1
norm(x + 1) = (x) % 12 + 1
The % (modulus) operator produces an answer in the range 0..(N-1) for x % N. Given that your inputs are in the range 1..N (for N = 12), the general adding code for adding a positive number y months to current month x should be:
(x + y - 1) % 12 + 1
When y is 1, this reduces to
x % 12 + 1
Subtracting is basically the same. However, there are complications with the answers produced by different implementations of the modulus operator when either (or both) of the operands is negative. If the number to be subtracted is known to be in in the range 1..N, then you can use the fact that subtracting y modulo N is the same as adding (N - y) modulo N. If y is unconstrained (but positive), then use:
(x + (12 - (y % 12) - 1) % 12 + 1
This double-modulo operation is a common part of the solution to problems like this when the range of the values is not under control.

Resources