What is the Big O complexity for this division algorithm - algorithm

Input: Two n-bit integers x and y, where x ≥ 0, y ≥ 1.
Output: The quotient and remainder of x divided by y.
if x = 0, then return (q, r) := (0, 0);
q := 0; r := x;
while (r ≥ y) do
{ q := q + 1;
r := r – y};
return (q, r);
I have obtained the Big O complexity as O(n^2) but a friends says it is O(2^n) where n is the number of bits as the input size
Please provide explanation

The number of iterations of the while-loop is exactly floor(x/y). Each iteration takes n operations, because that is the complexity of the subtraction r - y.
Hence the complexity of the algorithm is n * floor(x/y). However, we want to express the complexity as a function of n, not as a function of x and y.
Thus the question becomes: how does floor(x/y) relate to n, in the worst case?
The biggest value that can be obtained for x/y when x and y are two nonnegative n-digits numbers, and y >= 1, is obtained by taking the biggest possible value for x, and the smallest possible value for y.
The biggest possible value for x is x = 2**n - 1 (all bits of x are 1 in its binary representation);
The smallest possible value for y is y = 1.
Hence the biggest possible value for x/y is x/y = 2**n - 1.
The time-complexity of your division algorithm is O(n * 2**n), and this upper-bound is achieved when x = 2**n - 1 and y = 1.

My proposed solution:
When calculating Big O complexity we need to take n->infinity, where n is > input size, we have 3
possibilities:
x & y both become infinity when n->infinity
y becomes infinity when n->infinity
x becomes infinity when n-> infinity
We are interested only in case 3 here,
(x – i * y ) < y, i being the number of iterations
also written as x/y < i + 1
when x becomes infinity LHS(Left hand side) is infinity in this
equation which implies RHS is infinity as well
So as n-> infinity the number iterations becomes equal to n
Hence, the complexity is O(n^2)

Related

Time complexity of iterative division algorithm

Input: Two n-bit integers x and y, where y ≥ 1.
Output: The quotient and remainder of x divided by y.
if x = 0, then return (q, r) := (0, 0);
q := 0; r := x;
while (r ≥ y) do // takes n iterations for the worse case.
{
q := q + 1;
r := r – y
}; // O(n) for each r – y, where y is n bits long.
return (q, r);
For the above algorihtm which mainly focuses on dividing two 'n' bit integers 'x' and 'y', can somone please explain and let me know the time complexity in terms of 'n'.
Can't guarantee my explanation is flawless, but here it is anyway:
Just as written in comments, the time complexity is O(n), i.e. linear. In other words, the time it takes for the computer to execute the algorithm increases linearly with the increase of x or the decrease of y.
Example: it will take 3 loops to get the result if x = 10 and y = 3, and it will take twice as many loops if we increase x by two to equal 20. That's what they call O(n).
Other time complexity types that exist are O(n^2) - quadratic time (ex: you increase x by 2 and the number of required loops increases by 4), O(n^3) - cubic (same logic), O(logn) - logarithmic time (the time is increasing less than linearly and at some point almost stops to increase at all).
And finally, there's O(1) - constant time. The above algorithm can be improved to have constant time if you type q=x/y and r=x%y instead of using the loops.

Euclid's Algorithm Time Complexity

I have a question about the Euclid's Algorithm for finding greatest common divisors.
gcd(p,q) where p > q and q is a n-bit integer.
I'm trying to follow a time complexity analysis on the algorithm (input is n-bits as above)
gcd(p,q)
if (p == q)
return q
if (p < q)
gcd(q,p)
while (q != 0)
temp = p % q
p = q
q = temp
return p
I already understand that the sum of the two numbers, u + v where u and v stand for initial values of p and q , reduces by a factor of at least 1/2.
Now let m be the number of iterations for this algorithm.
We want to find the smallest integer m such that (1/2)^m(u + v) <= 1
Here is my question.
I get that sum of the two numbers at each iteration is upper-bounded by (1/2)^m(p + q). But I don't really see why the max m is reached when this quantity is <= 1.
The answer is O(n) for n-bits q, but this is where I'm getting stuck.
Please help!!
Imagine that we have p and q where p > q. Now, there are two cases:
1) p >= 2*q: in this case, p will be reduced to something less than q after mod, so the sum will be at most 2/3 of what is was before.
2) q < p < 2*q: in this case, a mod operation will be like subtracting q from p, so again the overall sum will be at most 2/3 of what is was before.
Therefore, in each step this sum will be 2/3 of the last sum. Since your numbers are n bits the magnitude of the sum is 2^{n+1}; so, with log 2^{n+1} (base 3/2) steps which is actually O(n), the sum will be 0.

Finding remainder of division the sum of the given diapason and 987654321

Could you help me please ? I need a fast algorithm for calculating the following : the remainder of division the sum of the integers in the power from given range ( from A to B , 1 < A,B < 10^8 ) and 987654321;
For instance , if I have A = 10 , B = 15, I should calculate
((11^11) + (12^12) + (13^13) + (14^14) ) % 987654321
If I use this direct approach, it takes forever to calculate this. Is there a trick to calculate such kind of remainders?
Using fast modulo exponentiation, we can calculate x^n in O(log(n)) time. In the worst case, if A = 1 and B = n where n can be upto 10^8, then the total complexity will be around
log(2) + log(3) + log(4) + ... + log(n)
= log(n!)
~ n*log(n) - n + O(log(n)) (According to Striling's Approximation)
Wikipedia
Fast Modulo Exponentiation
This method is used to quickly calculate powers of the form x^n (in O(log(n)) time).
It can be given as a recurrence relation:
x^n = (x^2)^(n/2) if n is even
= x*{(x^2)^(n/2)} if n is odd
So, essentially instead of multiplying x n times, we do the following:
x = x^2;
n = n/2;
time till we reach a trivial case, where n = 1.
Python code (with modulo for this case):
def fast(x, n, mod):
if n == 1:
return x % mod
if n % 2 == 0:
return fast(x**2 % mod, n/2, mod)
else:
return x*fast(x**2 % mod, (n-1)/2, mod) % mod

Is this algorithm's Big oh is n^3 rather than n^2

def triangles(A):
n = len(A)
result = 0
for x in xrange(n):
z=x+2
for y in xrange(x + 1, n):
while (z < n and A[x] + A[y] > A[z]):
z += 1
result += z - y - 1
return result
This is an example of the solution in the codility (https://codility.com/media/train/13-CaterpillarMethod.pdf)
In the manual, they claim that the big-Oh of this algorithm is O(N^2)
Bout I think big-Oh is O(N^3) as the worst case's iteration will be
(n-1)(n-2) + (n-2)(n-3) + ..... + 1*0
so I think its big-Oh will be O(N^3).
Can anybody explain why the big-Oh of this algorithm is O(N^2)?
or am I right?
The while loop over z will, accumulately, only run once (<n times) for each execution of the for loop over y, since z is reset only in the scope outside of the for loop over y. Hence a rough upper bound loop count of "inner scope" is along the lines n*(n+n) (which is in O(n^2)) and not n*n*n. W.r.t. calculating complexity, we might as well consider the while loop over z as a loop parallell to the for loop over y, rather than one nested within the for loop over y.
I.e., same complexity as e.g.:
def triangles(A):
n = len(A)
result = 0
for x in xrange(n):
z=x+2
while (z < n)
z += 1
for y in xrange(x + 1, n):
...
return result
For each value of x, z can only be increased up to n times. Once z is equal to n then no iterations of the inner loop is possible until x is increased again. So n values of x with n iterations of the inner loop gives n^2.

Minimize number of divisors of an integer within an interval

I have recently stumbled upon an algorithmic problem and I can't get the end of it. You're given a positive integer N < 10^13, and you need to choose a nonnegative integer M, such that the sum: MN + N(N-1) / 2 has the least number of divisors that lie between 1 and N, inclusive.
Can someone point me to the right direction for solving this problem?
Thank you for your time.
Find a prime P greater than N. There are a number of ways to do this.
If N is odd, then M*N + N*(N-1)/2 is a multiple of N. It must be divisible by any factor of N, but if we choose M = P - (N-1)/2, then M*N + N*(N-1)/2 = P*N, so it isn't divisible by any other integers between 1 and N.
If N is even, then M*N + N*(N-1)/2 is a multiple of N/2. It must be divisible by any factor of N/2, but if we choose M = (P - N + 1)/2 (which must be an integer), then M*N + N*(N-1)/2 = (P - N + 1)*N/2 + (N-1)*N/2 = P*N/2, so it isn't divisible by any other integers between 1 and N.

Resources