{"id":24540673,"url":"https://github.com/mistralys/subsetsum","last_synced_at":"2025-06-23T12:34:32.498Z","repository":{"id":57017614,"uuid":"298001255","full_name":"Mistralys/subsetsum","owner":"Mistralys","description":"PHP SubsetSum implementation","archived":false,"fork":false,"pushed_at":"2021-10-31T09:31:17.000Z","size":40,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-09T05:43:49.397Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Mistralys.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-09-23T14:44:33.000Z","updated_at":"2021-10-31T09:29:30.000Z","dependencies_parsed_at":"2022-08-22T11:31:41.562Z","dependency_job_id":null,"html_url":"https://github.com/Mistralys/subsetsum","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/Mistralys/subsetsum","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mistralys%2Fsubsetsum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mistralys%2Fsubsetsum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mistralys%2Fsubsetsum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mistralys%2Fsubsetsum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Mistralys","download_url":"https://codeload.github.com/Mistralys/subsetsum/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mistralys%2Fsubsetsum/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261479739,"owners_count":23164756,"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":[],"created_at":"2025-01-22T18:14:38.726Z","updated_at":"2025-06-23T12:34:27.478Z","avatar_url":"https://github.com/Mistralys.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.com/Mistralys/subsetsum.svg?branch=master)](https://travis-ci.com/Mistralys/subsetsum)\n\n# PHP SubsetSum implementation\n\nGiven a target number and a list of numbers, determines which number combinations equal the target number.\n\nFor example: With `25` as the target number, and `10`, `5`, `15` as the numbers list, this will determine that `25 = 10 + 15`.\n\n## Requirements\n\n- PHP \u003e= 7.1\n- BCMath extension\n\n## Installation\n\nRequire the package via composer on the command line:\n\n```\ncomposer require mistralys/subsetsum\n```\n\nOr edit composer.json directly:\n\n```\n\"require\": \n{\n    \"mistralys/subsetsum\": \"dev-master\"\n}\n```\n\n## Usage\n\nThe `create` method is used to create a new instance, which can be used to retrieve matches, or to configure options:\n\n```php\n$sub = SubsetSum::create(25, array(5,10,7,3,20));\n```\n\n### Checking if there are any matches\n\nSome methods like `getShortestMatch()` can return null, so it's best to check if there are matches beforehand.\n\n```php\nif(!$sub-\u003ehasMatches())\n{\n    echo 'No matches.';\n}\n```\n\n### Getting all matches\n\nTo retrieve all matching number combinations:\n\n```php\n$matches = $sub-\u003egetMatches();\n```\n\nThis will return an array like this:\n\n```php\narray(\n    array(3, 5, 7, 10),\n    array(5, 20)\n)\n```\n\nNOTE: The numbers in each match result are always sorted in ascending order.\n\n### Getting the shortest match\n\nThe shortest match is the one that uses the least amount of number combinations.\n\n```php\n// check if there are matches, since the method can return null.\nif($sub-\u003ehasMatches())\n{\n\t$match = $sub-\u003egetShortestMatch();\n}\n```\n\nIn the example, this would return the following match array:\n\n```\narray(5, 20)\n```\n\n### Getting the longest match\n\nThe longest match is the one that uses the highest amount of number combinations.\n\n```php\n// check if there are matches, since the method can return null.\nif($sub-\u003ehasMatches())\n{\n\t$match = $sub-\u003egetLongestMatch();\n}\n```\n\nIn the example, this would return the following match array:\n\n```\narray(3, 5, 7, 10)\n```\n\n### Adjusting the amount of decimals \u0026 rounding\n\nBy default, the internal calculations will round the numbers to `2` decimals, using PHP's default \"round up half\" rounding. This can be easily adjusted to your needs:\n\n```php\n// 4 decimals, default rounding mode\n$sub-\u003esetPrecision(4);\n\n// 1 decimal, specific rounding mode\n$sub-\u003esetPrecision(1, PHP_ROUND_HALF_DOWN);\n```\n\nThe full list of possible modes can be found here:\n\nhttp://www.php.net/manual/en/math.constants.php\n\n### Working with integers\n\nWorking in integer mode simply means using a precision of `0`. \n\n```php\n$sub-\u003emakeInteger();\n```\n\nNOTE: The match arrays will contain integers, but which are still typed as floats. You will have to cast them to `int` as needed.\n\n## Performance\n\nA word of caution: calculating subset sums has an exponential complexity the higher the amount of numbers to search through. You can easily bring your server to your knees with larger sets, so I would recommend setting some limits on the amount of numbers in your application.\n\n## Credits\n\nThe initial mechanism was inspired by this answer on StackOverflow:\n\nhttp://stackoverflow.com/questions/4632322/finding-all-possible-combinations-of-numbers-to-reach-a-given-sum/#answer-43351998\n\nThere is also another interesting package that goes further than this:\n\nhttps://github.com/pipan/subsetsum-php\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmistralys%2Fsubsetsum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmistralys%2Fsubsetsum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmistralys%2Fsubsetsum/lists"}