0

I am sorry for asking such a Dummy question but I can't seem to find the answer. I know how to calculate the complexity of an algorithm ( O() ) when there are loops'n stuff, but in this case I have difficulty wrapping my head around it. The language is C++.

Here's the code :

int calculate(int k, int n){ // Code C++
    int firstSequenceEnd = k-1;
    int sumAk = ((1 + (firstSequenceEnd))*(firstSequenceEnd) ) >> 1;
    return (1 << n-k)*sumAk;
}

Thank you in advance !

5
  • Well, how many instructions get executed? Commented Jun 29, 2014 at 16:47
  • Following @OliCharlesworth's question: in this code the number steps does not depend on the input. It does not loop, does not recurse, just a static calculation. Commented Jun 29, 2014 at 16:50
  • That is kinda my question, does 1 << n-k counts as one or n-k ? Commented Jun 29, 2014 at 16:50
  • It's two instruction independently on the size of n or k. Commented Jun 29, 2014 at 16:51
  • "there are loops'n stuff" I see no loops'n stuff. Commented Jun 29, 2014 at 16:52

1 Answer 1

4

When there are no loops then we are talking about O(1).

Sign up to request clarification or add additional context in comments.

2 Comments

This is overly-simplistic; recursive code will generally be greater than O(1) for example, even without loops.
@OliCharlesworth I agree with your comment, But there is no recursion on his example. And a recursion will be transferred (on machine code ) to loop (based on the stack but still - a loop)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.