I know the concept of division by 2 & then taking the remainder but I want to know how this method actually works. I want a mathematical derivation for this.
The mathematical derivation you are asking for is based on the remainder theorem, which states that:
Dividend = Divisor*Quotient + Remainder
Now consider a decimal number X. We can express X in binary form as follows:
X= a*2^0 + b*2^1 + c*2^2 + d*2^3 .........................
Our aim is to find the values of coefficients a,b,c,d... to express the number in binary form.
Now if you divide X by 2, you will get 'a' as the remainder & the corresponding quotient would be (b*2^0 + c*2^1 + d*2^2 ........)
Now if we divide the above quotient again by 2 we will get 'b' as the remainder & this cycle will go on until we get all coefficients which will give us the final binary form.
Related
I am having some difficulty understanding why this recursive algorithm works mathematically for converting decimal to binary.
I understand that the last digit in binary code determines if the decimal number is odd or even, thus we can use the remainder(%) to determine if the last digit is 0(odd) or 1(even). However, my thought process stops there, and I am confused about why the digit in position for 2 to the higher power can be calculated by dividing the decimal by 2 and then check its %2 as shown in the picture.
Can someone help me understand the logic behind the conversion? Thank you very much!
This is the way to calculate a number in accord to a base, you can do it for every base b>1. for example the same number should be expressed in base b = 3
Start Conversion from base 10 to base 3
26/3 = 8 + 2
8/3 = 2 + 2
(26)_10 = (222)_3
Proof
2*3^2 + 2*3 + 2*1 = 18+6+2 = 26
I have been reading a Elements of Programming Interview and am struggling to understand the passage below:
"The algorithm taught in grade-school for decimal multiplication does
not use repeated addition- it uses shift and add to achieve a much
better time complexity. We can do the same with binary numbers- to
multiply x and y we initialize the result to 0 and iterate through the
bits of x, adding (2^k)y to the result if the kth bit of x is 1.
The value (2^k)y can be computed by left-shifting y by k. Since we
cannot use add directly, we must implement it. We can apply the
grade-school algorithm for addition to the binary case, i.e, compute
the sum bit-by-bit and "rippling" the carry along.
As an example, we show how to multiply 13 = (1101) and 9 = (1001)
using the algorithm described above. In the first iteration, since
the LSB of 13 is 1, we set the result to (1001). The second bit of
(1101) is 0, so we move on the third bit. The bit is 1, so we shift
(1001) to the left by 2 to obtain (1001001), which we add to (1001) to
get (101101). The forth and final bit of (1101) is 1, so we shift
(1001) to the left by 3 to obtain (1001000), which we add to (101101)
to get (1110101) = 117.
My Questions are:
What is the overall idea behind this, how is it a "bit-by-bit" addition
where does (2^k)y come from
what does it mean by "left-shifting y by k"
In the example, why do we set result to (1001) just because the LSB of 13 is 1?
The algorithm relies on the way numbers are coded in binary.
Let A be an unsigned number. A is coded by a set of bits an-1an-2...a0 in such a way that A=∑i=0n-1ai×2i
Now, assume you have two numbers A and B coded in binary and you wand to compute A×B
B×A=B×∑i=0n-1ai×2i
=∑i=0n-1B×ai×2i
ai is equal to 0 or 1. If ai=0, the sum will not be modified. If ai=1, we need to add B×ai
So, we can simply deduce the multiplication algorithm
result=0
for i in 0 to n-1
if a[i]=1 // assumes a[i] is the ith bit
result = result + B * 2^i
end
end
What is the overall idea behind this, how is it a "bit-by-bit" addition
It is just an application of the previous method where you process successively every bit of the multiplicator
where does (2^k)y come from
As mentioned above from the way binary numbers are coded. If ith bit is set, then there is a 2i in the decomposition of the number.
what does it mean by "left-shifting y by k"
Left shift means "pushing" the bits leftwards and filling the "holes" with zeroes. Hence if number is 1101 and it is left shifted by three, it becomes 1101000.
This is the way to multiply the number by 2i (just as when "left shifting" by 2 a decimal number and putting zeroes at the right places is the way to multiply by 100=102)
In the example, why do we set result to (1001) just because the LSB of 13 is 1?
Because there is a 1 at right most position, that corresponds to 20. So we left shift by 0 and add it to the result that is initialized to 0.
I've been getting myself acquainted with the Pollard Rho factorization from this page.
I think I understand almost everything there, but one thing I'm confused about – the fact that it uses f(x) = x^2 + a mod N to generate pseudo-random numbers for checking.
My question is, why can't we simply have a random number generator give us some two random numbers (xi, xj) each time, where xi, xj < N?
Why use this function f(x)?
The particular random number generator doesn't matter. Pollard in his original paper describing the algorithm says "Other polynomials of degree ≥ 2 and other starting values can be used." Brent says "The choice of a (pseudo-) random u ∈ [0,1) is not essential; it merely makes the average-case analysis tractable. Pollard and Brent describe the use of a function other than x2 + c to factor 228 + 1. The advantage of the x2 + c method is that it is simple to implement and gives a family of polynomials, making it easy to switch to another one if the first doesn't work.
I want to design a binary full adder to add 3 binary numbers ,
a typical cell of this adder would look like this
Can someone explain why we have 2 carries to the next bit ?
regards
Let's look at a particular formula: 0b11 + 0b11 + 0b11 == 0b1001.
The schematic of this would look like:
Adder 0 has the following properties:
Normal inputs can total to at most 0b11.
Carried inputs should always be 0b00.
The maximum output is 0b11 (One carry bit, one output bit).
Adder 1 has the following properties:
Normal inputs can total to at most 0b11.
Carried inputs can total to 0b01.
Maximum output is 0b100 (Two carry bits, one output bit).
My Calculus teacher gave us a program on to calculate the definite integrals of a given interval using the trapezoidal rule. I know that programmed functions take an input and produce an output as arithmetic functions would but I don't know how to do the inverse: find the input given the output.
The problem states:
"Use the trapezoidal rule with varying numbers, n, of increments to estimate the distance traveled from t=0 to t=9. Find a number D for which the trapezoidal sum is within 0.01 unit of this limit (468) when n > D."
I've estimated the limit through "plug and chug" with the calculator and I know that with a regular algebraic function, I could easily do:
limit (468) = algebraic expression with variable x
(then solve for x)
However, how would I do this for a programmed function? How would I determine the input of a programmed function given output?
I am calculating the definite integral for the polynomial, (x^2+11x+28)/(x+4), between the interval 0 and 9. The trapezoidal rule function in my calculator calculates the definite integral between the interval 0 and 9 using a given number of trapezoids, n.
Overall, I want to know how to do this:
Solve for n:
468 = trapezoidal_rule(a = 0, b = 9, n);
The code for trapezoidal_rule(a, b, n) on my TI-83:
Prompt A
Prompt B
Prompt N
(B-A)/N->D
0->S
A->X
Y1/2->S
For(K,1,N-1,1)
X+D->X
Y1+S->S
End
B->X
Y1/2+S->S
SD->I
Disp "INTEGRAL"
Disp I
Because I'm not familiar with this syntax nor am I familiar with computer algorithms, I was hoping someone could help me turn this code into an algebraic equation or point me in the direction to do so.
Edit: This is not part of my homework—just intellectual curiosity
the polynomial, (x^2+11x+28)/(x+4)
This is equal to x+7. The trapezoidal rule should give exactly correct results for this function! I'm guessing that this isn't actually the function you're working with...
There is no general way to determine, given the output of a function, what its input was. (For one thing, many functions can map multiple different inputs to the same output.)
So, there is a formula for the error when you apply the trapezoidal rule with a given number of steps to a given function, and you could use that here to work out the value of n you need ... but (1) it's not terribly beautiful, and (2) it doesn't seem like a very reasonable thing to expect you to do when you're just starting to look at the trapezoidal rule. I'd guess that your teacher actually just wanted you to "plug and chug".
I don't know (see above) what function you're actually integrating, but let's pretend it's just x^2+11x+28. I'll call this f(x) below. The integral of this from 0 to 9 is actually 940.5. Suppose you divide the interval [0,9] into n pieces. Then the trapezoidal rule gives you: [f(0)/2 + f(1*9/n) + f(2*9/n) + ... + f((n-1)*9/n) + f(9)/2] * 9/n.
Let's separate this out into the contributions from x^2, from 11x, and from 28. It turns out that the trapezoidal approximation gives exactly the right result for the latter two. (Exercise: Work out why.) So the error you get from the trapezoidal rule is exactly the same as the error you'd have got from f(x) = x^2.
The actual integral of x^2 from 0 to 9 is (9^3-0^3)/3 = 243. The trapezoidal approximation is [0/2 + 1^2+2^2+...+(n-1)^2 + n^2/2] * (9/n)^2 * (9/n). (Exercise: work out why.) There's a standard formula for sums of consecutive squares: 1^2 + ... + n^2 = n(n+1/2)(n+1)/3. So our trapezoidal approximation to the integral of x^2 is (9/n)^3 times [(n-1)(n-1/2)n/3 + n^2/2] = (9/n)^3 times [n^3/3+1/6] = 243 + (9/n)^3/6.
In other words, the error in this case is exactly (9/n)^3/6 = (243/2) / n^3.
So, for instance, the error will be less than 0.01 when (243/2) / n^3 < 0.01, which is the same as n^3 > 100*243/2 = 12150, which is true when n >= 23.
[EDITED to add: I haven't checked any of my algebra or arithmetic carefully; there may be small errors. I take it what you're interested is the ideas rather than the specific numbers.]