How to resolve the algorithm Entropy/Narcissist step by step in the Go programming language

Published on 12 May 2024 09:40 PM
#Go

How to resolve the algorithm Entropy/Narcissist step by step in the Go programming language

Table of Contents

Problem Statement

Write a computer program that computes and shows its own   entropy.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Entropy/Narcissist step by step in the Go programming language

Source code overview

This Go program calculates the entropy of two files, one source code file and one binary file. Entropy is a measure of uncertainty or randomness in a data set. It is often used to quantify the security of a cipher or the randomness of a random number generator.

Implementation details

  1. The program starts by importing the necessary packages:

    • fmt for formatting output
    • io/ioutil for reading files
    • log for logging errors
    • math for mathematical operations
    • os for accessing the operating system and environment variables
    • runtime for getting the name of the currently executing function
  2. The main function is the entry point of the program. It calls the entropy function twice, once for the source code file and once for the binary file. The entropy function takes a file name as an argument and returns the entropy of that file.

  3. The entropy function reads the contents of the file into a byte slice. It then initializes an array of 256 floating-point numbers, one for each possible byte value. The array is used to count the number of occurrences of each byte value in the file.

  4. The entropy function then calculates the entropy of the file using the following formula:

entropy = log2(N) - H(X) / N

where:

  • N is the number of bytes in the file
  • H(X) is the entropy of the byte distribution
  1. The entropy of the byte distribution is calculated using the following formula:
H(X) = -∑p(x) * log2(p(x))

where:

  • p(x) is the probability of occurrence of byte value x
  1. The entropy function returns the calculated entropy.

Sample output

The output of the program will vary depending on the files that are being analyzed. For example, the following is the output of the program when run on the source code file main.go and the binary file main:

Source file entropy: 4.318027936966649
Binary file entropy: 8.0

Source code in the go programming language

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "math"
    "os"
    "runtime"
)

func main() {
    _, src, _, _ := runtime.Caller(0)
    fmt.Println("Source file entropy:", entropy(src))
    fmt.Println("Binary file entropy:", entropy(os.Args[0]))
}

func entropy(file string) float64 {
    d, err := ioutil.ReadFile(file)
    if err != nil {
        log.Fatal(err)
    }
    var f [256]float64
    for _, b := range d {
        f[b]++
    }
    hm := 0.
    for _, c := range f {
        if c > 0 {
            hm += c * math.Log2(c)
        }
    }
    l := float64(len(d))
    return math.Log2(l) - hm/l
}


  

You may also check:How to resolve the algorithm Pointers and references step by step in the SAS programming language
You may also check:How to resolve the algorithm Check Machin-like formulas step by step in the Factor programming language
You may also check:How to resolve the algorithm Humble numbers step by step in the Perl programming language
You may also check:How to resolve the algorithm Set consolidation step by step in the SETL programming language
You may also check:How to resolve the algorithm Munchausen numbers step by step in the Raku programming language