Giter Site home page Giter Site logo

h5law / primality Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 37 KB

A Go library for checking whether an integer is prime or not, using either the AKS or Miller-Rabin algorithms.

Home Page: https://pkg.go.dev/github.com/h5law/primality

License: BSD 3-Clause "New" or "Revised" License

Go 100.00%
aks go golang miller-rabin number-theory primality-test prime-numbers

primality's Introduction

primality

Primality is a Golang library for determining whether any given integer $i\in\mathbb{Z}^+,~i\ge1$. The library provides two implementations of algorithms for determining the primality of an arbitrarily sized integer, using the math/big library.

The two methods provided are the Miller-Rabin and AKS primality test the former being probabilistic and the latter deterministic, meaning that the Miller-Rabin primality test doesn't guarantee 100% accuracy in certain situations. Whereas the AKS primality test is always 100% accurate - but a lot slower on larger integers.

Features

Miller-Rabin:

  • Highly accurate probabilistic primality test
    • 25 rounds and force usage of base 2 recommended for near 100% accuracy
  • Arbitrarily large integer support (big.Int)
  • $\mathcal{O}(r\cdot s)$ time complexity (assuming big.Int operations are $\mathcal{O}(1)$ where $r$ is the number of repetitions and $s$ the number of trailing zeros on $n$, the number being tested - otherwise it is related to the operations of big.Int integers and $n$ itself.
    • As a prime must be odd $s\le7$ meaning the time complexity in its worst case is $\mathcal{O}(175)=\mathcal{O}(1)$ with 25 repetitions.

AKS

  • Deterministic primality test
    • Slow overall - but guarantees 100% a valid outcome
  • int support only

TODOs

  • Improve speed of the AKS method
    • Specifically step 5 but overall it is slow
  • Make the AKS method work on arbitrarily sized integers
    • use big.Ints over ints
  • Determine the true time and space complexities for both methods

primality's People

Contributors

h5law avatar

Watchers

 avatar

primality's Issues

Bug: Determine the cause and fix the issue with false positives in the AKS method

Overview

The AKS method produces false positive outputs as can be seen by running the following code and confirming the resulting array of primes is indeed correct.

package main

import (
	"fmt"
	"math/big"

	"github.com/h5law/primality"
)

func main() {
	res := make([]int, 0, 1000)
	for i := 1; i <= 1000; i++ {
		aks := primality.AKS(i)
		mr := primality.MillerRabin(big.NewInt(int64(i)), 25, true)
		if aks != mr {
			fmt.Printf("%d: AKS(%v) - MillerRabin(%v)\n", i, aks, mr)
		} else if aks && mr {
			res = append(res, i)
		}
	}
	fmt.Println("Total found: ", len(res))
	fmt.Println(res)
}

Issue

Determine if this was introduced through code change or whether the implementation has been incorrect the entire time.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.