Last active
August 27, 2020 18:45
-
-
Save kylelemons/c010835549dc595d5c36f387ef3d2c45 to your computer and use it in GitHub Desktop.
Perfect numbers in parallel
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 ( | |
"flag" | |
"fmt" | |
"runtime" | |
"sync" | |
"time" | |
) | |
var ( | |
n = flag.Int("n", 1000, "Max number to search") | |
workers = flag.Int("workers", runtime.GOMAXPROCS(0), "Number of workers") | |
updates = flag.Duration("updates", 10*time.Second, "How often to print updates") | |
) | |
func isPerfect(n int) bool { | |
var sum int | |
for i := 1; i*i < n; i++ { | |
if n%i != 0 { | |
continue | |
} | |
sum += i | |
if i != 1 { | |
sum += n / i | |
} | |
} | |
return sum == n | |
} | |
func main() { | |
flag.Parse() | |
progress := time.Tick(*updates) | |
work := make(chan int, *workers*2) | |
perfect := make(chan int) | |
var wg sync.WaitGroup | |
for i := 0; i < *workers; i++ { | |
wg.Add(1) | |
go func() { | |
defer wg.Done() | |
for { | |
candidate, ok := <-work | |
if !ok { | |
return | |
} | |
if isPerfect(candidate) { | |
perfect <- candidate | |
} | |
} | |
}() | |
} | |
go func() { | |
for i := 2; i <= *n; { | |
select { | |
case work <- i: | |
i++ | |
case <-progress: | |
fmt.Printf("... %d\n", i) | |
} | |
} | |
close(work) | |
wg.Wait() | |
close(perfect) | |
}() | |
start := time.Now() | |
count := 0 | |
for { | |
select { | |
case n, ok := <-perfect: | |
if !ok { | |
goto done | |
} | |
fmt.Printf("Perfect: %d\n", n) | |
count++ | |
} | |
} | |
done: | |
fmt.Printf("Found %d perfect numbers under %d in %s\n", count, *n, time.Since(start)) | |
} |
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
$ go run perfect.go -n 100000000 | |
Perfect: 6 | |
Perfect: 28 | |
Perfect: 496 | |
Perfect: 8128 | |
... 7205645 | |
... 14711469 | |
... 20996253 | |
... 26584596 | |
... 31696347 | |
Perfect: 33550336 | |
... 36497108 | |
... 41046846 | |
... 45396598 | |
... 49563289 | |
... 53581849 | |
... 57448907 | |
... 61230252 | |
... 64904562 | |
... 68490501 | |
... 72000477 | |
... 75433485 | |
... 78789168 | |
... 82097531 | |
... 85334579 | |
... 88529677 | |
... 91678190 | |
... 94786011 | |
... 97847648 | |
Found 5 perfect numbers under 100000000 in 3m57.070214021s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment