• egg

coin-change

Description

Coin change problem solver with finite number of coins, using a greedy algorithm.

siiky

Repository

https://git.sr.ht/~siiky/coin-change

Requirements

The following eggs are required for using this egg:

The following eggs are required for testing this egg:

API

[procedure] (coin-change target denominations) --> (values solution memo)
target
The value that the solution must add up to.
denominations
The list of available denominations, and respective counts (i.e., upper closed limit).

Returns two values: the solution (or #f), and the memoization table.

Solves the coin change problem, taking into account a given finite limit of coins of each denomination, using a "dumb" greedy algorithm. This doesn't necessarily result in the minimum number of coins for non-canonical sets of denominations, but is in practice not far from it, and is simple to implement. Uses a couple of simple checks to fail early in certain scenarios, and a memoization table, from target to denomination to solution (this works because of the greedy nature). The recursive level will be at most the number of denominations.

Mathematically speaking, given target T, denominations di, and counts ci, it computes the coefficients si of the following linear equation, such that 0 <= si <= ci: T = (sum (* si di))

```(coin-change 765 '((50 . 123) (10 . 456) (5 . 789)))
;=> ((50 . 15) (10 . 1) (5 . 1))
;   ((5 (5)) (15 (10) (5 . 1)) (765 (50) (10 . 1) (5 . 1)))```

This is NOT the solutions counting algorithm!

[procedure] (coin-change/unlimited target denominations) --> (values solution memo)
target
The value that the solution must add up to.
denominations
The list of available denominations.

Convenience wrapper around coin-change that, given a list of denominations, computes the maximum number of coins of each denomination.

```This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Version History

1.0.0 (2023/11/18)

• Initial implementation

1.0.1 (2023/11/18)

• Fix tests report formatting