Skip to content

Instantly share code, notes, and snippets.

@brianonn
Last active June 10, 2019 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianonn/7b29bc0f77aba75ed723ff221c6c2acf to your computer and use it in GitHub Desktop.
Save brianonn/7b29bc0f77aba75ed723ff221c6c2acf to your computer and use it in GitHub Desktop.
test print out times tables in golang and erlang
00_timestables_test
This is a test gist to try out times tables in golang and erlang
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()
}
%% @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