what is the complexity of the following code? [closed] - c++

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 10 years ago.
What is the complexity of the following piece of code??
for (int i = 1; i * i <= n; i++)
{
if (n%i == 0)
//do anything
}

The loop runs √n times, and the conditional is met every time i is a factor of n — the latter is a non-trivial condition and needs to be analysed carefully. It depends on the prime factorisation of n. For example, if n is prime, the condition is only true once, for i == 1, and never again.

Related

Execution Control In C [closed]

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 9 years ago.
I was solving a problem where i was asked to output the list of numbers satisfying certain conditions. The score awarded depended upon the size of the output ( as it is a partial marking question). How do i restrict my code to keep outputting the numbers till it does hits the time limit.
It seems like the obvious structure would be something like this:
while (current_time < end_time) {
current_number = *next_number++;
if (meets_conditions(current_number))
output(current_number);
}

Asymptotic Estimate for integer division [closed]

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 9 years ago.
k = n; //integer division
while(k > 1) {
std::cout << k;
k=k/2;
}
I need to find out the asymptotic estimate as a function of n.
The complexity is logarithmic.
Assuming K is non-negative, division by two is equivalent to shifting right by one bit. Therefore, the maximum number of iterations before k becomes 0 is the number of bits in k. More specifically, the position of the most significant bit in k that is set (i.e., a 1) will determine the number of iterations executed in the loop.
Since the operations in the loop are (presumably) constant complexity, logarithmic number of iterations leads directly to logarithmic complexity.

Which is more optimized code? [closed]

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 10 years ago.
// (1)
for (int iter = 1; iter <= VERTEX_SIZE; iter ++) {
if (visit[iter]) continue;
dfs(iter);
}
// (2)
for (int iter = 1; iter <= VERTEX_SIZE; iter ++) {
if (!visit[iter]) {
dfs(iter);
}
}
Which code is more optimized? I'm just curious about it.
With any even semi-decent compiler this won’t make a difference at all; the generated machine code will be exactly the same. If you still want to be sure, benchmark it.
Go for the one you find most readable.
There is no difference. Pick the one that you thing is the best. Every program you compile with the first way will definitely work with the second way too, and the running time will be the same.
Note: I would use the second way; I thing it's more readable and the majority of implementations of DFS I have ever seen (in C++ and in pheudocode) uses it.

Palindrome without using extra space [closed]

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 10 years ago.
I know various ways to check if an integer is a palindrome or not by using string conversion, stack and number breaking, but here question is
"How can we check wether an integer is a palindrome or not, without using any extra space?"
You can revert a number with some code like:
int revert(int num) {
int reverted = 0;
while (num) {
reverted = reverted*10 + num%10;
num /= 10;
}
return reverted;
}
And now you only check if
num == revert(num)
That is all. Sorry for giving the exact solution instead of just a tip, but I don't think I could have given any tip without the solution itself.

How do I make a smaller range with two given integers? [closed]

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 11 years ago.
For example,
I have a range 14269-14274.
To conserve space on the screen my users want to have it display in the format 14269-74.
Another example would be a range of 14269-14529 which should output as 14269-529.
How would I achieve this?
Something like this should do the trick:
int a = 14269;
int b = 14529;
int endrange = b % pow(10, floor(log10(b - a) + 1));
You need to make sure that a < b though.
You can check the first digit that differs, output the first number and then the second one, starting at the first different digit.
This of course only makes sense if the two numbers have the same length.
Were you expecting the implementation?