Any number multiplies by zero equals to zero but why the factorial number of zero equals 1? - language-agnostic

Mathematically speaking, zero is the absobing element of the multiplication. I would like to know why the factorial number of zero equals 1? Is this general accepted rule?
Can someone reason about this factorial concept based on this factorial formula?
n!=n×(n−1)×(n−2)×………×1
0! = 0 * 0 = 1
1! = 1 * 1 = 1
2! = 2 * 1 = 2
3! = 3 * 2 * 1 = 6
4! = 4 * 3 * 2 * 1 = 24
5! = 5 * 4 * 3 * 2 * 1 = 120
6! = 6 * 5 * 4 * 3 * 2 * 1 = 720

Here is a description with multiple definitions. 0! is always 1, in mathematics as well as programming. It has to do with data sets.

Related

round to the nearest even number with array of numbers

My function and rounding to nearest even number
function y = rndeven(x)
if x<=1
y=2;
else
y = 2*floor(x);
end
endfunction
When I run it I get:
cc=[0:3]'
both=[cc,rndeven(cc)]
0 0
1 2
2 4
3 6
What I'm trying to get as the Result:
0 2
1 2
2 2
3 4
You can use the modulo 2 to find whether a number is even. If it isn't this will return 1, so just add 1 to this number to find the nearest (larger) even number:
function y = rndeven(x)
x = floor(x);
x(x <= 1) = 2;
y = mod(x,2)+x;
end
This works for any array, order of elements does not matter.
You could also check if it is dividable by 2 if you don't want to use the mod function. The pseudo code would be something like this:
while(x % 2 != 0) x = x + 1
return x

Why exactly does this function return a value to some power? (python 2.7)

def power(num, x = 1):
result = 1
for i in range(x):
result = result * num
return result
So I came across a tutorial on calling functions with 2 arguments and this one in the picture was used as an example to show how you could make a function called power(num, x=1) that takes an interval in the first argument and raises it to the power of the second argument. Can someone explain in laymen's terms why this happens and what exactly is going on in this function and 'for' loop?
First, range(x) is equivalent to range(0, x), and generates a sequence that ranges from 0 to x - 1. For example, with range(3) you get the sequence 0, 1, and 2, which has three elements. In general, range(x) generates a sequence that has x elements.
Second, for i in range(x) makes i iterates throught all the elements of range(x). Since range(x) has x elements, i will iterate through x different values, so the statements in the for loop will be executed x times.
With the above analysis, the body of the power function is equivalent to the following:
result = 1
result = result * num
result = result * num
// repeat x times
result = result * num
which is equivalent to:
result = 1 * num * num * ... * num // x nums here
which, apparently, is num raised to the power of x.
Update
Here's how this function works with specific input data. When num is 3 and x is 4, we have:
result = 1
result = result * num // = 1 * 3 = 3
result = result * num // = 3 * 3 = 9
reuslt = result * num // = 9 * 3 = 27
result = result * num // = 27 * 3 = 81 = 3^4
return result // 81 is returned
We can also show the execution process in more details:
result = 1
i = 0 // entering the loop
result = result * num // = 1 * 3 = 3
i = 1 // the second round of the loop begins
result = result * num // = 3 * 3 = 9
i = 2 // the third round of the loop begins
reuslt = result * num // = 9 * 3 = 27
i = 3 // the fourth and final round of the loop begins
result = result * num // = 27 * 3 = 81 = 3^4
// range(4) is exhausted, so the loop ends here
return result // 81 is returned

Is it possible to use logarithms to convert numbers to binary?

I'm a CS freshman and I find the division way of finding a binary number to be a pain. Is it possible to use log to quickly find 24, for instance, in binary?
If you want to use logarithms, you can.
Define log2(b) as log(b) / log(2) or ln(b) / ln(2) (they are the same).
Repeat the following:
Define n as the integer part of log2(b). There is a 1 in the nth position in the binary representation of b.
Set b = b - 2n
Repeat first step until b = 0.
Worked example: Converting 2835 to binary
log2(2835) = 11.47.. => n = 11
The binary representation has a 1 in the 211 position.
2835 - (211 = 2048) = 787
log2(787) = 9.62... => n = 9
The binary representation has a 1 in the 29 position.
787 - (29 = 512) = 275
log2(275) = 8.10... => n = 8
The binary representation has a 1 in the 28 position.
275 - (28 = 256) = 19
log2(19) = 4.25... => n = 4
The binary representation has a 1 in the 24 position.
19 - (24 = 16) = 3
log2(3) = 1.58.. => n = 1
The binary representation has a 1 in the 21 position.
3 - (21 = 2) = 1
log2(1) = 0 => n = 0
The binary representation has a 1 in the 20 position.
We know the binary representation has 1s in the 211, 29, 28, 24, 21, and 20 positions:
2^ 11 10 9 8 7 6 5 4 3 2 1 0
binary 1 0 1 1 0 0 0 1 0 0 1 1
so the binary representation of 2835 is 101100010011.
From a CS perspective, binary is quite easy because you usually only need to go up to 255. Or 15 if using HEX notation. The more you use it, the easier it gets.
How I do it on the fly, is by remembering all the 2 powers up to 128 and including 1. (The presence of the 1 instead of 1.4xxx possibly means that you can't use logs).
128,64,32,16,8,4,2,1
Then I use the rule that if the number is bigger than each power in descending order, that is a '1' and subtract it, else it's a '0'.
So 163
163 >= 128 = '1' R 35
35 !>= 64 = '0'
35 >= 32 = '1' R 3
3 !>= 16 = '0'
3 !>= 8 = '0'
3 !>= 4 = '0'
3 >= 2 = '1' R 1
1 >= 1 = '1' R 0
163 = 10100011.
It may not be the most elegant method, but when you just need to convert something ad-hoc thinking of it as comparison and subtraction may be easier than division.
Yes, you have to loop through 0 -> power which is bigger than you need and then take the remainder and do the same, which is a pain too.
I would suggest you trying recursion approach of division called 'Divide and Conquer'.
http://web.stanford.edu/class/archive/cs/cs161/cs161.1138/lectures/05/Small05.pdf
But again, since you need a binary representation, I guess unless you use ready utils, division approach is the simplest one IMHO.

Converting decimal floating number to binary

Can anyone please help me convert number (for example) 143,625 to binary? I've been searching through net for quite a long time but didn't find anything with good explanation.
Thanks in advance!
The integer part can be done by dividing by 2 repeatedly and keeping track of the remainder:
143 / 2 = 71 remainder 1
71 / 2 = 35 remainder 1
35 / 2 = 17 remainder 1
17 / 2 = 8 remainder 1
8 / 2 = 4 remainder 0
4 / 2 = 2 remainder 0
2 / 2 = 1 remainder 0
1 / 2 = 0 remainder 1
So the integer part is 10001111
For the fractional part, multiply it by 2 repeatedly and look at the integer part of the result:
.625 x 2 = 1.25 - we need the integer part, which is 1 - so far we have 0.1, we then disregard the 1 and look at 0.25
0.25 x 2 = 0.5 - so far we have 0.10, we look at 0.5
0.5 x 2 = 1.0 - we have 0.101, no decimal part so we're good.
The whole number is the integer part + the decimal part, so 10001111.101

bitwise AND in arithmetic operation

Bitwise OR(|) is similar to arithmetic addition(+) i.e. A|B = A+B (if A!=B)
Like, 2|4 = 6 and 2+4 = 6
That means there is a way to get "OR" result in by doing addition in arithmetic context.
Is there a similar way to get bitwise "AND" result by doing arithmetic operation.
i.e. A&B = aithmetic_op(A,B)
A = 2^k (k=1,2,3,4...)
B = 2^k (k=1,2,3,4...)
Thanks
Just as OR is analogous to +, AND is analogous to *
0 AND 0 = 0 * 0 = 0
0 AND 1 = 0 * 1 = 0
1 AND 0 = 1 * 0 = 0
1 AND 1 = 1 * 1 = 1
Note that this only works for a single bit (as does the analogy between OR and + that you mention), due to the effects of arithmetic carries.