Forked from dhlavaty/go-lang-factorization-tests-multithreaded.go
Created
October 23, 2017 13:22
-
-
Save 4ndrej/3729b01eeaf5b2baa4e60ab414f966a5 to your computer and use it in GitHub Desktop.
Factorization test in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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