How to Find Whether the given Number is PRIME or not using Recursion in Golang?


In mathematics, there are numbers that can be divisible by 1 or by itself, and such numbers are called Prime numbers. For example, 2, 3, 5, 7 … etc. In programming, we can create a program to check number is prime or not. In this article, we will use the concept of recursion which we call the function within the function to create a program to check number is prime or not.

Example 1

In this example, we are going to create a recursive function with two parameters one is the number and another one is the divisor. The number is one that we will check is prime or not and the divisor is initialized by the number −1. Then on each function calling we will check number % divisor is 0 or not and reduce the value of the divisor by 1. If at any point number is divisible by the divisor then we are returning false else in the end divisor reaches 1 then false.

Algorithm

  • Step 1: Import the required packages at the top using the import keyword.

  • Step 2: Then the main function will get run first.

    • First, we are declaring the int variable and then initialize it.

    • Now we are creating an if condition in which first we are checking whether the number is 1 or not. If not then we are calling the isPrime() function by passing the number and divisor as parameters.

    • According to if condition we are printing the result.

  • Step 3

    • In the isPrime() function we are having a base condition that the divisor is zero or not.

    • Then we are finding a reminder that it is zero or not.

    • Then we are calling the isPrime() function by reducing the divisor by 1.

Example

package main

import (
    // fmt package provides the function to print anything
    "fmt"
)

// declare the function with int type parameters and bool return type
func isPrime(number, divisor int) bool {
    // if the divisor reached 1 returning true
    if divisor == 1 {
        return true
    }
    // if the number is divisible by any number in between 1 and number them returning false
    if number%divisor == 0 {
        return false
    }

    // calling the function by reducing the divisor by 1
    return isPrime(number, divisor-1)
}
func main() {
    // declaring the number variable of the int type
    var number int

    fmt.Println("Golang program to find whether the number is prime or not using recursion.")

    // initializing the number that we want to check is prime or not
    number = 7

    // starting the if the condition that is checking that number is greater than 1 or not
    // also calling isPrime() number that is checking whether the number is prime or not
    // && is an operator in which if both the condition on the right and left are true then
    //Only we will go inside if block
    if number > 1 && isPrime(number, number-1) {
        fmt.Println("The number", number, "is a prime number.")
    } else {
        fmt.Println("The number", number, "is not a prime number.")
    }
}

Output 

Golang program to find whether the number is prime or not using recursion.
The number 7 is a prime number.

Conclusion

This is how we can find if the number is prime or not using the recursion function. There are different ways to check if the number is prime or not like the iterative way, or using the concept of sieve. As we are creating a separate function then we can say that the code is reusable. To learn more about Golang you can explore these tutorials.

Updated on: 10-Jul-2023

100 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements