Haskell program to calculate the sum of natural numbers

In this tutorial, we discuss writing a program to calculate the sum of the natural numbers in the Haskell programming language.

Natural numbers are positive integers starting from 1,2,3...N

In this tutorial, we see different ways to implement a program to compute the sum of natural numbers.

  • Program to compute the sum of the natural numbers using a mathematical formula.

  • Program to compute the sum of the natural numbers using a recursive function.

  • Program to compute the sum of the natural numbers using the list method/function sum.

Algorithm steps

  • Declare or take input the range value(n) of the natural number.

  • Implement the program to compute the sum of natural numbers up to n.

  • Print or Display the computed sum.

Example 1

Program to compute the sum of natural numbers using a mathematical formula.

<div class="execute"></div><div class="code-mirror  language-haskell" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token hvariable">main</span> <span class="token operator">::</span> <span class="token constant">IO</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token hvariable">main</span> <span class="token operator">=</span> <span class="token keyword">do</span>
<span class="token comment">-- declaring and initializing the variable for range n</span>
   <span class="token keyword">let</span> <span class="token hvariable">n</span> <span class="token operator">=</span> <span class="token number">10</span>
<span class="token comment">-- computing sum and loading it in a variable</span>
   <span class="token keyword">let</span> <span class="token builtin">sum</span> <span class="token operator">=</span> <span class="token hvariable">n</span><span class="token operator">*</span><span class="token punctuation">(</span><span class="token hvariable">n</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">2</span>
<span class="token comment">-- printing the computed sum</span>
   <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token string">"sum of the natural number in range 1 to "</span><span class="token operator">++</span> <span class="token builtin">show</span> <span class="token hvariable">n</span><span class="token punctuation">)</span>
   <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token builtin">sum</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

"sum of the natural number in range 1 to 10.0"
55.0

In the above program, we declared and initialized a variable n to load the range value. We computed the sum of the natural numbers up to n by using the logic n*(n+1)/2. We loaded the computed value in a variable sum. Finally, we printed the computed sum using the print function.

Note: "++" is an operator to concatenate two strings. The function show is used to parse numbers into strings. This function takes a single argument and returns a string.

Example 2

Program to compute the sum of natural numbers using a recursive function

<div class="execute"></div><div class="code-mirror  language-haskell" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">-- function declaration for function sumNatural</span>
<span class="token hvariable">sumNatural</span> <span class="token operator">::</span> <span class="token constant">Int</span><span class="token operator">-></span><span class="token constant">Int</span>

<span class="token comment">-- function definition for function sumNatural</span>
<span class="token comment">-- base case</span>
<span class="token hvariable">sumNatural</span> <span class="token number">1</span> <span class="token operator">=</span> <span class="token number">1</span>
<span class="token comment">-- all other cases</span>
<span class="token hvariable">sumNatural</span> <span class="token hvariable">n</span> <span class="token operator">=</span> <span class="token hvariable">n</span> <span class="token operator">+</span> <span class="token hvariable">sumNatural</span> <span class="token punctuation">(</span><span class="token hvariable">n</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>

<span class="token hvariable">main</span> <span class="token operator">::</span> <span class="token constant">IO</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token hvariable">main</span> <span class="token operator">=</span> <span class="token keyword">do</span>
<span class="token comment">-- declaring and initializing the variable for range n</span>
   <span class="token keyword">let</span> <span class="token hvariable">n</span> <span class="token operator">=</span> <span class="token number">20</span>
<span class="token comment">-- computing sum by invoking the function sumNatural and loading it in a variable</span>
   <span class="token keyword">let</span> <span class="token builtin">sum</span> <span class="token operator">=</span> <span class="token hvariable">sumNatural</span> <span class="token hvariable">n</span>
<span class="token comment">-- printing the computed sum</span>
   <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token string">"sum of the natural number in range 1 to "</span><span class="token operator">++</span> <span class="token builtin">show</span> <span class="token hvariable">n</span><span class="token punctuation">)</span>
   <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token builtin">sum</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

"sum of the natural number in range 1 to 20"
210

In the above program, We declared a function sumNatural as such it takes an integer as an argument and returns an integer. In its function definition, we are taking an integer argument n and returning n addition with a recursive call to itself with argument n-1. In the function base, if the argument is 1 the function returns 1. I.e the function returns the sum of the number from 1 to n. In the main function, we declared and initialized the variable for range value n. We invoked the function sumNatural with argument n and loaded the returned value into a variable sum. Finally, we printed the variable sum.

Example 3

Program to compute the sum of natural numbers using the list method/function sum

<div class="execute"></div><div class="code-mirror  language-haskell" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token hvariable">main</span> <span class="token operator">::</span> <span class="token constant">IO</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token hvariable">main</span> <span class="token operator">=</span> <span class="token keyword">do</span>
<span class="token comment">-- declaring and initializing the variable for range n</span>
   <span class="token keyword">let</span> <span class="token hvariable">n</span> <span class="token operator">=</span> <span class="token number">10</span>
<span class="token comment">-- generating a list of numbers from 1 to n</span>
   <span class="token keyword">let</span> <span class="token hvariable">numbers</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">..</span><span class="token hvariable">n</span><span class="token punctuation">]</span>
<span class="token comment">-- computing sum by using the list method sum</span>
   <span class="token keyword">let</span> <span class="token hvariable">sum1</span> <span class="token operator">=</span> <span class="token builtin">sum</span> <span class="token hvariable">numbers</span>
<span class="token comment">-- printing the computed sum</span>
   <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token string">"sum of the natural number in range 1 to "</span><span class="token operator">++</span> <span class="token builtin">show</span> <span class="token hvariable">n</span><span class="token punctuation">)</span>
   <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token hvariable">sum1</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

"sum of the natural number in range 1 to 10.0"
55.0

In the above program, we declared and initialized a variable n to load the range value. We generated a list of numbers from 1 to n using the ".." operator and loaded the list into a variable numbers. We invoked the function sum with the argument numbers. The function sum takes a number list and returns the sum of the elements in the list. We loaded the returned sum into a variable sum1 and Finally, we printed the variable sum1.

Conclusion

In this tutorial, we discussed three different ways to implement a program to calculate the sum of natural numbers in Haskell programming language.

Updated on: 2022-11-24T06:22:43+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements