pi

package module
v2.0.0-rc7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 3, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package pi calculates the nth and 8 following fractional decimal digits of pi using a Bailey-Borwein-Plouffe algorithm (see https://wikipedia.org/wiki/Bailey%E2%80%93Borwein%E2%80%93Plouffe_formula). This allows any arbitrary fractional digit of pi to be calculated independently of the preceding digits albeit with longer calculation times as the value of n increases because of the need to calculate prime numbers of increasing value.

NOTE 1: This package is intended to be used in distributed computing and cloud scaling demos, and does not guarantee accuracy or efficiency of calculated fractional digits.

NOTE 2: The algorithms used invMod, powMod, and BPPDigits are based on the work of Fabrice Bellard (original source code published at https://bellard.org/pi/pi.c).

Example
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/go-logr/stdr"
	pi "github.com/memes/pi/v2"
)

func main() {
	// Enable verbose library logging to standard library logger via go-logr/stdr
	_ = stdr.SetVerbosity(1)
	pi.Logger = stdr.New(log.New(os.Stdout, "", log.Lshortfile))
	// Calculate the first 9 decimal digits of pi
	digits := pi.BBPDigits(0)
	fmt.Printf("The first 10 digits of pi are: 3.%s\n", digits)
}
Output:

pi.go:109: "level"=1 "msg"="BBPDigits: enter" "n"=0
pi.go:171: "level"=1 "msg"="BBPDigits: exit" "n"=0 "result"="141592653"
The first 10 digits of pi are: 3.141592653

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// The package logr.Logger instance to use.
	//nolint: gochecknoglobals // Allow package consumers to set the logger
	Logger = logr.Discard()
	// The number of Miller-Rabin rounds to use in FindNextPrime when
	// determining if an integer is probabilistically a prime. A value of
	// zero will apply a Baillie-PSW only test and requires Go 1.8+.
	//nolint: gochecknoglobals // Allow package consumers to override
	MillerRabinRounds = 0
)

Functions

func BBPDigits

func BBPDigits(n uint64) string

Implements a BBP spigot algorithm to determine the nth and 8 following fractional decimal digits of pi at the specified zero-based offset.

Example
package main

import (
	"fmt"

	pi "github.com/memes/pi/v2"
)

func main() {
	// Print the first 100 digits of pi using a default calculator
	fmt.Printf("The first 100 digits of pi are: 3.")
	for n := uint64(0); n < 99; n += 9 {
		fmt.Print(pi.BBPDigits(n))
	}
	fmt.Println()
}
Output:

The first 100 digits of pi are: 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067

func FindNextPrime

func FindNextPrime(n int64) int64

Return the next largest prime number that is greater than n.

Example
package main

import (
	"fmt"

	pi "github.com/memes/pi/v2"
)

func main() {
	// Find the first prime number after 10,000
	nextPrime := pi.FindNextPrime(10000)
	fmt.Printf("The first prime number greater than 10000 is %d\n", nextPrime)
}
Output:

The first prime number greater than 10000 is 10007

Types

This section is empty.

Directories

Path Synopsis
cmd
pi
This package contains a gRPC client and server to demonstrate use of library in a distributed environment.
This package contains a gRPC client and server to demonstrate use of library in a distributed environment.
pkg
cache
Package cache defines a common interface for cache implementations that can be used by PiService implementations.
Package cache defines a common interface for cache implementations that can be used by PiService implementations.
client
Package client implements a gRPC client implementation that satisfies the PiServiceClient interface requirements with optional OpenTelemetry metrics and traces.
Package client implements a gRPC client implementation that satisfies the PiServiceClient interface requirements with optional OpenTelemetry metrics and traces.
generated
Package generated contains the generated code for gRPC client and server stubs, and an OpenAPIv2 (swagger) JSON declaration, from protobuf source(s).
Package generated contains the generated code for gRPC client and server stubs, and an OpenAPIv2 (swagger) JSON declaration, from protobuf source(s).
server
Package server implements a gRPC server (and optional REST gateway) implementation that satisfies the PiServiceClient interface requirements, with optional OpenTelemetry metrics and traces.
Package server implements a gRPC server (and optional REST gateway) implementation that satisfies the PiServiceClient interface requirements, with optional OpenTelemetry metrics and traces.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL