Skip navigation.
Home

An example of Computational Mathematics

Recently someone I was talking with was confused about the purpose of computational mathematics.  After all, aren't all mathematics based on computation?  Well, the easy answer for that is no - whether it is predicate calculus or dealing with infinity.

A purpose of computational mathematics though, is teaching a computing device how to figure out an equation.

Lets look at a simple problem, what minimum of each coin do I need to make change for a given amount under a dollar?  In US currency, it would be described as (in cents):

C = 25q + 10d + 5n + 1p

Where:

  • C is the desired amount of change;
  • q details the number of quarters;
  • d details the number of dimes;
  • n details the number of nickels;
  • and p detains the number of pennies.

Why would this be an interesting question?  Have you ever managed a cash drawer?  How about you are in charge of cash drawers for a retail location with 10 or 20 registers?  How many of each coin will you need inventoried?  One can look at sales transactions and get a pretty good idea of how many coins were needed for each transaction in change - that is, how many coins were depleted from your inventory of change for your customers.  Perhaps you have an automatic coin dispensor at the register to aid in making change?


As one can see, it is difficult to calculate this based on basic algebra manipulations because while one might be able to solve for one variable, there will be three others left behind.  For example, perhaps we want the minimum coins needed for 23, so we get:

23 = 25q + 10d + 5n + 1p

How can we solve for these variables?

Here is a pretty simple and straight forward program to achieve that:

// A problem to help explain what computational mathematics is.
// Determine the minimum number of each coin needed to make change for an amount.
// This is a C++ program.
// Scott Auge sauge@amduus.com


#include <iostream>

int main (int argc, char * const argv[]) {
    
    int AmountOfChange;
    int CoinDenomination;
    int AmountOfCoins;
    int CheckChange;
    int TestAmount;
    int StartingChange;
    
    std::cout << "Making Change For Amount With Minimum Number Of Coins\n";
    std::cout <<  "Scott Auge sauge@amduus.com\n";
    
    // Amount we need to consider from the command line
    
    sscanf(argv[1], "%d", &AmountOfChange);
    
    // Make sure alg handles all change.  CheckChange should be StartingChange at end of program.
    
    CheckChange = 0;
    StartingChange = AmountOfChange;
    
    // Handle Quarters
    
    CoinDenomination = 25;
    AmountOfCoins = 0;
    
    while (true) {

        TestAmount = AmountOfChange - ((AmountOfCoins + 1) * CoinDenomination);

        if (TestAmount < 0 ) {
           
            std::cout << "Amount of quarters: " << AmountOfCoins << "\n";
            break;
           
        } // if
       
        AmountOfCoins++;
       
    } // while
    
    AmountOfChange -= (AmountOfCoins * CoinDenomination);
    CheckChange += (AmountOfCoins * CoinDenomination);
    
    // Handle Dimes
    CoinDenomination = 10;
    AmountOfCoins = 0;
    
    
    while (true) {
       
        TestAmount = AmountOfChange - ((AmountOfCoins + 1) * CoinDenomination);
       
        if (TestAmount < 0 ) {
           
            std::cout << "Amount of dimes: " << AmountOfCoins << "\n";
            break;
           
        } // if
       
        AmountOfCoins++;
       
    } // while
         
    AmountOfChange -= (AmountOfCoins * CoinDenomination);
    CheckChange += (AmountOfCoins * CoinDenomination);
    
    // Handle Nickels
    CoinDenomination = 5;
    AmountOfCoins = 0;
    
    
    while (true) {
       
        TestAmount = AmountOfChange - ((AmountOfCoins + 1) * CoinDenomination);
       
        if (TestAmount < 0 ) {
           
            std::cout << "Amount of nickels: " << AmountOfCoins << "\n";
            break;
           
        } // if
       
        AmountOfCoins++;

       
    } // while
          
    AmountOfChange -= (AmountOfCoins * CoinDenomination);
    CheckChange += (AmountOfCoins * CoinDenomination);
    
    // Handle Pennies
    CoinDenomination = 1;
    AmountOfCoins = 0;
    
    
    while (true) {
       
        TestAmount = AmountOfChange - ((AmountOfCoins + 1) * CoinDenomination);
       
        if (TestAmount < 0 ) {
           
            std::cout << "Amount of pennies: " << AmountOfCoins << "\n";            
            break;
           
        } // if
       
        AmountOfCoins++;

       
    } // while
    
    AmountOfChange -= (AmountOfCoins * CoinDenomination);
    CheckChange += (AmountOfCoins * CoinDenomination);    
          
    std::cout << "Change Check: " << CheckChange << " Amount of Change: " << StartingChange << "\n";
    
    return 0;
    
} // main

Of course there is an opportunity to simplify the coding, but the point is to make it plain.  The program finishes off as command line program called CompMath which acccepts an argument detailing the amount of change to make.  It will then return the amount of change in terms of quarters, dimes, nickels, and pennies needed.

Lets put it through it's paces shall we:

TD321-12:release scott_auge$ ./CompMath 13
Making Change For Amount With Minimum Number Of Coins
Scott Auge sauge@amduus.com
Amount of quarters: 0
Amount of dimes: 1
Amount of nickels: 0
Amount of pennies: 3
Change Check: 13 Amount of Change: 13

Another might be:

TD321-12:release scott_auge$ ./CompMath 6
Making Change For Amount With Minimum Number Of Coins
Scott Auge sauge@amduus.com
Amount of quarters: 0
Amount of dimes: 0
Amount of nickels: 1
Amount of pennies: 1
Change Check: 6 Amount of Change: 6

Or perhaps something a little bigger:

TD321-12:release scott_auge$ ./CompMath 45
Making Change For All Amounts With Minimum Number Of Coins
Scott Auge sauge@amduus.com
Amount of quarters: 1
Amount of dimes: 2
Amount of nickels: 0
Amount of pennies: 0
Change Check: 45 Amount of Change: 45

So this is a little and simple problem where computational mathematics comes into play.