From 3f8299430258179f90a15c5cdfefcc6037f6908a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Wed, 10 Apr 2024 05:52:31 -0700 Subject: [PATCH] Add collatz-conjecture (#204) --- config.json | 8 ++++ .../collatz-conjecture/.docs/instructions.md | 29 ++++++++++++++ .../practice/collatz-conjecture/.gitignore | 11 ++++++ .../collatz-conjecture/.meta/config.json | 19 ++++++++++ .../collatz-conjecture/.meta/example.lfe | 19 ++++++++++ .../collatz-conjecture/.meta/tests.toml | 38 +++++++++++++++++++ .../practice/collatz-conjecture/Makefile | 21 ++++++++++ .../practice/collatz-conjecture/rebar.config | 11 ++++++ .../practice/collatz-conjecture/rebar.lock | 8 ++++ .../src/collatz-conjecture.app.src | 11 ++++++ .../src/collatz-conjecture.lfe | 4 ++ .../test/collatz-conjecture-tests.lfe | 23 +++++++++++ 12 files changed, 202 insertions(+) create mode 100644 exercises/practice/collatz-conjecture/.docs/instructions.md create mode 100644 exercises/practice/collatz-conjecture/.gitignore create mode 100644 exercises/practice/collatz-conjecture/.meta/config.json create mode 100644 exercises/practice/collatz-conjecture/.meta/example.lfe create mode 100644 exercises/practice/collatz-conjecture/.meta/tests.toml create mode 100644 exercises/practice/collatz-conjecture/Makefile create mode 100644 exercises/practice/collatz-conjecture/rebar.config create mode 100644 exercises/practice/collatz-conjecture/rebar.lock create mode 100644 exercises/practice/collatz-conjecture/src/collatz-conjecture.app.src create mode 100644 exercises/practice/collatz-conjecture/src/collatz-conjecture.lfe create mode 100644 exercises/practice/collatz-conjecture/test/collatz-conjecture-tests.lfe diff --git a/config.json b/config.json index 33692bde..64551168 100644 --- a/config.json +++ b/config.json @@ -137,6 +137,14 @@ "prerequisites": [], "difficulty": 1 }, + { + "slug": "collatz-conjecture", + "name": "Collatz Conjecture", + "uuid": "3796230e-f37e-464c-bb22-8dc3d1664aec", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "difference-of-squares", "name": "Difference of Squares", diff --git a/exercises/practice/collatz-conjecture/.docs/instructions.md b/exercises/practice/collatz-conjecture/.docs/instructions.md new file mode 100644 index 00000000..ba060483 --- /dev/null +++ b/exercises/practice/collatz-conjecture/.docs/instructions.md @@ -0,0 +1,29 @@ +# Instructions + +The Collatz Conjecture or 3x+1 problem can be summarized as follows: + +Take any positive integer n. +If n is even, divide n by 2 to get n / 2. +If n is odd, multiply n by 3 and add 1 to get 3n + 1. +Repeat the process indefinitely. +The conjecture states that no matter which number you start with, you will always reach 1 eventually. + +Given a number n, return the number of steps required to reach 1. + +## Examples + +Starting with n = 12, the steps would be as follows: + +0. 12 +1. 6 +2. 3 +3. 10 +4. 5 +5. 16 +6. 8 +7. 4 +8. 2 +9. 1 + +Resulting in 9 steps. +So for input n = 12, the return value would be 9. diff --git a/exercises/practice/collatz-conjecture/.gitignore b/exercises/practice/collatz-conjecture/.gitignore new file mode 100644 index 00000000..6dd20ff0 --- /dev/null +++ b/exercises/practice/collatz-conjecture/.gitignore @@ -0,0 +1,11 @@ +## -*- conf -*- +.rebar3 +_build/ +ebin/ +erl_crash.dump +rebar3.crashdump + +tmp +bin/configlet +bin/configlet.exe +CHECKLIST diff --git a/exercises/practice/collatz-conjecture/.meta/config.json b/exercises/practice/collatz-conjecture/.meta/config.json new file mode 100644 index 00000000..bbc95e8b --- /dev/null +++ b/exercises/practice/collatz-conjecture/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "src/collatz-conjecture.lfe" + ], + "test": [ + "test/collatz-conjecture-tests.lfe" + ], + "example": [ + ".meta/example.lfe" + ] + }, + "blurb": "Calculate the number of steps to reach 1 using the Collatz conjecture.", + "source": "An unsolved problem in mathematics named after mathematician Lothar Collatz", + "source_url": "https://en.wikipedia.org/wiki/3x_%2B_1_problem" +} diff --git a/exercises/practice/collatz-conjecture/.meta/example.lfe b/exercises/practice/collatz-conjecture/.meta/example.lfe new file mode 100644 index 00000000..59e039d1 --- /dev/null +++ b/exercises/practice/collatz-conjecture/.meta/example.lfe @@ -0,0 +1,19 @@ +(defmodule collatz-conjecture + (export (steps 1))) + +(defun add1 (n) (+ n 1)) + +(defun zerop (n) (=:= (rem n 2))) + +(defun steps (start) + (do-steps start 0)) + +(defun do-steps + ((n _) (when (< n 1)) + (error "Only positive integers are allowed")) + ((1 acc) + acc) + ((n acc) (when (=:= (rem n 2) 0)) + (do-steps (div n 2) (add1 acc))) + ((n acc) + (do-steps (add1 (* 3 n)) (add1 acc)))) diff --git a/exercises/practice/collatz-conjecture/.meta/tests.toml b/exercises/practice/collatz-conjecture/.meta/tests.toml new file mode 100644 index 00000000..cc34e168 --- /dev/null +++ b/exercises/practice/collatz-conjecture/.meta/tests.toml @@ -0,0 +1,38 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[540a3d51-e7a6-47a5-92a3-4ad1838f0bfd] +description = "zero steps for one" + +[3d76a0a6-ea84-444a-821a-f7857c2c1859] +description = "divide if even" + +[754dea81-123c-429e-b8bc-db20b05a87b9] +description = "even and odd steps" + +[ecfd0210-6f85-44f6-8280-f65534892ff6] +description = "large number of even and odd steps" + +[7d4750e6-def9-4b86-aec7-9f7eb44f95a3] +description = "zero is an error" +include = false + +[2187673d-77d6-4543-975e-66df6c50e2da] +description = "zero is an error" +reimplements = "7d4750e6-def9-4b86-aec7-9f7eb44f95a3" + +[c6c795bf-a288-45e9-86a1-841359ad426d] +description = "negative value is an error" +include = false + +[ec11f479-56bc-47fd-a434-bcd7a31a7a2e] +description = "negative value is an error" +reimplements = "c6c795bf-a288-45e9-86a1-841359ad426d" diff --git a/exercises/practice/collatz-conjecture/Makefile b/exercises/practice/collatz-conjecture/Makefile new file mode 100644 index 00000000..fbb5a7de --- /dev/null +++ b/exercises/practice/collatz-conjecture/Makefile @@ -0,0 +1,21 @@ +ERL := $(shell which erl) +REBAR3 := $(shell which rebar3) + +null := +space := $(null) # +comma := , + +ifeq ($(ERL),) + $(error Can't find Erlang executable 'erl') +else ifeq ($(REBAR3),) + $(error Can't find rebar3) +endif + +compile: ; $(REBAR3) compile + +clean: ; $(REBAR3) clean + +.PHONY: test +test: + $(REBAR3) eunit \ + -m $(subst $(space),$(comma),$(basename $(notdir $(wildcard test/*.lfe)))) diff --git a/exercises/practice/collatz-conjecture/rebar.config b/exercises/practice/collatz-conjecture/rebar.config new file mode 100644 index 00000000..d53487ac --- /dev/null +++ b/exercises/practice/collatz-conjecture/rebar.config @@ -0,0 +1,11 @@ +{plugins, [{rebar3_lfe, "0.4.3"}]}. + +{provider_hooks, [{post, [{compile, {lfe, compile}}]}]}. + +{deps, [{lfe, "2.1.1"}]}. + +{profiles, + [{test, + [{eunit_compile_opts, [{src_dirs, ["src", "test"]}]}, + {deps, + [{ltest, "0.13.3"}]}]}]}. diff --git a/exercises/practice/collatz-conjecture/rebar.lock b/exercises/practice/collatz-conjecture/rebar.lock new file mode 100644 index 00000000..d5a6b3b9 --- /dev/null +++ b/exercises/practice/collatz-conjecture/rebar.lock @@ -0,0 +1,8 @@ +{"1.2.0", +[{<<"lfe">>,{pkg,<<"lfe">>,<<"2.1.1">>},0}]}. +[ +{pkg_hash,[ + {<<"lfe">>, <<"4A888B26172D198DC7A5AFEB897E8248AF7D56E1638D9C8249AAF933AE811B96">>}]}, +{pkg_hash_ext,[ + {<<"lfe">>, <<"C484D3B655D40DED58BC41B17B22F173711C681BF36063A234A9BAA9506947E1">>}]} +]. diff --git a/exercises/practice/collatz-conjecture/src/collatz-conjecture.app.src b/exercises/practice/collatz-conjecture/src/collatz-conjecture.app.src new file mode 100644 index 00000000..3d3cfa37 --- /dev/null +++ b/exercises/practice/collatz-conjecture/src/collatz-conjecture.app.src @@ -0,0 +1,11 @@ +%% -*- erlang -*- +{application, 'collatz-conjecture', + [{description, ""}, + {vsn, "0.0.1"}, + {modules, + ['collatz-conjecture']}, + {registered, []}, + {applications, + [kernel, stdlib]}, + {included_applications, []}, + {env, []}]}. diff --git a/exercises/practice/collatz-conjecture/src/collatz-conjecture.lfe b/exercises/practice/collatz-conjecture/src/collatz-conjecture.lfe new file mode 100644 index 00000000..47b1b245 --- /dev/null +++ b/exercises/practice/collatz-conjecture/src/collatz-conjecture.lfe @@ -0,0 +1,4 @@ +(defmodule collatz-conjecture + (export (steps 1))) + + ; Please implement the steps function. \ No newline at end of file diff --git a/exercises/practice/collatz-conjecture/test/collatz-conjecture-tests.lfe b/exercises/practice/collatz-conjecture/test/collatz-conjecture-tests.lfe new file mode 100644 index 00000000..732b5050 --- /dev/null +++ b/exercises/practice/collatz-conjecture/test/collatz-conjecture-tests.lfe @@ -0,0 +1,23 @@ +(defmodule collatz-conjecture-tests + (behaviour ltest-unit) + (export all)) + +(include-lib "ltest/include/ltest-macros.lfe") + +(deftest zero-steps-for-one + (is-equal (collatz-conjecture:steps 1) 0)) + +(deftest divide-if-even + (is-equal (collatz-conjecture:steps 16) 4)) + +(deftest even-and-odd-steps + (is-equal (collatz-conjecture:steps 12) 9)) + +(deftest large-number-of-even-and-odd-steps + (is-equal (collatz-conjecture:steps 1000000) 152)) + +(deftest zero-is-an-error + (is-error _ (collatz-conjecture:steps 0))) + +(deftest negative-value-is-an-error + (is-error _ (collatz-conjecture:steps -1)))