Code Review
##Code Review AllAll return paths from a function must return value. Leaving a function that has a non void type without a return is undefined behavior (google nasal dragons).
int getZeroes(long long n){
if(n>0){
// STUFF
return stuff;
}
// BUT HERE there is no return.
// Any negative value or zero causes undefined behavior.
}
This while loop seems overly cumbersome with a break;
while(a%5){
if(a%5 == 0){ // will this ever be true.
// STUFF // Since it is just after a test that checks the opposite?
}else{
break;
}
a = a/5;
}
This looks exactly the same both sides of the else:
return (b+getZeroes(n-1));
}else{
return (getZeroes(n-1));
The only difference is b (which just needs to be made zero for the second case then) you can yank it out of the conditional.
Why store the results?
x.push_back(getZeroes(cases));
The only thing you do with them is print them. May as well just print them as you generate them!
##Code Review All return paths from a function must return value. Leaving a function that has a non void type without a return is undefined behavior (google nasal dragons).
int getZeroes(long long n){
if(n>0){
// STUFF
return stuff;
}
// BUT HERE there is no return.
// Any negative value or zero causes undefined behavior.
}
This while loop seems overly cumbersome with a break;
while(a%5){
if(a%5 == 0){ // will this ever be true.
// STUFF // Since it is just after a test that checks the opposite?
}else{
break;
}
a = a/5;
}
This looks exactly the same both sides of the else:
return (b+getZeroes(n-1));
}else{
return (getZeroes(n-1));
The only difference is b (which just needs to be made zero for the second case then) you can yank it out of the conditional.
Why store the results?
x.push_back(getZeroes(cases));
The only thing you do with them is print them. May as well just print them as you generate them!
Code Review
All return paths from a function must return value. Leaving a function that has a non void type without a return is undefined behavior (google nasal dragons).
int getZeroes(long long n){
if(n>0){
// STUFF
return stuff;
}
// BUT HERE there is no return.
// Any negative value or zero causes undefined behavior.
}
This while loop seems overly cumbersome with a break;
while(a%5){
if(a%5 == 0){ // will this ever be true.
// STUFF // Since it is just after a test that checks the opposite?
}else{
break;
}
a = a/5;
}
This looks exactly the same both sides of the else:
return (b+getZeroes(n-1));
}else{
return (getZeroes(n-1));
The only difference is b (which just needs to be made zero for the second case then) you can yank it out of the conditional.
Why store the results?
x.push_back(getZeroes(cases));
The only thing you do with them is print them. May as well just print them as you generate them!
##Aproach
The classic version of this problem is fib()
.
fib(0) = 0;
fib(1) = 1;
fib(n) = fib(n-1) + fib(n-2);
The classic first solution looks like your code.
int fib(int n)
{
if (n == 0) {return 0;}
if (n == 1) {return 1;}
return fib(n-1) + fib(n-2);
}
The thing you have to recognize is that the number of calls to fib()
is huge O(2^n)
. But it does not need to be. Once you have calculated the value for n
it is constant so you don't need to re-calculate it if you have already calculated it; use memoization .
Now if you are using memoization it does not matter if you calculate down or up. So it is easier to work up towards the goal as you don't need to remember any state on the way up (just the results). So it takes less stack space.
int fib(int n)
{
static std::vector<int> result = {0,1};
while(result.size() <= n)
{
result.push_back(result[result.size() - 1] + result[result.size() - 2]);
}
return result[n];
}
##Code Review All return paths from a function must return value. Leaving a function that has a non void type without a return is undefined behavior (google nasal dragons).
The only difference is b (which just needs to be made zero for the second case then) you can yank it out of the conditional.
##Aproach
The classic version of this problem is fib()
.
fib(0) = 0;
fib(1) = 1;
fib(n) = fib(n-1) + fib(n-2);
The classic first solution looks like your code.
int fib(int n)
{
if (n == 0) {return 0;}
if (n == 1) {return 1;}
return fib(n-1) + fib(n-2);
}
The thing you have to recognize is that the number of calls to fib()
is huge O(2^n)
. But it does not need to be. Once you have calculated the value for n
it is constant so you don't need to re-calculate it if you have already calculated it; use memoization .
Now if you are using memoization it does not matter if you calculate down or up. So it is easier to work up towards the goal as you don't need to remember any state on the way up (just the results). So it takes less stack space.
int fib(int n)
{
static std::vector<int> result = {0,1};
while(result.size() <= n)
{
result.push_back(result[result.size() - 1] + result[result.size() - 2]);
}
return result[n];
}
##Code Review All return paths from a function must return value. Leaving a function that has a non void type without a return is undefined behavior (google nasal dragons).
The only difference is b (which just needs to be made zero for the second case then you can yank it out of the conditional.
##Code Review All return paths from a function must return value. Leaving a function that has a non void type without a return is undefined behavior (google nasal dragons).
The only difference is b (which just needs to be made zero for the second case then) you can yank it out of the conditional.
##Aproach
The classic version of this problem is fib()
.
fib(0) = 0;
fib(1) = 1;
fib(n) = fib(n-1) + fib(n-2);
The classic first solution looks like your code.
int fib(int n)
{
if (n == 0) {return 0;}
if (n == 1) {return 1;}
return fib(n-1) + fib(n-2);
}
The thing you have to recognize is that the number of calls to fib()
is huge O(2^n)
. But it does not need to be. Once you have calculated the value for n
it is constant so you don't need to re-calculate it if you have already calculated it; use memoization.
Now if you are using memoization it does not matter if you calculate down or up. So it is easier to work up towards the goal as you don't need to remember any state on the way up (just the results). So it takes less stack space.
int fib(int n)
{
static std::vector<int> result = {0,1};
while(result.size() <= n)
{
result.push_back(result[result.size() - 1] + result[result.size() - 2]);
}
return result[n];
}
##Code Review All return paths from a function must return value. Leaving a function that has a non void type without a return is undefined behavior (google nasal dragons).
int getZeroes(long long n){
if(n>0){
// STUFF
return stuff;
}
// BUT HERE there is no return.
// Any negative value or zero causes undefined behavior.
}
This while loop seems overly cumbersome with a break;
while(a%5){
if(a%5 == 0){ // will this ever be true.
// STUFF // Since it is just after a test that checks the opposite?
}else{
break;
}
a = a/5;
}
This looks exactly the same both sides of the else:
return (b+getZeroes(n-1));
}else{
return (getZeroes(n-1));
The only difference is b (which just needs to be made zero for the second case then you can yank it out of the conditional.
Why store the results?
x.push_back(getZeroes(cases));
The only thing you do with them is print them. May as well just print them as you generate them!