Big-O notation of 1+2+3+…+n
I'm currently a CS undergrad enrolled in a data structures course. During the semester, we learned about big-O notation, and on one assignment, we had to write out the big-O notation of summing the numbers 1+2+3+...+n. I figured that, in the simplest method, you would be be looping from 1 to n and in each iteration adding i to the sum, so it seemed like this would be O(n) time.
I am also aware that this specific summation can be expressed as (n(n+1))/2 as a more direct way to receive the answer.
My professor insists that in both cases, the time complexity is O(n^2), and I have been emailing him back and forth hoping to get a better explanation, but he basically sends the same reply every time.
I feel like I must be misunderstanding the purpose of big-O in the first place. Even when I implement these 2 methods of finding the sum in a program and time the execution, the time of the loop method seems to increase linearly based on the size of n, and in the second method, it takes the same amount of time no matter how large n is because there is no iteration occurring in this case.
Could someone please help me understand why this would still be O(n^2)?
time-complexity big-o
add a comment |
I'm currently a CS undergrad enrolled in a data structures course. During the semester, we learned about big-O notation, and on one assignment, we had to write out the big-O notation of summing the numbers 1+2+3+...+n. I figured that, in the simplest method, you would be be looping from 1 to n and in each iteration adding i to the sum, so it seemed like this would be O(n) time.
I am also aware that this specific summation can be expressed as (n(n+1))/2 as a more direct way to receive the answer.
My professor insists that in both cases, the time complexity is O(n^2), and I have been emailing him back and forth hoping to get a better explanation, but he basically sends the same reply every time.
I feel like I must be misunderstanding the purpose of big-O in the first place. Even when I implement these 2 methods of finding the sum in a program and time the execution, the time of the loop method seems to increase linearly based on the size of n, and in the second method, it takes the same amount of time no matter how large n is because there is no iteration occurring in this case.
Could someone please help me understand why this would still be O(n^2)?
time-complexity big-o
1
It's coming from him telling me that the answer depends on the number of operations we are performing, and in the case of (n(n+1)/2, I only see 3 operations happening there. If he would take the time to properly explain it instead of sending me "the answer is O(n^2) because it is (n(n+1))/2" as a reply no matter how many times I ask him to explain.
– sode
Nov 21 '18 at 15:22
add a comment |
I'm currently a CS undergrad enrolled in a data structures course. During the semester, we learned about big-O notation, and on one assignment, we had to write out the big-O notation of summing the numbers 1+2+3+...+n. I figured that, in the simplest method, you would be be looping from 1 to n and in each iteration adding i to the sum, so it seemed like this would be O(n) time.
I am also aware that this specific summation can be expressed as (n(n+1))/2 as a more direct way to receive the answer.
My professor insists that in both cases, the time complexity is O(n^2), and I have been emailing him back and forth hoping to get a better explanation, but he basically sends the same reply every time.
I feel like I must be misunderstanding the purpose of big-O in the first place. Even when I implement these 2 methods of finding the sum in a program and time the execution, the time of the loop method seems to increase linearly based on the size of n, and in the second method, it takes the same amount of time no matter how large n is because there is no iteration occurring in this case.
Could someone please help me understand why this would still be O(n^2)?
time-complexity big-o
I'm currently a CS undergrad enrolled in a data structures course. During the semester, we learned about big-O notation, and on one assignment, we had to write out the big-O notation of summing the numbers 1+2+3+...+n. I figured that, in the simplest method, you would be be looping from 1 to n and in each iteration adding i to the sum, so it seemed like this would be O(n) time.
I am also aware that this specific summation can be expressed as (n(n+1))/2 as a more direct way to receive the answer.
My professor insists that in both cases, the time complexity is O(n^2), and I have been emailing him back and forth hoping to get a better explanation, but he basically sends the same reply every time.
I feel like I must be misunderstanding the purpose of big-O in the first place. Even when I implement these 2 methods of finding the sum in a program and time the execution, the time of the loop method seems to increase linearly based on the size of n, and in the second method, it takes the same amount of time no matter how large n is because there is no iteration occurring in this case.
Could someone please help me understand why this would still be O(n^2)?
time-complexity big-o
time-complexity big-o
asked Nov 21 '18 at 15:04
sodesode
413
413
1
It's coming from him telling me that the answer depends on the number of operations we are performing, and in the case of (n(n+1)/2, I only see 3 operations happening there. If he would take the time to properly explain it instead of sending me "the answer is O(n^2) because it is (n(n+1))/2" as a reply no matter how many times I ask him to explain.
– sode
Nov 21 '18 at 15:22
add a comment |
1
It's coming from him telling me that the answer depends on the number of operations we are performing, and in the case of (n(n+1)/2, I only see 3 operations happening there. If he would take the time to properly explain it instead of sending me "the answer is O(n^2) because it is (n(n+1))/2" as a reply no matter how many times I ask him to explain.
– sode
Nov 21 '18 at 15:22
1
1
It's coming from him telling me that the answer depends on the number of operations we are performing, and in the case of (n(n+1)/2, I only see 3 operations happening there. If he would take the time to properly explain it instead of sending me "the answer is O(n^2) because it is (n(n+1))/2" as a reply no matter how many times I ask him to explain.
– sode
Nov 21 '18 at 15:22
It's coming from him telling me that the answer depends on the number of operations we are performing, and in the case of (n(n+1)/2, I only see 3 operations happening there. If he would take the time to properly explain it instead of sending me "the answer is O(n^2) because it is (n(n+1))/2" as a reply no matter how many times I ask him to explain.
– sode
Nov 21 '18 at 15:22
add a comment |
4 Answers
4
active
oldest
votes
The answer is O(n) if you do the summation by iterating over the numbers one by one.
The answer is O(1) if you use the formula for summation directly.
add a comment |
Is it possible that this is a misunderstanding and that 1, 2, ..., n are time values of another operation that is performed, meaning that the time to execute that operation constantly increases, and you have to give the Big-O for this sequence of operations?
Otherwise, when the task is really just summing up all numbers from 1 to n and express that time in Big-O-notation, your opinion is correct that it is an O(n) when you take the approach looping over all elements, and using the n*(n+1)/2-formula you get even O(1).
Thanks for the quick reply, the question taken exactly off the assignment is "Which of these is the correct big - O expression for 1+2+3+...+n? A. O(log n) B. O(n) C. O(n log n) D. O(n²)", so there wasn't even an option for constant time which makes it weird that he even brought up that formula in the first place.
– sode
Nov 21 '18 at 15:17
Ok, I guess then your professor is right. I'm not a native english speaker, but the question is not to give the big-O-expression for the time needed to calculate that sum, but 1+2+...+n is the value, depending on n. And, as you correctly stated, this is n*(n+1)/2 which is (n^2)/2 + n/2, so O(n^2) is the correct answer.
– Stefan Illner
Nov 21 '18 at 15:22
That may be the case, but he keeps bringing up the number of operations occurring and iterations, but if he only means the result itself, then him bringing these things up is just making it more confusing. I know he did not make this question himself because you can find it all over google, and he is also not a native English speaker, so this could possibly be a misunderstanding on both of our parts.
– sode
Nov 21 '18 at 15:41
add a comment |
You are computing the order of the wrong value.
As you indicated in a comment, the question does not ask what's the time complexity of doing the sum; the question asks what is the order of the sum itself. And indeed 1 +
2 + ... + n is O(n²).
Thanks for the answer, but in a response to one of my emails when I had asked if we were only concerned with the order and not time complexity, he said that this problem had to do with time complexity and the number of operations being performed.
– sode
Nov 21 '18 at 15:28
add a comment |
In a Java example like this
int n = 1000;
int sum = 0;
// iterating n times
for (int i = 1; i <= n; i++) {
// just a basic operation, so no extra complexity here
sum += i;
}
the addition is called n
times, so the whole code has a time complexity of O(1) * n = O(n).
If there is nothing missing in your question, O(n) will be the correct answer to the task.
Anyway, there is a good chance the professor is right ;-)
O(n * (n + 1) / 2) = O(n / 2) * O((n + 1) / 2) = O(n) * O(n + 1) = O(n) * O(n) = O(n * n) = O(n^2)
I really don't know what to think now, his latest reply to me was "each iteration requires i operations, so all together we have n(n+1)/2 operations" and I am just getting more confused because in the case of that formula, there is no iteration occurring afaik?
– sode
Nov 21 '18 at 15:39
It would be nice to know what happens in each iteration (the professor is thinking of)... I hope he does not meann
iterations of summing up values from1
ton
...
– deHaar
Nov 21 '18 at 15:40
I'm not entirely sure, and getting any reply out of him other than that the answer is O(n^2) is seeming pretty unlikely
– sode
Nov 21 '18 at 15:43
Giving you the right answer without an explanation is a really weird way to teach... Then give the right answer, pass the assignment and discuss with others. I think there is no different option apart from talking to different professors, maybe.
– deHaar
Nov 21 '18 at 15:46
1
summing input using formula (n * (n + 1))/2 complexity is O(1). Why do you use Big-O notation there?
– kriss
Nov 21 '18 at 15:48
|
show 2 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414918%2fbig-o-notation-of-123-n%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
The answer is O(n) if you do the summation by iterating over the numbers one by one.
The answer is O(1) if you use the formula for summation directly.
add a comment |
The answer is O(n) if you do the summation by iterating over the numbers one by one.
The answer is O(1) if you use the formula for summation directly.
add a comment |
The answer is O(n) if you do the summation by iterating over the numbers one by one.
The answer is O(1) if you use the formula for summation directly.
The answer is O(n) if you do the summation by iterating over the numbers one by one.
The answer is O(1) if you use the formula for summation directly.
answered Nov 21 '18 at 15:19
displayNamedisplayName
9,02133557
9,02133557
add a comment |
add a comment |
Is it possible that this is a misunderstanding and that 1, 2, ..., n are time values of another operation that is performed, meaning that the time to execute that operation constantly increases, and you have to give the Big-O for this sequence of operations?
Otherwise, when the task is really just summing up all numbers from 1 to n and express that time in Big-O-notation, your opinion is correct that it is an O(n) when you take the approach looping over all elements, and using the n*(n+1)/2-formula you get even O(1).
Thanks for the quick reply, the question taken exactly off the assignment is "Which of these is the correct big - O expression for 1+2+3+...+n? A. O(log n) B. O(n) C. O(n log n) D. O(n²)", so there wasn't even an option for constant time which makes it weird that he even brought up that formula in the first place.
– sode
Nov 21 '18 at 15:17
Ok, I guess then your professor is right. I'm not a native english speaker, but the question is not to give the big-O-expression for the time needed to calculate that sum, but 1+2+...+n is the value, depending on n. And, as you correctly stated, this is n*(n+1)/2 which is (n^2)/2 + n/2, so O(n^2) is the correct answer.
– Stefan Illner
Nov 21 '18 at 15:22
That may be the case, but he keeps bringing up the number of operations occurring and iterations, but if he only means the result itself, then him bringing these things up is just making it more confusing. I know he did not make this question himself because you can find it all over google, and he is also not a native English speaker, so this could possibly be a misunderstanding on both of our parts.
– sode
Nov 21 '18 at 15:41
add a comment |
Is it possible that this is a misunderstanding and that 1, 2, ..., n are time values of another operation that is performed, meaning that the time to execute that operation constantly increases, and you have to give the Big-O for this sequence of operations?
Otherwise, when the task is really just summing up all numbers from 1 to n and express that time in Big-O-notation, your opinion is correct that it is an O(n) when you take the approach looping over all elements, and using the n*(n+1)/2-formula you get even O(1).
Thanks for the quick reply, the question taken exactly off the assignment is "Which of these is the correct big - O expression for 1+2+3+...+n? A. O(log n) B. O(n) C. O(n log n) D. O(n²)", so there wasn't even an option for constant time which makes it weird that he even brought up that formula in the first place.
– sode
Nov 21 '18 at 15:17
Ok, I guess then your professor is right. I'm not a native english speaker, but the question is not to give the big-O-expression for the time needed to calculate that sum, but 1+2+...+n is the value, depending on n. And, as you correctly stated, this is n*(n+1)/2 which is (n^2)/2 + n/2, so O(n^2) is the correct answer.
– Stefan Illner
Nov 21 '18 at 15:22
That may be the case, but he keeps bringing up the number of operations occurring and iterations, but if he only means the result itself, then him bringing these things up is just making it more confusing. I know he did not make this question himself because you can find it all over google, and he is also not a native English speaker, so this could possibly be a misunderstanding on both of our parts.
– sode
Nov 21 '18 at 15:41
add a comment |
Is it possible that this is a misunderstanding and that 1, 2, ..., n are time values of another operation that is performed, meaning that the time to execute that operation constantly increases, and you have to give the Big-O for this sequence of operations?
Otherwise, when the task is really just summing up all numbers from 1 to n and express that time in Big-O-notation, your opinion is correct that it is an O(n) when you take the approach looping over all elements, and using the n*(n+1)/2-formula you get even O(1).
Is it possible that this is a misunderstanding and that 1, 2, ..., n are time values of another operation that is performed, meaning that the time to execute that operation constantly increases, and you have to give the Big-O for this sequence of operations?
Otherwise, when the task is really just summing up all numbers from 1 to n and express that time in Big-O-notation, your opinion is correct that it is an O(n) when you take the approach looping over all elements, and using the n*(n+1)/2-formula you get even O(1).
edited Nov 21 '18 at 15:17
answered Nov 21 '18 at 15:10
Stefan IllnerStefan Illner
1551212
1551212
Thanks for the quick reply, the question taken exactly off the assignment is "Which of these is the correct big - O expression for 1+2+3+...+n? A. O(log n) B. O(n) C. O(n log n) D. O(n²)", so there wasn't even an option for constant time which makes it weird that he even brought up that formula in the first place.
– sode
Nov 21 '18 at 15:17
Ok, I guess then your professor is right. I'm not a native english speaker, but the question is not to give the big-O-expression for the time needed to calculate that sum, but 1+2+...+n is the value, depending on n. And, as you correctly stated, this is n*(n+1)/2 which is (n^2)/2 + n/2, so O(n^2) is the correct answer.
– Stefan Illner
Nov 21 '18 at 15:22
That may be the case, but he keeps bringing up the number of operations occurring and iterations, but if he only means the result itself, then him bringing these things up is just making it more confusing. I know he did not make this question himself because you can find it all over google, and he is also not a native English speaker, so this could possibly be a misunderstanding on both of our parts.
– sode
Nov 21 '18 at 15:41
add a comment |
Thanks for the quick reply, the question taken exactly off the assignment is "Which of these is the correct big - O expression for 1+2+3+...+n? A. O(log n) B. O(n) C. O(n log n) D. O(n²)", so there wasn't even an option for constant time which makes it weird that he even brought up that formula in the first place.
– sode
Nov 21 '18 at 15:17
Ok, I guess then your professor is right. I'm not a native english speaker, but the question is not to give the big-O-expression for the time needed to calculate that sum, but 1+2+...+n is the value, depending on n. And, as you correctly stated, this is n*(n+1)/2 which is (n^2)/2 + n/2, so O(n^2) is the correct answer.
– Stefan Illner
Nov 21 '18 at 15:22
That may be the case, but he keeps bringing up the number of operations occurring and iterations, but if he only means the result itself, then him bringing these things up is just making it more confusing. I know he did not make this question himself because you can find it all over google, and he is also not a native English speaker, so this could possibly be a misunderstanding on both of our parts.
– sode
Nov 21 '18 at 15:41
Thanks for the quick reply, the question taken exactly off the assignment is "Which of these is the correct big - O expression for 1+2+3+...+n? A. O(log n) B. O(n) C. O(n log n) D. O(n²)", so there wasn't even an option for constant time which makes it weird that he even brought up that formula in the first place.
– sode
Nov 21 '18 at 15:17
Thanks for the quick reply, the question taken exactly off the assignment is "Which of these is the correct big - O expression for 1+2+3+...+n? A. O(log n) B. O(n) C. O(n log n) D. O(n²)", so there wasn't even an option for constant time which makes it weird that he even brought up that formula in the first place.
– sode
Nov 21 '18 at 15:17
Ok, I guess then your professor is right. I'm not a native english speaker, but the question is not to give the big-O-expression for the time needed to calculate that sum, but 1+2+...+n is the value, depending on n. And, as you correctly stated, this is n*(n+1)/2 which is (n^2)/2 + n/2, so O(n^2) is the correct answer.
– Stefan Illner
Nov 21 '18 at 15:22
Ok, I guess then your professor is right. I'm not a native english speaker, but the question is not to give the big-O-expression for the time needed to calculate that sum, but 1+2+...+n is the value, depending on n. And, as you correctly stated, this is n*(n+1)/2 which is (n^2)/2 + n/2, so O(n^2) is the correct answer.
– Stefan Illner
Nov 21 '18 at 15:22
That may be the case, but he keeps bringing up the number of operations occurring and iterations, but if he only means the result itself, then him bringing these things up is just making it more confusing. I know he did not make this question himself because you can find it all over google, and he is also not a native English speaker, so this could possibly be a misunderstanding on both of our parts.
– sode
Nov 21 '18 at 15:41
That may be the case, but he keeps bringing up the number of operations occurring and iterations, but if he only means the result itself, then him bringing these things up is just making it more confusing. I know he did not make this question himself because you can find it all over google, and he is also not a native English speaker, so this could possibly be a misunderstanding on both of our parts.
– sode
Nov 21 '18 at 15:41
add a comment |
You are computing the order of the wrong value.
As you indicated in a comment, the question does not ask what's the time complexity of doing the sum; the question asks what is the order of the sum itself. And indeed 1 +
2 + ... + n is O(n²).
Thanks for the answer, but in a response to one of my emails when I had asked if we were only concerned with the order and not time complexity, he said that this problem had to do with time complexity and the number of operations being performed.
– sode
Nov 21 '18 at 15:28
add a comment |
You are computing the order of the wrong value.
As you indicated in a comment, the question does not ask what's the time complexity of doing the sum; the question asks what is the order of the sum itself. And indeed 1 +
2 + ... + n is O(n²).
Thanks for the answer, but in a response to one of my emails when I had asked if we were only concerned with the order and not time complexity, he said that this problem had to do with time complexity and the number of operations being performed.
– sode
Nov 21 '18 at 15:28
add a comment |
You are computing the order of the wrong value.
As you indicated in a comment, the question does not ask what's the time complexity of doing the sum; the question asks what is the order of the sum itself. And indeed 1 +
2 + ... + n is O(n²).
You are computing the order of the wrong value.
As you indicated in a comment, the question does not ask what's the time complexity of doing the sum; the question asks what is the order of the sum itself. And indeed 1 +
2 + ... + n is O(n²).
answered Nov 21 '18 at 15:21
AlexPAlexP
3,624811
3,624811
Thanks for the answer, but in a response to one of my emails when I had asked if we were only concerned with the order and not time complexity, he said that this problem had to do with time complexity and the number of operations being performed.
– sode
Nov 21 '18 at 15:28
add a comment |
Thanks for the answer, but in a response to one of my emails when I had asked if we were only concerned with the order and not time complexity, he said that this problem had to do with time complexity and the number of operations being performed.
– sode
Nov 21 '18 at 15:28
Thanks for the answer, but in a response to one of my emails when I had asked if we were only concerned with the order and not time complexity, he said that this problem had to do with time complexity and the number of operations being performed.
– sode
Nov 21 '18 at 15:28
Thanks for the answer, but in a response to one of my emails when I had asked if we were only concerned with the order and not time complexity, he said that this problem had to do with time complexity and the number of operations being performed.
– sode
Nov 21 '18 at 15:28
add a comment |
In a Java example like this
int n = 1000;
int sum = 0;
// iterating n times
for (int i = 1; i <= n; i++) {
// just a basic operation, so no extra complexity here
sum += i;
}
the addition is called n
times, so the whole code has a time complexity of O(1) * n = O(n).
If there is nothing missing in your question, O(n) will be the correct answer to the task.
Anyway, there is a good chance the professor is right ;-)
O(n * (n + 1) / 2) = O(n / 2) * O((n + 1) / 2) = O(n) * O(n + 1) = O(n) * O(n) = O(n * n) = O(n^2)
I really don't know what to think now, his latest reply to me was "each iteration requires i operations, so all together we have n(n+1)/2 operations" and I am just getting more confused because in the case of that formula, there is no iteration occurring afaik?
– sode
Nov 21 '18 at 15:39
It would be nice to know what happens in each iteration (the professor is thinking of)... I hope he does not meann
iterations of summing up values from1
ton
...
– deHaar
Nov 21 '18 at 15:40
I'm not entirely sure, and getting any reply out of him other than that the answer is O(n^2) is seeming pretty unlikely
– sode
Nov 21 '18 at 15:43
Giving you the right answer without an explanation is a really weird way to teach... Then give the right answer, pass the assignment and discuss with others. I think there is no different option apart from talking to different professors, maybe.
– deHaar
Nov 21 '18 at 15:46
1
summing input using formula (n * (n + 1))/2 complexity is O(1). Why do you use Big-O notation there?
– kriss
Nov 21 '18 at 15:48
|
show 2 more comments
In a Java example like this
int n = 1000;
int sum = 0;
// iterating n times
for (int i = 1; i <= n; i++) {
// just a basic operation, so no extra complexity here
sum += i;
}
the addition is called n
times, so the whole code has a time complexity of O(1) * n = O(n).
If there is nothing missing in your question, O(n) will be the correct answer to the task.
Anyway, there is a good chance the professor is right ;-)
O(n * (n + 1) / 2) = O(n / 2) * O((n + 1) / 2) = O(n) * O(n + 1) = O(n) * O(n) = O(n * n) = O(n^2)
I really don't know what to think now, his latest reply to me was "each iteration requires i operations, so all together we have n(n+1)/2 operations" and I am just getting more confused because in the case of that formula, there is no iteration occurring afaik?
– sode
Nov 21 '18 at 15:39
It would be nice to know what happens in each iteration (the professor is thinking of)... I hope he does not meann
iterations of summing up values from1
ton
...
– deHaar
Nov 21 '18 at 15:40
I'm not entirely sure, and getting any reply out of him other than that the answer is O(n^2) is seeming pretty unlikely
– sode
Nov 21 '18 at 15:43
Giving you the right answer without an explanation is a really weird way to teach... Then give the right answer, pass the assignment and discuss with others. I think there is no different option apart from talking to different professors, maybe.
– deHaar
Nov 21 '18 at 15:46
1
summing input using formula (n * (n + 1))/2 complexity is O(1). Why do you use Big-O notation there?
– kriss
Nov 21 '18 at 15:48
|
show 2 more comments
In a Java example like this
int n = 1000;
int sum = 0;
// iterating n times
for (int i = 1; i <= n; i++) {
// just a basic operation, so no extra complexity here
sum += i;
}
the addition is called n
times, so the whole code has a time complexity of O(1) * n = O(n).
If there is nothing missing in your question, O(n) will be the correct answer to the task.
Anyway, there is a good chance the professor is right ;-)
O(n * (n + 1) / 2) = O(n / 2) * O((n + 1) / 2) = O(n) * O(n + 1) = O(n) * O(n) = O(n * n) = O(n^2)
In a Java example like this
int n = 1000;
int sum = 0;
// iterating n times
for (int i = 1; i <= n; i++) {
// just a basic operation, so no extra complexity here
sum += i;
}
the addition is called n
times, so the whole code has a time complexity of O(1) * n = O(n).
If there is nothing missing in your question, O(n) will be the correct answer to the task.
Anyway, there is a good chance the professor is right ;-)
O(n * (n + 1) / 2) = O(n / 2) * O((n + 1) / 2) = O(n) * O(n + 1) = O(n) * O(n) = O(n * n) = O(n^2)
edited Nov 21 '18 at 15:29
answered Nov 21 '18 at 15:18
deHaardeHaar
2,54351629
2,54351629
I really don't know what to think now, his latest reply to me was "each iteration requires i operations, so all together we have n(n+1)/2 operations" and I am just getting more confused because in the case of that formula, there is no iteration occurring afaik?
– sode
Nov 21 '18 at 15:39
It would be nice to know what happens in each iteration (the professor is thinking of)... I hope he does not meann
iterations of summing up values from1
ton
...
– deHaar
Nov 21 '18 at 15:40
I'm not entirely sure, and getting any reply out of him other than that the answer is O(n^2) is seeming pretty unlikely
– sode
Nov 21 '18 at 15:43
Giving you the right answer without an explanation is a really weird way to teach... Then give the right answer, pass the assignment and discuss with others. I think there is no different option apart from talking to different professors, maybe.
– deHaar
Nov 21 '18 at 15:46
1
summing input using formula (n * (n + 1))/2 complexity is O(1). Why do you use Big-O notation there?
– kriss
Nov 21 '18 at 15:48
|
show 2 more comments
I really don't know what to think now, his latest reply to me was "each iteration requires i operations, so all together we have n(n+1)/2 operations" and I am just getting more confused because in the case of that formula, there is no iteration occurring afaik?
– sode
Nov 21 '18 at 15:39
It would be nice to know what happens in each iteration (the professor is thinking of)... I hope he does not meann
iterations of summing up values from1
ton
...
– deHaar
Nov 21 '18 at 15:40
I'm not entirely sure, and getting any reply out of him other than that the answer is O(n^2) is seeming pretty unlikely
– sode
Nov 21 '18 at 15:43
Giving you the right answer without an explanation is a really weird way to teach... Then give the right answer, pass the assignment and discuss with others. I think there is no different option apart from talking to different professors, maybe.
– deHaar
Nov 21 '18 at 15:46
1
summing input using formula (n * (n + 1))/2 complexity is O(1). Why do you use Big-O notation there?
– kriss
Nov 21 '18 at 15:48
I really don't know what to think now, his latest reply to me was "each iteration requires i operations, so all together we have n(n+1)/2 operations" and I am just getting more confused because in the case of that formula, there is no iteration occurring afaik?
– sode
Nov 21 '18 at 15:39
I really don't know what to think now, his latest reply to me was "each iteration requires i operations, so all together we have n(n+1)/2 operations" and I am just getting more confused because in the case of that formula, there is no iteration occurring afaik?
– sode
Nov 21 '18 at 15:39
It would be nice to know what happens in each iteration (the professor is thinking of)... I hope he does not mean
n
iterations of summing up values from 1
to n
...– deHaar
Nov 21 '18 at 15:40
It would be nice to know what happens in each iteration (the professor is thinking of)... I hope he does not mean
n
iterations of summing up values from 1
to n
...– deHaar
Nov 21 '18 at 15:40
I'm not entirely sure, and getting any reply out of him other than that the answer is O(n^2) is seeming pretty unlikely
– sode
Nov 21 '18 at 15:43
I'm not entirely sure, and getting any reply out of him other than that the answer is O(n^2) is seeming pretty unlikely
– sode
Nov 21 '18 at 15:43
Giving you the right answer without an explanation is a really weird way to teach... Then give the right answer, pass the assignment and discuss with others. I think there is no different option apart from talking to different professors, maybe.
– deHaar
Nov 21 '18 at 15:46
Giving you the right answer without an explanation is a really weird way to teach... Then give the right answer, pass the assignment and discuss with others. I think there is no different option apart from talking to different professors, maybe.
– deHaar
Nov 21 '18 at 15:46
1
1
summing input using formula (n * (n + 1))/2 complexity is O(1). Why do you use Big-O notation there?
– kriss
Nov 21 '18 at 15:48
summing input using formula (n * (n + 1))/2 complexity is O(1). Why do you use Big-O notation there?
– kriss
Nov 21 '18 at 15:48
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414918%2fbig-o-notation-of-123-n%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
It's coming from him telling me that the answer depends on the number of operations we are performing, and in the case of (n(n+1)/2, I only see 3 operations happening there. If he would take the time to properly explain it instead of sending me "the answer is O(n^2) because it is (n(n+1))/2" as a reply no matter how many times I ask him to explain.
– sode
Nov 21 '18 at 15:22