Skip to content

Instantly share code, notes, and snippets.

@aberke
Last active January 5, 2017 20:27
Show Gist options
  • Save aberke/c2480a5d5a42b396d6d7cafa498dfee2 to your computer and use it in GitHub Desktop.
Save aberke/c2480a5d5a42b396d6d7cafa498dfee2 to your computer and use it in GitHub Desktop.
Snap Crackle Pop: Prints out the numbers 1 to 100 (inclusive). If the number is divisible by 3, print Crackle instead of the number. If it's divisible by 5, print Pop. If it's divisible by both 3 and 5, print CracklePop.
%%%-------------------------------------------------------------------
%%% @doc
%%% Snap Crackle Pop
%%% Prints out the numbers 1 to 100 (inclusive).
%%% If the number is divisible by 3, print Crackle instead of the number.
%%% If it's divisible by 5, print Pop.
%%% If it's divisible by both 3 and 5, print CracklePop.
%%%
%%% Run (the simple way):
%%% - Download erlang/otp: $ brew install erlang
%%% - Start erlang: $ erl
%%% - Compile + run: > c(snapcracklepop). snapcracklepop:snap_crackle_pop().
%%%
%%% @end
%%% @contributors:
%%% Alexandra Berke (github: aberke)
%%%-------------------------------------------------------------------
-module(snapcracklepop).
-export([
snap_crackle_pop/0
]).
-define(MIN, 1).
-define(MAX, 100).
print_snap_crackle_pop(N) when (N rem 15 =:= 0) -> io:format("CracklePop~n");
print_snap_crackle_pop(N) when (N rem 3 =:= 0) -> io:format("Crackle~n");
print_snap_crackle_pop(N) when (N rem 5 =:= 0) -> io:format("Pop~n");
print_snap_crackle_pop(N) -> io:format("~p~n", [N]).
snap_crackle_pop(N) when N > ?MAX -> ok;
snap_crackle_pop(N) ->
print_snap_crackle_pop(N),
snap_crackle_pop(N + 1).
-spec snap_crackle_pop() -> ok.
snap_crackle_pop() ->
snap_crackle_pop(?MIN).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment