Golang

 

Analyze Fibonacci and Arithmetic Progression sequences using Go programming language

Introduction

Number sequences such as Fibonacci and Arithmetic Progression (AP) play an important role in mathematics and computer science. Understanding how to generate and analyze such sequences is crucial for a variety of problems in data science, algorithm design, and more. In this tutorial, we will create a simple Number Sequence Analyzer program in Go that can analyze and generate both Fibonacci sequences and Arithmetic Progressions (AP).

Objective

The objective of this program is to provide a tool that can compute and analyze common mathematical sequences like Fibonacci and Arithmetic Progression. Users will be able to input values and get results for the first ‘n’ terms of the selected sequence.

Go Program Code


package main

import (
    "fmt"
    "os"
    "strconv"
)

func fibonacci(n int) []int {
    sequence := make([]int, n)
    sequence[0] = 0
    if n > 1 {
        sequence[1] = 1
    }
    for i := 2; i < n; i++ {
        sequence[i] = sequence[i-1] + sequence[i-2]
    }
    return sequence
}

func arithmeticProgression(firstTerm, commonDifference, n int) []int {
    sequence := make([]int, n)
    for i := 0; i < n; i++ {
        sequence[i] = firstTerm + i*commonDifference
    }
    return sequence
}

func main() {
    fmt.Println("Welcome to the Number Sequence Analyzer!")

    // Input sequence choice
    fmt.Println("Enter 'fibonacci' for Fibonacci Sequence or 'ap' for Arithmetic Progression:")
    var choice string
    fmt.Scanln(&choice)

    // Input number of terms
    fmt.Println("Enter the number of terms:")
    var terms int
    fmt.Scanln(&terms)

    // Process based on the choice
    switch choice {
    case "fibonacci":
        sequence := fibonacci(terms)
        fmt.Println("Fibonacci Sequence:")
        fmt.Println(sequence)
    case "ap":
        fmt.Println("Enter the first term of the Arithmetic Progression:")
        var firstTerm int
        fmt.Scanln(&firstTerm)

        fmt.Println("Enter the common difference:")
        var commonDifference int
        fmt.Scanln(&commonDifference)

        sequence := arithmeticProgression(firstTerm, commonDifference, terms)
        fmt.Println("Arithmetic Progression Sequence:")
        fmt.Println(sequence)
    default:
        fmt.Println("Invalid choice. Please enter 'fibonacci' or 'ap'.")
        os.Exit(1)
    }
}
            

Program Structure and Explanation

The program consists of three main parts:

  • Fibonacci Function: This function generates the Fibonacci sequence up to the ‘n’ terms. It initializes the first two numbers as 0 and 1, and each subsequent number is the sum of the two preceding ones.
  • Arithmetic Progression Function: This function generates an Arithmetic Progression starting from a given first term and a common difference, producing the sequence up to ‘n’ terms.
  • Main Function: This is the entry point of the program. It asks the user to choose between Fibonacci or Arithmetic Progression, inputs the number of terms, and calls the appropriate function based on the user’s choice.

How to Run the Program

To run the program, follow these steps:

    1. Ensure that Go is installed on your system. If not, download and install it from here.
    2. Create a new Go file, e.g., sequence_analyzer.go, and paste the code provided above into this file.
    3. Open your terminal or command prompt, navigate to the directory where your Go file is located, and run the following command:
go run sequence_analyzer.go
  1. The program will prompt you to select the type of sequence (Fibonacci or AP) and ask for the number of terms. Follow the prompts to get your results.
© 2025 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)