PHPackages                             mistralys/subsetsum - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. mistralys/subsetsum

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mistralys/subsetsum
===================

PHP SubsetSum implementation to search for number combinations.

1.0.2(4y ago)11.1k↓50%11MITPHPPHP &gt;=7.1

Since Sep 23Pushed 4y ago1 watchersCompare

[ Source](https://github.com/Mistralys/subsetsum)[ Packagist](https://packagist.org/packages/mistralys/subsetsum)[ RSS](/packages/mistralys-subsetsum/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (2)Versions (4)Used By (1)

[![Build Status](https://camo.githubusercontent.com/afc3d707ad41e97b2f9e4c7264c872990604dfd06d8ac79ae50163fc945e9781/68747470733a2f2f7472617669732d63692e636f6d2f4d69737472616c79732f73756273657473756d2e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/Mistralys/subsetsum)

PHP SubsetSum implementation
============================

[](#php-subsetsum-implementation)

Given a target number and a list of numbers, determines which number combinations equal the target number.

For example: With `25` as the target number, and `10`, `5`, `15` as the numbers list, this will determine that `25 = 10 + 15`.

Requirements
------------

[](#requirements)

- PHP &gt;= 7.1
- BCMath extension

Installation
------------

[](#installation)

Require the package via composer on the command line:

```
composer require mistralys/subsetsum

```

Or edit composer.json directly:

```
"require":
{
    "mistralys/subsetsum": "dev-master"
}

```

Usage
-----

[](#usage)

The `create` method is used to create a new instance, which can be used to retrieve matches, or to configure options:

```
$sub = SubsetSum::create(25, array(5,10,7,3,20));
```

### Checking if there are any matches

[](#checking-if-there-are-any-matches)

Some methods like `getShortestMatch()` can return null, so it's best to check if there are matches beforehand.

```
if(!$sub->hasMatches())
{
    echo 'No matches.';
}
```

### Getting all matches

[](#getting-all-matches)

To retrieve all matching number combinations:

```
$matches = $sub->getMatches();
```

This will return an array like this:

```
array(
    array(3, 5, 7, 10),
    array(5, 20)
)
```

NOTE: The numbers in each match result are always sorted in ascending order.

### Getting the shortest match

[](#getting-the-shortest-match)

The shortest match is the one that uses the least amount of number combinations.

```
// check if there are matches, since the method can return null.
if($sub->hasMatches())
{
	$match = $sub->getShortestMatch();
}
```

In the example, this would return the following match array:

```
array(5, 20)

```

### Getting the longest match

[](#getting-the-longest-match)

The longest match is the one that uses the highest amount of number combinations.

```
// check if there are matches, since the method can return null.
if($sub->hasMatches())
{
	$match = $sub->getLongestMatch();
}
```

In the example, this would return the following match array:

```
array(3, 5, 7, 10)

```

### Adjusting the amount of decimals &amp; rounding

[](#adjusting-the-amount-of-decimals--rounding)

By 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:

```
// 4 decimals, default rounding mode
$sub->setPrecision(4);

// 1 decimal, specific rounding mode
$sub->setPrecision(1, PHP_ROUND_HALF_DOWN);
```

The full list of possible modes can be found here:

### Working with integers

[](#working-with-integers)

Working in integer mode simply means using a precision of `0`.

```
$sub->makeInteger();
```

NOTE: The match arrays will contain integers, but which are still typed as floats. You will have to cast them to `int` as needed.

Performance
-----------

[](#performance)

A 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.

Credits
-------

[](#credits)

The initial mechanism was inspired by this answer on StackOverflow:

There is also another interesting package that goes further than this:

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~201 days

Total

3

Last Release

1660d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8895528?v=4)[Mistralys](/maintainers/Mistralys)[@Mistralys](https://github.com/Mistralys)

---

Top Contributors

[![Mistralys](https://avatars.githubusercontent.com/u/8895528?v=4)](https://github.com/Mistralys "Mistralys (22 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/mistralys-subsetsum/health.svg)

```
[![Health](https://phpackages.com/badges/mistralys-subsetsum/health.svg)](https://phpackages.com/packages/mistralys-subsetsum)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
