How to resolve the algorithm Even or odd step by step in the Go programming language
Published on 12 May 2024 09:40 PM
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:
-
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
- The code begins with the
-
main
Function:- The
main
function is the entry point of the program.
- The
-
test
Function:- The
test
function takes an integern
as its argument. - It prints the value of
n
usingfmt.Printf
and performs two tests to check ifn
is even or odd:n&1 == 0
: This is a bitwise operation that checks if the least significant bit (LSB) ofn
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 ofn
divided by 2 is 0. If it is,n
is considered even.
- The
-
testBig
Function:- The
testBig
function takes a strings
as its argument. - It parses
s
as a big integer usingnew(big.Int).SetString(s, 10)
. Themath/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
.
- The
-
Testing Different Values:
- The
main
function callstest
with various integer values (-2, -1, 0, 1, 2) andtestBig
with corresponding big integer values to demonstrate the behavior of these functions for both positive and negative numbers, including big integers.
- The
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