Skip to content
Snippets Groups Projects
Commit 8fb411fd authored by Jannik Bendfeld's avatar Jannik Bendfeld
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
# vendor/
# Go workspace file
go.work
\ No newline at end of file
# Fermat Primality Test
This small go program tests numbers for primality using the fermat primality test.
## Getting started
Simply start the program with:
```bash
go run cmd/fermatprimalitytest/main.go
```
\ No newline at end of file
package main
// Importing packages
import (
"fermatprimalitytest/internal"
"fmt"
)
// Main function
func main() {
n := 341
fmt.Printf("n %d is (probably) prim: %t\n", n, internal.IsProbablyPrimFermat(n, 2))
fmt.Printf("n %d is (probably) prim: %t\n", n, internal.IsProbablyPrimFermatMulti(n, []int{2, 3}))
}
go.mod 0 → 100644
module fermatprimalitytest
go 1.18
package internal
import (
"fmt"
"math/big"
)
func IsProbablyPrimFermat(n int, a int) bool {
if n%a == 0 {
return false
}
var aExp big.Int
aExp.SetUint64(1)
for i := 0; i < (n - 1); i++ {
aExp = *aExp.Mul(&aExp, big.NewInt(int64(a)))
}
if aExp.Mod(&aExp, big.NewInt(int64(n))).Cmp(big.NewInt(int64(1))) == 0 {
return true
} else {
return false
}
}
func IsProbablyPrimFermatMulti(n int, bases []int) bool {
for _, base := range bases {
if n > base {
if IsProbablyPrimFermat(n, base) {
fmt.Printf("True with base: %d and value %d\n", base, n)
} else {
fmt.Printf("False with base: %d and value %d\n", base, n)
return false
}
}
}
return true
}
package internal
import "testing"
func TestIsProbablyPrimFermat(t *testing.T) {
tables := []struct {
n int
a int
expected bool
}{
{3, 2, true},
{4, 2, false},
{5, 2, true},
{9, 2, false},
{341, 2, true}, // Pseudeo prim
{341, 3, false},
}
for _, table := range tables {
res := IsProbablyPrimFermat(table.n, table.a)
if res != table.expected {
t.Errorf("Test of n: %d and a: %d was incorrect, got: %t, expected: %t.", table.n, table.a, res, table.expected)
} else {
t.Logf("Test of n: %d and a: %d was correct, got: %t, expected: %t.", table.n, table.a, res, table.expected)
}
}
}
func TestIsProbablyPrimFermatMulti(t *testing.T) {
tables := []struct {
n int
bases []int
expected bool
}{
{3, []int{2, 3}, true},
{4, []int{2, 3}, false},
{5, []int{2, 3}, true},
{9, []int{2, 3}, false},
{341, []int{2}, true}, // Pseudeo prim
{341, []int{2, 3}, false},
}
for _, table := range tables {
res := IsProbablyPrimFermatMulti(table.n, table.bases)
if res != table.expected {
t.Errorf("Test of n: %d and bases: %d was incorrect, got: %t, expected: %t.", table.n, table.bases, res, table.expected)
} else {
t.Logf("Test of n: %d and bases: %d was correct, got: %t, expected: %t.", table.n, table.bases, res, table.expected)
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment