File tree Expand file tree Collapse file tree 3 files changed +45
-12
lines changed Expand file tree Collapse file tree 3 files changed +45
-12
lines changed Original file line number Diff line number Diff line change 11# A line by line translation of the Erlang version
22defmodule Primes do
3- def main ( _args ) do
3+ def main ( ) do
44 max = 1000
55
66 2 .. max
@@ -37,4 +37,4 @@ defmodule Primes do
3737 end
3838end
3939
40- Primes . main ( System . argv ( ) )
40+ Primes . main ( )
Original file line number Diff line number Diff line change 11defmodule Primes do
2- def main ( _args ) do
3- 2 .. 1000
4- |> Task . async_stream (
5- & print_if_prime / 1 ,
6- max_concurrency: System . schedulers_online ( ) )
7- |> Stream . run ( )
8- end
9-
10- defp print_if_prime ( n ) do
2+ def print_if_prime ( n ) do
113 if is_prime? ( n ) , do: IO . write ( "#{ n } " )
124 end
135
@@ -20,4 +12,9 @@ defmodule Primes do
2012 end
2113end
2214
23- Primes . main ( System . argv ( ) )
15+ 2 .. 1000
16+ |> Task . async_stream (
17+ & Primes . print_if_prime / 1 ,
18+ max_concurrency: System . schedulers_online ( ) ,
19+ ordered: false )
20+ |> Stream . run ( )
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import (
4+ "fmt"
5+ "sync"
6+ )
7+
8+ func main () {
9+ max := 1000
10+ var wg sync.WaitGroup
11+ for n := 2 ; n <= max ; n ++ {
12+ wg .Add (1 )
13+ go func (n int ) {
14+ defer wg .Done ()
15+ if isPrime (n ) {
16+ fmt .Printf ("%d " , n )
17+ }
18+ }(n )
19+ }
20+ wg .Wait ()
21+ }
22+
23+ func isPrime (n int ) bool {
24+ if n == 2 || n == 3 {
25+ return true
26+ }
27+ if n <= 1 || n % 2 == 0 || n % 3 == 0 {
28+ return false
29+ }
30+ for i := 5 ; i * i <= n ; i += 6 {
31+ if n % i == 0 || n % (i + 2 ) == 0 {
32+ return false
33+ }
34+ }
35+ return true
36+ }
You can’t perform that action at this time.
0 commit comments