Using namespaces imports the said namespace globally into your code file, in this case you are importing the standard library.
Let's say you have another custom namespace providing a function cout
what will happen then.
Namespace Collision
Here your code will be confused on which function it can call, hence it will call them both leaving you with unwanted results.
Also, the statement std::cout
adds clarity to your code as here you are aware that the cout
belongs to the standard library.
For the answer to your question...
Yes, your code can be shorter using these 2 ways:
(PS: Both of them use mathematics)
Here limit
variable essentially asks, how many odd numbers you want to add? If you say 10 then it means add first 10 odd numbers not the odd numbers till 10...
First approach
#include <iostream>
/*
* This method uses the formula (2 x n + 1) with a sign modification.
* There n denotes the odd number at nth place and n = 0, 1, 2...
* eg: if n = 2 then the odd number is 5 and sum till n is 9
*/
int main(){
int limit = 10, oddSum = 0;
// Here you set a limit... i.e: The value of n
// Notice we are subtracting one from the limit
// Hence the value of n or limit ranges from 1, 2, 3, 4.....
/*
* limit << 1 is a right shift operation which is an efficient way
* of saying limit * 2, as right shift operation multiplies the
* number given by 2
*/
for(limit = limit; limit > 0; limit--)
oddSum += (limit << 1) - 1;
// Wola! You are done now print the sum
// Yes, mathematics is important in coding
std::cout << oddSum << std::endl;
}
Second approach
Here pow is a function in math.h
It will raise the limit i.e n to the said exponent
Please don't use it for squares, I added it so that you might learn a useful function.
pow(value, exponent)... Fancy way of saying n2
#include <iostream>
#include <math.h>
/*
* Using mathematics we can always see that the sum of n odd numbers
* is the square of itself...
* This result makes the problem very simple
*/
int main(){
int limit = 10, oddSum = pow(limit, 2);
// Boom! You are done now print the sum
std::cout << oddSum << std::endl
}
Approach without mathematics
First Approach
#include <iostream>
int main(){
int limit, oddNumber = 1, oddSum = 0;
for (limit = 10; limit > 0; limit-- && (oddNumber = oddNumber + 2))
oddSum += oddNumber;
std::cout << oddSum << std::endl;
}
Second Approach
Here limit
means add all odd numbers till the number limit
. If limit = 10
then add 1, 3, 5, 7, 9...
#include <iostream>
int main(){
int limit, oddSum = 0;
for (limit = 10; limit > 0; limit--)
oddSum = (limit % 2 != 0) ? (oddSum + limit): oddSum;
std::cout << oddSum << std::endl;
}
Third Approach
Here the meaning of limit
is same as the previous approach...
#include <iostream>
int main(){
int limit = 10, oddNumber, oddSum = 0;
for (oddNumber = 1; oddNumber <= limit; oddNumber = oddNumber + 2)
oddSum += oddNumber;
std::cout << oddSum << std::endl;
}
Increasing the variable range from int
to long
or long long
will increase the number of values you can calculate... No need to overkill such a simple problem with methods. Its like bringing machine guns to a knife fight.
Ohh! And please don't use std::endl
on every line... It flushes out the buffer which is an expensive call. Use it at the end or if performance is necessary don't use it at all.
Edit:
I had some free time from my own personal projects, college projects and assignments. Therefore, I went over your problem of odd sums...
If you have read this answer thoroughly, then you might wonder.. Why do the last 2 solutions change the definition of the variable limit
?
I wanted to give you and comments a simplified solution using loops and the initial approach of people when solving this problem. There are many complicated ways in which you can solve this problem, for example add in hash maps and mem sets, you will find some very different codes in either cases.
This means you can even use dynamic programming and backtracking on such a simple problem... Why? Just so that you can calculate values larger than what long long
can hold, while also calculating their sum. Then again as I said, this will be similar to killing an ant with 100 ballistic missiles.
You might wonder why have I approached this problem using simple and concise codes? It is because of time complexity... Here in all the solutions not only including my answers but other as well, the solution which uses another overkill function pow
will run the fastest for any values that fit under long long
.
Then how do I solve, when the definition of the variable limit
changes for the last 2 approaches in my answer?
There is another efficient way using maths...
The odd numbers is a series values from 1 to infinity...
That is: 1, 3, 5, 7, 9, 11, 13.... and so on.
You know that using the formula 2*n - 1 you can find any odd number at any given position in the series... Here n ranges from 1 to infinity.
That is, n can be any number in this series: 1, 2, 3, 4, 5, 6... infinity.
Using mathematics again you can find the position (n) of any given odd number, if you do so then you simply square the position (n) of it to obtain the sum of odd number till the given odd number. Thus also answering the problem of finding a better way to solve for the last 2 approaches.
Enough blabbering here is the code:
#include <iostream>
int main() {
int limit = 10, n = 0, oddSum = 0;
// Check if the given limit is even if yes then subtract 1 to make it odd
limit = (limit % 2 == 0) ? limit - 1: limit;
// Now calculate the position (n)
n = (limit + 1) / 2;
// Now calculate the sum
oddSum = n * n;
// Print the sum
std::cout << "The sum of odd numbers is " << oddSum
// No std::endl here and the time complexity should be O(1) if I am correct
// This will net you say for limit = 10 the oddSum will be 25
}
odd_num
is a lousy name (if you'd used 8 instead of 10,odd_num
would end up being 16, which is odd (strange) to call odd (not even)). Better would berunning_sum
. \$\endgroup\$