🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

github.com/kavehmz/prime

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/kavehmz/prime

v1.0.0
Source
Go
Version published
Created
Source

Prime

Go Lang GoDoc Build Status Coverage Status Go Report Card Gitter

This is a Go library to produce prime numbers using all available cpu cores.

Installation

$ go get github.com/kavehmz/prime

Usage

package main

import (
	"fmt"
	"github.com/kavehmz/prime"
)

func main() {
	p := prime.Primes(1000000)
	fmt.Println("Number of primes:", len(p))
}

Algorithm

To find more about different methods to find a range of prime numbers you can look at following pages:

  • Sieve of Eratosthenes This is a more memory demanding method but faster by far for larger numbers. Here I have implemented both Segmented and non-Segmented methods. Segmented method had must less memory footprint.
  • Trial division Easier to understand and less memory consuming.

Performance

Performance depends on the size of max number. But as an example, it needs about 3ms to produce the first 1,000,000 prime numbers.

$ go test -bench .  
PASS
BenchmarkPrimes-4	     500	   3181972 ns/op
ok  	github.com/kavehmz/prime	1.618s
xno segmentsegmented
1,000,0000.003s0.007s
10,000,0000.035s0.044s
100,000,0000.642s0.345s
1,000,000,0008.253s3.146s

These calculations are done on a 3.1GHz Dual-core Intel Core i7.

Profiling

If you like to see how profiling in Go works and you have a usage Go installation you can use pprof.

First go and get the package

$ go get github.com/kavehmz/prime
$ cd $GOPATH/src/github.com/kavehmz/prime
$ go build example/main.go
$ ./main -cpuprofile=prime.prof  -memprofile=prime.mprof
$ # For inspecting memory usage do
$ go tool pprof main prime.mprof
$ # For inspecting cpu usage do
$ go tool pprof main prime.prof

Entering interactive mode (type "help" for commands)
(pprof) list

To learn how you have use pprof look at the following links:

Why

I used this simple library mainly to learn Go language, Go standards and for solving problems in https://projecteuler.net/

It can also be useful as a relatively fast implementation of prime numbers generator in Go.

FAQs

Package last updated on 05 Sep 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts