Last active
June 10, 2019 03:36
-
-
Save brianonn/7b29bc0f77aba75ed723ff221c6c2acf to your computer and use it in GitHub Desktop.
test print out times tables in golang and erlang
This file contains 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
00_timestables_test | |
This is a test gist to try out times tables in golang and erlang |
This file contains 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" | |
"sync" | |
) | |
var wg sync.WaitGroup | |
func main() { | |
for n := 2; n <= 12; n++ { | |
wg.Add(1) | |
go timestable(n) | |
} | |
wg.Wait() | |
} | |
func timestable(x int) { | |
for i := 1; i < 12; i++ { | |
fmt.Printf("%d x %d = %d\n", i, x, i*x) | |
} | |
wg.Done() | |
} |
This file contains 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
%% @doc This is a module | |
%% | |
%% Start the erlang shell, compile and load this code by typing | |
%% `c(timestable).' | |
%% `timestable:main().' | |
%% | |
%% @end | |
-module(timestables). | |
-compile([export_all]). | |
timestable(N) -> | |
compute_times(N, 12). | |
compute_times(_X, 0) -> | |
ok; | |
compute_times(X, I) -> | |
io:format(user, "~w x ~w = ~w~n", [X, I, X*I]), | |
compute_times(X, I-1). | |
main() -> | |
spawn_workers(12). | |
spawn_workers(0) -> | |
ok; | |
spawn_workers(N) -> | |
spawn(fun() -> timestable(N) end), | |
spawn_workers(N-1). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment