Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Calculate the Sum of Natural Numbers in Golang?
In this tutorial, we are going to see how to find the sum of Natural Numbers in Golang. To do that we have two ways one using the formula itself and the other is to use a for loop we will explore both ways in this tutorial.
Formula
Sum = ( N * ( N + 1))/2 N = The value of the natural number till which you want to find the sum.
Explanation
Let's find the sum of the first 6 natural numbers using the above formula.
Sum of first 6 natural numbers = 1 + 2 + 3 + 4 + 5 + 6 = ( N * ( N + 1)) / 2 = ( 6 * ( 6 + 1 )) / 2 = ( 6 * 7) / 2 = 42 / 2 = 21
Finding the sum of the Natural number using the formula
Algorithm
Step 1 ? Declaring the variable N that is storing the number till which we have to find the sum and also and also the answer variable to store the final result.
Step 2 ? Initializing the variable N.
Step 3 ? Calling the sumOfNNaturalNumbers() function that is finding the sum using the formula mentioned above.
Step 4 ? Printing the result.
Time Complexity: O(1) Space Complexity: O(1)
Example 1
In this example, we are going to find the sum of N natural numbers using the above formulae.
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>
<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token comment">// defining the function with a parameter of int32</span>
<span class="token comment">// type and have a return type int32</span>
func <span class="token function">sumOfNNaturalNumbers</span><span class="token punctuation">(</span><span class="token class-name">N</span> int32<span class="token punctuation">)</span> int32 <span class="token punctuation">{</span>
<span class="token comment">// declaring the variable sum of int32 type</span>
<span class="token comment">// that will store the sum of N Natural numbers</span>
<span class="token keyword">var</span> sum int32
<span class="token comment">// finding the sum using the formula and storing</span>
<span class="token comment">// into the variable</span>
sum <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">N</span> <span class="token operator">*</span> <span class="token punctuation">(</span><span class="token class-name">N</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span>
<span class="token comment">// returning the sum</span>
<span class="token keyword">return</span> sum
<span class="token punctuation">}</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// declaring the variable N of int32 type till which we</span>
<span class="token comment">// have to find the sum of Natural numbers and a variable</span>
<span class="token comment">// answer that will store the sum</span>
<span class="token keyword">var</span> <span class="token class-name">N</span><span class="token punctuation">,</span> answer int32
<span class="token comment">// initializing the variable N</span>
<span class="token class-name">N</span> <span class="token operator">=</span> <span class="token number">10</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Program to find the sum of the Natural number using the formula."</span><span class="token punctuation">)</span>
<span class="token comment">// calling the sumOfNNaturalNumbers() function and storing</span>
<span class="token comment">// the result in the answer variable</span>
answer <span class="token operator">=</span> <span class="token function">sumOfNNaturalNumbers</span><span class="token punctuation">(</span><span class="token class-name">N</span><span class="token punctuation">)</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"The sum of"</span><span class="token punctuation">,</span> <span class="token class-name">N</span><span class="token punctuation">,</span> <span class="token string">"natural numbers is"</span><span class="token punctuation">,</span> answer<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
Program to find the sum of the Natural number using the formula. The sum of 10 natural numbers is 55
Finding the sum of the Natural number using the for loop
Algorithm
Step 1 ? Declaring the variable N that is storing the number till which we have to find the sum and also and also the answer variable to store the final result.
Step 2 ? Initializing the variable N.
Step 3 ? Calling the sumOfNNaturalNumbers() function that is finding the sum using the for loop.
Step 4 ? Printing the result.
Time Complexity: O(N) Space Complexity: O(1)
Example 2
In this example, we are going to find the sum of N natural numbers using the for loop.
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>
<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token comment">// defining the function with a parameter of int32</span>
<span class="token comment">// type and have a return type int32</span>
func <span class="token function">sumOfNNaturalNumbers</span><span class="token punctuation">(</span><span class="token class-name">N</span> int32<span class="token punctuation">)</span> int32 <span class="token punctuation">{</span>
<span class="token comment">// declaring the variable sum of int32 type</span>
<span class="token comment">// that will store the sum of N Natural numbers</span>
<span class="token comment">// and a variable iterator that we will use in for loop</span>
<span class="token keyword">var</span> iterator<span class="token punctuation">,</span> sum int32
<span class="token comment">// initializing the sum variable with 0</span>
sum <span class="token operator">=</span> <span class="token number">0</span>
<span class="token comment">// running a for loop from 1 till N</span>
<span class="token keyword">for</span> iterator <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> iterator <span class="token operator"><=</span> <span class="token class-name">N</span><span class="token punctuation">;</span> iterator <span class="token operator">=</span> iterator <span class="token operator">+</span> <span class="token number">1</span> <span class="token punctuation">{</span>
<span class="token comment">// adding each natural number in the sum</span>
sum <span class="token operator">=</span> sum <span class="token operator">+</span> iterator
<span class="token punctuation">}</span>
<span class="token comment">// returning the sum</span>
<span class="token keyword">return</span> sum
<span class="token punctuation">}</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// declaring the variable N of int32 type till which we</span>
<span class="token comment">// have to find the sum of Natural numbers and a variable</span>
<span class="token comment">// answer that will store the sum</span>
<span class="token keyword">var</span> <span class="token class-name">N</span><span class="token punctuation">,</span> answer int32
<span class="token comment">// initializing the variable N</span>
<span class="token class-name">N</span> <span class="token operator">=</span> <span class="token number">10</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Program to find the sum of the Natural number using the for loop."</span><span class="token punctuation">)</span>
<span class="token comment">// calling the sumOfNNaturalNumbers() function and storing</span>
<span class="token comment">// the result in the answer variable</span>
answer <span class="token operator">=</span> <span class="token function">sumOfNNaturalNumbers</span><span class="token punctuation">(</span><span class="token class-name">N</span><span class="token punctuation">)</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"The sum of"</span><span class="token punctuation">,</span> <span class="token class-name">N</span><span class="token punctuation">,</span> <span class="token string">"natural numbers is"</span><span class="token punctuation">,</span> answer<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
Program to find the sum of the Natural number using the for loop. The sum of 10 natural numbers is 55
Conclusion
These are the two ways to find the sum of Natural numbers in Golang. The first way is much better in terms of time complexity. To learn more about go you can explore these tutorials.
