Time Complexity of Fibonacci Series












0















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.










share|improve this question























  • 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
















0















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.










share|improve this question























  • 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














0












0








0








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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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












3 Answers
3






active

oldest

votes


















1














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).






share|improve this answer


























  • 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



















0














For this program , complexity is O(n)






share|improve this answer
























  • Hi, thank you so much for your information

    – vinh0105
    Mar 8 '15 at 15:51



















0














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.






share|improve this answer
























  • 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













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
});


}
});














draft saved

draft discarded


















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









1














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).






share|improve this answer


























  • 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














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).






share|improve this answer


























  • 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








1







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).






share|improve this answer















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).







share|improve this answer














share|improve this answer



share|improve this answer








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



















  • 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













0














For this program , complexity is O(n)






share|improve this answer
























  • Hi, thank you so much for your information

    – vinh0105
    Mar 8 '15 at 15:51
















0














For this program , complexity is O(n)






share|improve this answer
























  • Hi, thank you so much for your information

    – vinh0105
    Mar 8 '15 at 15:51














0












0








0







For this program , complexity is O(n)






share|improve this answer













For this program , complexity is O(n)







share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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











0














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.






share|improve this answer
























  • 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


















0














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.






share|improve this answer
























  • 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
















0












0








0







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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





















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory