PHP code example of mistralys / subsetsum

1. Go to this page and download the library: Download mistralys/subsetsum library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mistralys / subsetsum example snippets


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

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

$matches = $sub->getMatches();

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

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

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

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

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

$sub->makeInteger();

array(3, 5, 7, 10)