Skip to content

Instantly share code, notes, and snippets.

@dhlavaty
Last active October 23, 2017 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dhlavaty/a8ea12423a3f8292da0d66e0b2b02dd2 to your computer and use it in GitHub Desktop.
Save dhlavaty/a8ea12423a3f8292da0d66e0b2b02dd2 to your computer and use it in GitHub Desktop.
Factorization test in golang
package main
import (
"fmt"
"math/big"
"sync"
)
func runInThread(wg *sync.WaitGroup, startFrom int64, numOfIterations int64) {
defer wg.Done()
bigInt := new(big.Int)
zero := big.NewInt(0)
// Suchalove N
N := new(big.Int)
N.SetString("19320234965388231365540331093872230283778945634556596682214163280186385949444714747846212850116256215110019915157460836374458213753399271651569995871137126916385002459283305264844322656670858555695350602088849590984911208795341527784373572568199485660335040039545712139902834483955502131356223511756716987839814223560695397544760324236593252526008468733300092541502234485830371545572743645953216843356025562571409688147979531382601119854626778211029567854618943714104772640084682336503263097825328909488844956732298839600439486781366754445139880815199642118131001675897147726276626348623513074701194667574356320780611", 10)
for i := startFrom; i < numOfIterations; i += 2 {
result := bigInt.Mod(N, big.NewInt(i))
if result.Cmp(zero) == 0 {
fmt.Println(i)
}
}
fmt.Println("runInThread(%v,%v) ended", startFrom, numOfIterations)
}
func main() {
var wg sync.WaitGroup
numOfIterations := 100000000
numberOfThreads := 8
add := numOfIterations / numberOfThreads
for i := 0; i < numberOfThreads; i += 1 {
wg.Add(1)
go runInThread(&wg, int64((i * add) + 1), int64(((i + 1) * add)))
}
wg.Wait()
fmt.Println("main() ended")
}
package main
import (
"fmt"
"math/big"
)
func main() {
bigInt := new(big.Int)
zero := big.NewInt(0)
numOfIterations := int64(100000000)
// Suchalove N
N := new(big.Int)
N.SetString("19320234965388231365540331093872230283778945634556596682214163280186385949444714747846212850116256215110019915157460836374458213753399271651569995871137126916385002459283305264844322656670858555695350602088849590984911208795341527784373572568199485660335040039545712139902834483955502131356223511756716987839814223560695397544760324236593252526008468733300092541502234485830371545572743645953216843356025562571409688147979531382601119854626778211029567854618943714104772640084682336503263097825328909488844956732298839600439486781366754445139880815199642118131001675897147726276626348623513074701194667574356320780611", 10)
for i := int64(1); i < numOfIterations; i += 2 {
result := bigInt.Mod(N, big.NewInt(i))
if result.Cmp(zero) == 0 {
fmt.Println(i)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment