{"id":13428874,"url":"https://github.com/MarcinMoskala/KotlinDiscreteMathToolkit","last_synced_at":"2025-03-16T02:30:40.025Z","repository":{"id":41351293,"uuid":"87302297","full_name":"MarcinMoskala/KotlinDiscreteMathToolkit","owner":"MarcinMoskala","description":"Set of extensions for Kotlin that provides Discrete math functionalities","archived":false,"fork":false,"pushed_at":"2020-07-02T17:43:15.000Z","size":160,"stargazers_count":185,"open_issues_count":4,"forks_count":17,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-04T21:25:21.632Z","etag":null,"topics":["discrete-math-functionalities","extension-methods","fun","functional-programming","kotlin"],"latest_commit_sha":null,"homepage":"http://marcinmoskala.com/KotlinDiscreteMathToolkit/","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MarcinMoskala.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-04-05T11:37:25.000Z","updated_at":"2024-11-03T01:54:29.000Z","dependencies_parsed_at":"2022-09-26T16:20:35.063Z","dependency_job_id":null,"html_url":"https://github.com/MarcinMoskala/KotlinDiscreteMathToolkit","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarcinMoskala%2FKotlinDiscreteMathToolkit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarcinMoskala%2FKotlinDiscreteMathToolkit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarcinMoskala%2FKotlinDiscreteMathToolkit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarcinMoskala%2FKotlinDiscreteMathToolkit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MarcinMoskala","download_url":"https://codeload.github.com/MarcinMoskala/KotlinDiscreteMathToolkit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243815605,"owners_count":20352195,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["discrete-math-functionalities","extension-methods","fun","functional-programming","kotlin"],"created_at":"2024-07-31T01:01:07.502Z","updated_at":"2025-03-16T02:30:39.693Z","avatar_url":"https://github.com/MarcinMoskala.png","language":"Kotlin","funding_links":[],"categories":["Libraries"],"sub_categories":[],"readme":"# DiscreteMathToolkit\nSet of extensions for Kotlin that provides Discrete Math functionalities as an Kotlin extension functions.\n\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.marcinmoskala/DiscreteMathToolkit/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.marcinmoskala/DiscreteMathToolkit)\n[![Build Status](https://travis-ci.org/MarcinMoskala/KotlinDiscreteMathToolkit.svg?branch=master)](https://travis-ci.org/MarcinMoskala/KotlinDiscreteMathToolkit)\n[![codecov](https://codecov.io/gh/MarcinMoskala/KotlinDiscreteMathToolkit/branch/master/graph/badge.svg)](https://codecov.io/gh/MarcinMoskala/KotlinDiscreteMathToolkit)\n[![codebeat badge](https://codebeat.co/badges/70bb9b0e-a47e-477a-933d-adc7220ae926)](https://codebeat.co/projects/github-com-marcinmoskala-kotlindiscretemathtoolkit-master)\n[![Analytics](https://ga-beacon.appspot.com/UA-92159206-7/main-page?pixel)](https://github.com/MarcinMoskala/KotlinDiscreteMathToolkit)\n\nTo stay current with news about library [![Twitter URL](https://img.shields.io/twitter/url/https/twitter.com/fold_left.svg?style=social\u0026label=Follow%20%40marcinmoskala)](https://twitter.com/marcinmoskala?ref_src=twsrc%5Etfw)\n\n# Permutations\n\n```kotlin\nsetOf(1, 2, 3).permutations() // {[1, 2, 3], [2, 1, 3], [3, 2, 1], [1, 3, 2], [2, 3, 1], [3, 1, 2]}\nsetOf(1, 2, 3).permutationsNumber() // 6\nlistOf(1, 2, 2).permutations() // {[1, 2, 2], [2, 1, 2], [2, 2, 1]}\nlistOf(1, 2, 2).permutationsNumber() // 3\n```\n\nMore examples [here](https://github.com/MarcinMoskala/KotlinDiscreteMathToolkit/blob/master/src/test/java/com/marcinmoskala/math/tests/PermutationTest.kt)\n\n# Combinations\n\n```kotlin\nsetOf(1, 2, 3, 4).combinations(3) // { {1, 2, 3}, {1, 2, 4}, {1, 4, 3}, {4, 2, 3} }\nsetOf(1, 2, 3, 4).combinationNumber(3) // 4\n\nsetOf(1, 2, 3, 4).combinationsWithRepetitions(2) // [{1=2}, {1=1, 2=1}, {1=1, 3=1}, {1=1, 4=1}, {2=2}, {2=1, 3=1}, {2=1, 4=1}, {3=2}, {3=1, 4=1}, {4=2}]\nsetOf(1, 2, 3, 4).combinationsWithRepetitionsNumber(2) // 10\n```\n\nMore examples [here](https://github.com/MarcinMoskala/KotlinDiscreteMathToolkit/blob/master/src/test/java/com/marcinmoskala/math/tests/CombinationTest.kt) and [here](https://github.com/MarcinMoskala/KotlinDiscreteMathToolkit/blob/master/src/test/java/com/marcinmoskala/math/tests/CombinationWithRepetitionTest.kt)\n\n# Powerset\nPowerset of any set S is the set of all subsets of S, including the empty set and S itself.\n\n```kotlin\nsetOf(1, 2, 3).powerset() // { {}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3} }\nsetOf(1, 2, 3).powersetSize() // 8\n```\n# Product\nProduct is the result of multiplying. \n\n```kotlin\n(3..4).product() // 12\nlistOf(10, 10, 10).product() // 1000\n```\n\nMore examples [here](https://github.com/MarcinMoskala/KotlinDiscreteMathToolkit/blob/master/src/test/java/com/marcinmoskala/math/tests/ProductTest.kt).\n\n# Factorial\nFactorian of n (n!) is a product of all positive integers less than or equal to n. \n\n```kotlin\n3.factorial() // 6L\n10.factorial() // 3628800L\n20.factorial() // 2432902008176640000L\n```\nMore examples [here](https://github.com/MarcinMoskala/KotlinDiscreteMathToolkit/blob/master/src/test/java/com/marcinmoskala/math/tests/FactorialTest.kt).\n\n# Numbers divisible and non-divisible by\n\n```kotlin\n(1..1000).countNonDivisiveBy(2) // 500\n(1..1000).countNonDivisiveBy(3) // 777\n(1..1000).countNonDivisiveBy(2, 6, 13) // 462\n(1..1000).countNonDivisiveBy(3, 7, 11) // 520\n\n(1..1000).countDivisiveBy(2) // 500\n(1..1000).countDivisiveBy(3) // 333\n(1..1000).countDivisiveBy(2, 6, 13) // 538\n(1..1000).countDivisiveBy(3, 7, 11) // 480\n```\nMore examples [here](https://github.com/MarcinMoskala/KotlinDiscreteMathToolkit/blob/master/src/test/java/com/marcinmoskala/math/tests/NumbersDivisibleTest.kt).\n\n# Splits of sets and numbers\nIn Descrete Math there are two functions used to count number of splits:\nS(n, k) - Stirling function - number of splits of n different elements to k groups\nP(n, k) - number of splits of n identical elements to k groups\n\n```kotlin\n(1..n).toSet().splitsNumber(1) // 1\n(1..n).toSet().splitsNumber(n) // 1\nsetOf(1, 2, 3).splitsNumber(2) // 3\nsetOf(1, 2, 3, 4).splitsNumber(2) // 7\nsetOf(1, 2, 3, 4, 5).splitsNumber(3) // 25\nsetOf(1, 2, 3, 4, 5, 6, 7).splitsNumber(4) // 350\nsetOf(1, 2, 3).splits(2) // { { {1, 2}, {3} },{ {1, 3}, {2} },{ {3, 2}, {1} } }\n```\n\nMore examples [here](https://github.com/MarcinMoskala/KotlinDiscreteMathToolkit/blob/master/src/test/java/com/marcinmoskala/math/tests/SetSplitTest.kt)\n\n```kotlin\nn.splitsNumber(1) // 1\nn.splitsNumber(n) // 1\n7.splitsNumber(4) // 3\n11.splitsNumber(4) // 11\n9.splitsNumber(5) // 5\n13.splitsNumber(8) // 7\n```\n\nMore examples [here](https://github.com/MarcinMoskala/KotlinDiscreteMathToolkit/blob/master/src/test/java/com/marcinmoskala/math/tests/NumberSplitTest.kt)\n\n# Iterable multiplication\n\nMultiplication of iterables returns iterable with pairs of each possible connections of elements from first and iterable:\n\n```kotlin\nlistOf(1, 2) * listOf(\"A\", \"B\") // returns List\u003cPair\u003cInt, String\u003e\u003e\n// [(1, \"A\"), (1, \"B\"), (2, \"A\"), (2, \"B\")] \nlistOf('a', 'b') * listOf(1, 2) * listOf(\"A\", \"B\") // returns List\u003cTriple\u003cChar, Int, String\u003e\u003e\n// [\n//    ('a', 1, \"A\"), ('a', 1, \"B\"), \n//    ('a', 2, \"A\"), ('a', 2, \"B\"), \n//    ('b', 1, \"A\"), ('b', 1, \"B\"), \n//    ('b', b, \"A\"), ('b', 2, \"B\")\n// ] \n```\n\nMore examples [here](https://github.com/MarcinMoskala/KotlinDiscreteMathToolkit/blob/master/src/test/java/com/marcinmoskala/math/tests/IterableMultipliationTest.kt).\n\n# Cartesian product of lists\n\nSimilar to iterable multiplication but produces sequence of lists:\n\n```kotlin\nlistOf('A', 'B', 'C', D).cartesianProduct(listOf('x', 'y')) // returns List\u003cList\u003cChar\u003e\u003e\n// [\n//     ['A', 'x'],\n//     ['A', 'y'],\n//     ['B', 'x'],\n//     ['B', 'y'],\n//     ['C', 'x'],\n//     ['C', 'y'],\n//     ['D', 'x'],\n//     ['D', 'y']\n// ]\nlistOf(0, 1).cartesianProduct(repeat = 2) // returns List\u003cList\u003cInt\u003e\u003e\n// [\n//     [0, 0],\n//     [0, 1],\n//     [1, 0],\n//     [1, 1]\n// ]\nlistOf(1, 2).cartesianProduct(listOf(\"ABC\")) // returns List\u003cList\u003cAny\u003e\u003e\n// [\n//     [1, \"ABC\"],\n//     [2, \"ABC\"]\n// ]\n```\n\nMore examples [here](https://github.com/MarcinMoskala/KotlinDiscreteMathToolkit/blob/master/src/test/java/com/marcinmoskala/math/tests/CartesianProductTest.kt).\n\n# Java support\n\nLibrary is fully supporting usage from Java. All functions can be used as static function of DiscreteMath. For example:\n\n```java\nDiscreteMath.permutationsNumber(set)\nDiscreteMath.permutationsNumber(list)\nDiscreteMath.factorial(10) // 3628800L\n```\n\nReturned list and sets are Java standard lists and sets. More examples of Java usage [here](https://github.com/MarcinMoskala/KotlinDiscreteMathToolkit/blob/master/src/test/java/com/marcinmoskala/math/tests/JavaTest.java).\n\n# Install\n\nGradle:\n```groovy\ncompile \"com.marcinmoskala:DiscreteMathToolkit:1.0.3\"\n```\n\nMaven:\n```\n\u003cdependency\u003e\n  \u003cgroupId\u003ecom.marcinmoskala\u003c/groupId\u003e\n  \u003cartifactId\u003eDiscreteMathToolkit\u003c/artifactId\u003e\n  \u003cversion\u003e1.0.3\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\nJar to download together with sources and javadoc can be found on [Maven Central](http://search.maven.org/#search%7Cga%7C1%7Cmarcinmoskala).\n\nLicense\n-------\n\n    Copyright 2017 Marcin Moskała\n\n    Licensed under the Apache License, Version 2.0 (the \"License\");\n    you may not use this file except in compliance with the License.\n    You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n    Unless required by applicable law or agreed to in writing, software\n    distributed under the License is distributed on an \"AS IS\" BASIS,\n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n    See the License for the specific language governing permissions and\n    limitations under the License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMarcinMoskala%2FKotlinDiscreteMathToolkit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FMarcinMoskala%2FKotlinDiscreteMathToolkit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMarcinMoskala%2FKotlinDiscreteMathToolkit/lists"}