How to resolve the algorithm Even or odd step by step in the Go programming language

Published on 12 May 2024 09:40 PM
#Go

How to resolve the algorithm Even or odd step by step in the Go programming language

Table of Contents

Problem Statement

Test whether an integer is even or odd. There is more than one way to solve this task:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Even or odd step by step in the Go programming language

The provided Go code defines two functions, test and testBig, which test whether an integer or big integer (represented as a string) is even or odd. Here's a detailed explanation of the code:

  1. import Statements:

    • The code begins with the import statements that import necessary standard libraries:
      • "fmt" for input and output operations
      • "math/big" for handling big integer operations
  2. main Function:

    • The main function is the entry point of the program.
  3. test Function:

    • The test function takes an integer n as its argument.
    • It prints the value of n using fmt.Printf and performs two tests to check if n is even or odd:
      • n&1 == 0: This is a bitwise operation that checks if the least significant bit (LSB) of n is 0. If it is, n is considered even because the LSB of even numbers is always 0.
      • n%2 == 0: This is a modulo operation that checks if the remainder of n divided by 2 is 0. If it is, n is considered even.
  4. testBig Function:

    • The testBig function takes a string s as its argument.
    • It parses s as a big integer using new(big.Int).SetString(s, 10). The math/big library provides support for handling big integers not supported by regular integer types in Go.
    • It prints the value of the big integer using fmt.Printf.
    • It tests if the big integer is even by checking its least significant bit using b.Bit(0) == 0.
  5. Testing Different Values:

    • The main function calls test with various integer values (-2, -1, 0, 1, 2) and testBig with corresponding big integer values to demonstrate the behavior of these functions for both positive and negative numbers, including big integers.

Source code in the go programming language

package main

import (
    "fmt"
    "math/big"
)

func main() {
    test(-2)
    test(-1)
    test(0)
    test(1)
    test(2)
    testBig("-222222222222222222222222222222222222")
    testBig("-1")
    testBig("0")
    testBig("1")
    testBig("222222222222222222222222222222222222")
}

func test(n int) {
    fmt.Printf("Testing integer %3d:  ", n)
    // & 1 is a good way to test
    if n&1 == 0 {
        fmt.Print("even ")
    } else {
        fmt.Print(" odd ")
    }
    // Careful when using %: negative n % 2 returns -1.  So, the code below
    // works, but can be broken by someone thinking they can reverse the
    // test by testing n % 2 == 1.  The valid reverse test is n % 2 != 0.
    if n%2 == 0 {
        fmt.Println("even")
    } else {
        fmt.Println(" odd")
    }
}

func testBig(s string) {
    b, _ := new(big.Int).SetString(s, 10)
    fmt.Printf("Testing big integer %v:  ", b)
    // the Bit function is the only sensible test for big ints.
    if b.Bit(0) == 0 {
        fmt.Println("even")
    } else {
        fmt.Println("odd")
    }
}


  

You may also check:How to resolve the algorithm Trigonometric functions step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Text processing/Max licenses in use step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Arithmetic evaluation step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Dot product step by step in the C programming language
You may also check:How to resolve the algorithm Doubly-linked list/Definition step by step in the Clojure programming language