Time Complexity of Fibonacci Series
long int F(int n){
long int F[n];
if (n<2) return n;
else {
F[0]=0; F[1]=1;
for (int i=2; i<n+1; i++)
F[i]=F[i-1]+F[i-2];
return F[n]; }
}
Hi guys, can anyone know how to compute the time complexity of the function above? I am studying C++ and I am quite suffering about compute time complexity of a random algorithm. Please help me! Thanks in advance.
c++ time-complexity fibonacci
add a comment |
long int F(int n){
long int F[n];
if (n<2) return n;
else {
F[0]=0; F[1]=1;
for (int i=2; i<n+1; i++)
F[i]=F[i-1]+F[i-2];
return F[n]; }
}
Hi guys, can anyone know how to compute the time complexity of the function above? I am studying C++ and I am quite suffering about compute time complexity of a random algorithm. Please help me! Thanks in advance.
c++ time-complexity fibonacci
This behavior is trivial. The recursive case is analyzed thoroughly in Vol I of Donald Knuth's The Art of Computer Programming. IIRC.
– Cheers and hth. - Alf
Mar 8 '15 at 15:00
Hi, thank you so much for your comment :)
– vinh0105
Mar 8 '15 at 15:51
add a comment |
long int F(int n){
long int F[n];
if (n<2) return n;
else {
F[0]=0; F[1]=1;
for (int i=2; i<n+1; i++)
F[i]=F[i-1]+F[i-2];
return F[n]; }
}
Hi guys, can anyone know how to compute the time complexity of the function above? I am studying C++ and I am quite suffering about compute time complexity of a random algorithm. Please help me! Thanks in advance.
c++ time-complexity fibonacci
long int F(int n){
long int F[n];
if (n<2) return n;
else {
F[0]=0; F[1]=1;
for (int i=2; i<n+1; i++)
F[i]=F[i-1]+F[i-2];
return F[n]; }
}
Hi guys, can anyone know how to compute the time complexity of the function above? I am studying C++ and I am quite suffering about compute time complexity of a random algorithm. Please help me! Thanks in advance.
c++ time-complexity fibonacci
c++ time-complexity fibonacci
asked Mar 8 '15 at 14:58
vinh0105vinh0105
611111
611111
This behavior is trivial. The recursive case is analyzed thoroughly in Vol I of Donald Knuth's The Art of Computer Programming. IIRC.
– Cheers and hth. - Alf
Mar 8 '15 at 15:00
Hi, thank you so much for your comment :)
– vinh0105
Mar 8 '15 at 15:51
add a comment |
This behavior is trivial. The recursive case is analyzed thoroughly in Vol I of Donald Knuth's The Art of Computer Programming. IIRC.
– Cheers and hth. - Alf
Mar 8 '15 at 15:00
Hi, thank you so much for your comment :)
– vinh0105
Mar 8 '15 at 15:51
This behavior is trivial. The recursive case is analyzed thoroughly in Vol I of Donald Knuth's The Art of Computer Programming. IIRC.
– Cheers and hth. - Alf
Mar 8 '15 at 15:00
This behavior is trivial. The recursive case is analyzed thoroughly in Vol I of Donald Knuth's The Art of Computer Programming. IIRC.
– Cheers and hth. - Alf
Mar 8 '15 at 15:00
Hi, thank you so much for your comment :)
– vinh0105
Mar 8 '15 at 15:51
Hi, thank you so much for your comment :)
– vinh0105
Mar 8 '15 at 15:51
add a comment |
3 Answers
3
active
oldest
votes
The code shown relies on a g++ language extension, variable length arrays.
I.e. it's not standard C++.
The code also misdirects a little by using the name F
for two different things.
And do note that the code exhibits Undefined Behavior by indexing an array beyond its end.
Apart from that it's trivial.
When the code is corrected, or is viewed as just pseudo-code, doing n-1 operations has complexity O(n).
Hi, could you please show me step by step how to compute the complexity? I saw on some books the calculate by using T(n) sth. But I have no idea how it works. Please help me.
– vinh0105
Mar 8 '15 at 15:50
add a comment |
For this program , complexity is O(n)
Hi, thank you so much for your information
– vinh0105
Mar 8 '15 at 15:51
add a comment |
Since the algorithm is using memoization, time and space complexity is linear O(n).
Usually time complexity involves accounting comparison operations on data, which are missing in this case (the only real comparison operation is the bound check), so the linear complexity is give by the F[i-1]+F[i-2]
operation.
Hi, could you please show me step by step how to compute the complexity? I saw on some books the calculate by using T(n) sth. But I have no idea how it works. Please help me.
– vinh0105
Mar 8 '15 at 15:50
1
@LucasVinhTran: In the general case, a loop iterates N-1 times. Each time a constant number of operations, each of constant complexity, such as addition and indexing, is performed. I.e. the loop body has constant complexity. Since it's performed on the order of N times, it has complexity O(N). That's without going into the finer points of the notation, where practice in online forums (text typed via keyboard) is somewhat at odds with the notation that mathematically bent computer scientists have chosen (text written with pencil or chalk). You can find more information e.g. in Wikipedia.
– Cheers and hth. - Alf
Mar 8 '15 at 17:39
@Jack: This function is not using memoization. A result is computed for every call.
– Cheers and hth. - Alf
Mar 8 '15 at 17:43
add a comment |
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%2f28927851%2ftime-complexity-of-fibonacci-series%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The code shown relies on a g++ language extension, variable length arrays.
I.e. it's not standard C++.
The code also misdirects a little by using the name F
for two different things.
And do note that the code exhibits Undefined Behavior by indexing an array beyond its end.
Apart from that it's trivial.
When the code is corrected, or is viewed as just pseudo-code, doing n-1 operations has complexity O(n).
Hi, could you please show me step by step how to compute the complexity? I saw on some books the calculate by using T(n) sth. But I have no idea how it works. Please help me.
– vinh0105
Mar 8 '15 at 15:50
add a comment |
The code shown relies on a g++ language extension, variable length arrays.
I.e. it's not standard C++.
The code also misdirects a little by using the name F
for two different things.
And do note that the code exhibits Undefined Behavior by indexing an array beyond its end.
Apart from that it's trivial.
When the code is corrected, or is viewed as just pseudo-code, doing n-1 operations has complexity O(n).
Hi, could you please show me step by step how to compute the complexity? I saw on some books the calculate by using T(n) sth. But I have no idea how it works. Please help me.
– vinh0105
Mar 8 '15 at 15:50
add a comment |
The code shown relies on a g++ language extension, variable length arrays.
I.e. it's not standard C++.
The code also misdirects a little by using the name F
for two different things.
And do note that the code exhibits Undefined Behavior by indexing an array beyond its end.
Apart from that it's trivial.
When the code is corrected, or is viewed as just pseudo-code, doing n-1 operations has complexity O(n).
The code shown relies on a g++ language extension, variable length arrays.
I.e. it's not standard C++.
The code also misdirects a little by using the name F
for two different things.
And do note that the code exhibits Undefined Behavior by indexing an array beyond its end.
Apart from that it's trivial.
When the code is corrected, or is viewed as just pseudo-code, doing n-1 operations has complexity O(n).
edited Mar 8 '15 at 17:41
answered Mar 8 '15 at 15:02


Cheers and hth. - AlfCheers and hth. - Alf
124k11160267
124k11160267
Hi, could you please show me step by step how to compute the complexity? I saw on some books the calculate by using T(n) sth. But I have no idea how it works. Please help me.
– vinh0105
Mar 8 '15 at 15:50
add a comment |
Hi, could you please show me step by step how to compute the complexity? I saw on some books the calculate by using T(n) sth. But I have no idea how it works. Please help me.
– vinh0105
Mar 8 '15 at 15:50
Hi, could you please show me step by step how to compute the complexity? I saw on some books the calculate by using T(n) sth. But I have no idea how it works. Please help me.
– vinh0105
Mar 8 '15 at 15:50
Hi, could you please show me step by step how to compute the complexity? I saw on some books the calculate by using T(n) sth. But I have no idea how it works. Please help me.
– vinh0105
Mar 8 '15 at 15:50
add a comment |
For this program , complexity is O(n)
Hi, thank you so much for your information
– vinh0105
Mar 8 '15 at 15:51
add a comment |
For this program , complexity is O(n)
Hi, thank you so much for your information
– vinh0105
Mar 8 '15 at 15:51
add a comment |
For this program , complexity is O(n)
For this program , complexity is O(n)
answered Mar 8 '15 at 15:02


01axel01christian01axel01christian
30218
30218
Hi, thank you so much for your information
– vinh0105
Mar 8 '15 at 15:51
add a comment |
Hi, thank you so much for your information
– vinh0105
Mar 8 '15 at 15:51
Hi, thank you so much for your information
– vinh0105
Mar 8 '15 at 15:51
Hi, thank you so much for your information
– vinh0105
Mar 8 '15 at 15:51
add a comment |
Since the algorithm is using memoization, time and space complexity is linear O(n).
Usually time complexity involves accounting comparison operations on data, which are missing in this case (the only real comparison operation is the bound check), so the linear complexity is give by the F[i-1]+F[i-2]
operation.
Hi, could you please show me step by step how to compute the complexity? I saw on some books the calculate by using T(n) sth. But I have no idea how it works. Please help me.
– vinh0105
Mar 8 '15 at 15:50
1
@LucasVinhTran: In the general case, a loop iterates N-1 times. Each time a constant number of operations, each of constant complexity, such as addition and indexing, is performed. I.e. the loop body has constant complexity. Since it's performed on the order of N times, it has complexity O(N). That's without going into the finer points of the notation, where practice in online forums (text typed via keyboard) is somewhat at odds with the notation that mathematically bent computer scientists have chosen (text written with pencil or chalk). You can find more information e.g. in Wikipedia.
– Cheers and hth. - Alf
Mar 8 '15 at 17:39
@Jack: This function is not using memoization. A result is computed for every call.
– Cheers and hth. - Alf
Mar 8 '15 at 17:43
add a comment |
Since the algorithm is using memoization, time and space complexity is linear O(n).
Usually time complexity involves accounting comparison operations on data, which are missing in this case (the only real comparison operation is the bound check), so the linear complexity is give by the F[i-1]+F[i-2]
operation.
Hi, could you please show me step by step how to compute the complexity? I saw on some books the calculate by using T(n) sth. But I have no idea how it works. Please help me.
– vinh0105
Mar 8 '15 at 15:50
1
@LucasVinhTran: In the general case, a loop iterates N-1 times. Each time a constant number of operations, each of constant complexity, such as addition and indexing, is performed. I.e. the loop body has constant complexity. Since it's performed on the order of N times, it has complexity O(N). That's without going into the finer points of the notation, where practice in online forums (text typed via keyboard) is somewhat at odds with the notation that mathematically bent computer scientists have chosen (text written with pencil or chalk). You can find more information e.g. in Wikipedia.
– Cheers and hth. - Alf
Mar 8 '15 at 17:39
@Jack: This function is not using memoization. A result is computed for every call.
– Cheers and hth. - Alf
Mar 8 '15 at 17:43
add a comment |
Since the algorithm is using memoization, time and space complexity is linear O(n).
Usually time complexity involves accounting comparison operations on data, which are missing in this case (the only real comparison operation is the bound check), so the linear complexity is give by the F[i-1]+F[i-2]
operation.
Since the algorithm is using memoization, time and space complexity is linear O(n).
Usually time complexity involves accounting comparison operations on data, which are missing in this case (the only real comparison operation is the bound check), so the linear complexity is give by the F[i-1]+F[i-2]
operation.
answered Mar 8 '15 at 15:03
JackJack
110k26186291
110k26186291
Hi, could you please show me step by step how to compute the complexity? I saw on some books the calculate by using T(n) sth. But I have no idea how it works. Please help me.
– vinh0105
Mar 8 '15 at 15:50
1
@LucasVinhTran: In the general case, a loop iterates N-1 times. Each time a constant number of operations, each of constant complexity, such as addition and indexing, is performed. I.e. the loop body has constant complexity. Since it's performed on the order of N times, it has complexity O(N). That's without going into the finer points of the notation, where practice in online forums (text typed via keyboard) is somewhat at odds with the notation that mathematically bent computer scientists have chosen (text written with pencil or chalk). You can find more information e.g. in Wikipedia.
– Cheers and hth. - Alf
Mar 8 '15 at 17:39
@Jack: This function is not using memoization. A result is computed for every call.
– Cheers and hth. - Alf
Mar 8 '15 at 17:43
add a comment |
Hi, could you please show me step by step how to compute the complexity? I saw on some books the calculate by using T(n) sth. But I have no idea how it works. Please help me.
– vinh0105
Mar 8 '15 at 15:50
1
@LucasVinhTran: In the general case, a loop iterates N-1 times. Each time a constant number of operations, each of constant complexity, such as addition and indexing, is performed. I.e. the loop body has constant complexity. Since it's performed on the order of N times, it has complexity O(N). That's without going into the finer points of the notation, where practice in online forums (text typed via keyboard) is somewhat at odds with the notation that mathematically bent computer scientists have chosen (text written with pencil or chalk). You can find more information e.g. in Wikipedia.
– Cheers and hth. - Alf
Mar 8 '15 at 17:39
@Jack: This function is not using memoization. A result is computed for every call.
– Cheers and hth. - Alf
Mar 8 '15 at 17:43
Hi, could you please show me step by step how to compute the complexity? I saw on some books the calculate by using T(n) sth. But I have no idea how it works. Please help me.
– vinh0105
Mar 8 '15 at 15:50
Hi, could you please show me step by step how to compute the complexity? I saw on some books the calculate by using T(n) sth. But I have no idea how it works. Please help me.
– vinh0105
Mar 8 '15 at 15:50
1
1
@LucasVinhTran: In the general case, a loop iterates N-1 times. Each time a constant number of operations, each of constant complexity, such as addition and indexing, is performed. I.e. the loop body has constant complexity. Since it's performed on the order of N times, it has complexity O(N). That's without going into the finer points of the notation, where practice in online forums (text typed via keyboard) is somewhat at odds with the notation that mathematically bent computer scientists have chosen (text written with pencil or chalk). You can find more information e.g. in Wikipedia.
– Cheers and hth. - Alf
Mar 8 '15 at 17:39
@LucasVinhTran: In the general case, a loop iterates N-1 times. Each time a constant number of operations, each of constant complexity, such as addition and indexing, is performed. I.e. the loop body has constant complexity. Since it's performed on the order of N times, it has complexity O(N). That's without going into the finer points of the notation, where practice in online forums (text typed via keyboard) is somewhat at odds with the notation that mathematically bent computer scientists have chosen (text written with pencil or chalk). You can find more information e.g. in Wikipedia.
– Cheers and hth. - Alf
Mar 8 '15 at 17:39
@Jack: This function is not using memoization. A result is computed for every call.
– Cheers and hth. - Alf
Mar 8 '15 at 17:43
@Jack: This function is not using memoization. A result is computed for every call.
– Cheers and hth. - Alf
Mar 8 '15 at 17:43
add a comment |
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%2f28927851%2ftime-complexity-of-fibonacci-series%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
This behavior is trivial. The recursive case is analyzed thoroughly in Vol I of Donald Knuth's The Art of Computer Programming. IIRC.
– Cheers and hth. - Alf
Mar 8 '15 at 15:00
Hi, thank you so much for your comment :)
– vinh0105
Mar 8 '15 at 15:51