Change coin types... subtract if available? help!

ok guys,

this one is tricky... put on your nerd caps :-) the nerds here at the

office cant figure it out........ ok, I built a change calculator.. I gave the

change codenames just out of our own reference system here at work.... but the

values go something like this.... diamond = 20 cents ruby=10 cents gold=5cents

and penny=1 cent....

heres the issue... each of our users in our database have a field for diamond,

ruby, gold, penny... now depending on certain actions, they may acquire more of

one than the other, it doesn't balance so that for instance you found two

rubys, you would get a diamond... no, you would have two rubies..

have a change calculator that finds the total amount to subtract.. for

instance.. I want to find out how to seperate out the number 93... that would

be 4 diamonds, 1 ruby, 0 gold and 3 silver... now I want to take that value out

of the users "stash" in the database.... heres the problem I am facing.

What if, the user only has ONE diamond and we need to take out four?

Obviously we have a checksum function built in to make sure we are not

exceeding the total, but for instance... what if the user has 18 rubies, and

only 2 diamonds?

So what I need help with is, what is the best way to most efficiently take out

the total amount from our fields when some of the fields are lacking sufficient

amounts?

Thanks in advance guys, you are always the best!

Re: Change coin types... subtract if available? help!

Hmmmn. Calling me the N-word and you are not (A) my mother or (B) a close

friend.

Also, this question smacks of someone's homework assignment and is not really

CF related.

Anyway, the "best", "most efficient" way to solve this problem is to use a

quantum computer.

Build yourself one of those and you'll be able to solve the change question in

a single operation!

-- Nerd apprentice

Re: Change coin types... subtract if available? help!

What if, the user only has ONE diamond and we need to take out four?

1 - 4 = -3

The run a check to annul anything less than zero.

seperate out the number 93... that would be 4 diamonds, 1 ruby, 0 gold and 3

silver... now

Your problem is poorly defined. You use silver whereas your basis is

(diamond, ruby, gold, penny), which doesn't include silver. Even if we correct

for that, and assume you meant penny , the problem definition remains

poor.

You seem to require unique representations, yet your basis is not unique.

To see that, take (diamond, ruby, gold, penny), respective weights 20, 10, 5,

1.

93 = (4, 1, 0, 3) = (0, 0, 18, 3) = (0, 0, 0, 93) = ... etc.

I would instead use the popular binary system, which is based on the weights

1, 2, 4, 8, 16, 32, ... and so on. Using it, you can represent any positive

integer

in one, and exactly one, way. However, here the weights are represented, in

order of magnitude, from right to left. Every representation consists of just

0s and 1s. There are even well-known techniques for subtraction and for

representing negative numbers without using the negative sign.

Example: 93 = (1,0, 1, 1, 1, 0, 1).

That is 93=1x64+0x32+1x16+1x8+1x4+0x2+1x1

Re: Change coin types... subtract if available? help!

Step 1:

Total all of the users change and convert to one total amount in pennies. (3

diamonds + 10 rubies = 160 pennies)

Step 2:

Convert amount to subtract into pennies also

Step 3:

Verify that user total is greater or equal to amount to subtract.

Step 4:

Subract amount to subtract from user total

Step 5:

P the remainder of the user total back into the various codenames/

diamonds etc.

Re: Change coin types... subtract if available? help!

ok,

just to clear up a few things.. the reason we are going by silver is because

the application is based on a treasure hunt game, so its not based on real

monetary value. .i have assigned a silver coin the value of 1...

hatethisnamegame ... this is as close to the idea that I have gotten, i have

already kind of built something along those lines, however, we want to keep it

where they retain their original stats as much as possible.. think of it kind

of like carrying around a treasure chest to pay for items...

if you were asked to pay for 300 treasure points worth of stuff and you had

850 treasure points, you wouldnt give the person you are buying the 300 TP

worth of stuff all 850 and then have them p it back out.. you would merely

take out the amount you need to pay with and that would be that....

thats what i am going for here. i need to merely take out the amount based on

what the user has available and not rearrange what he has left.

thanks in advance for all help guys, I know we will figure it out!!!

much love.... je

Re: Change coin types... subtract if available? help!

> if you were asked to pay for 300 treasure points worth of stuff and you had

850 treasure

all 850 and

need to pay

Then this algorithm may help:

1) Run a check to annul anything less than zero.

2) Assume the chips are (diamond, ruby, gold, penny) as before, having

respective

weights 20, 10, 5, 1. You wish to find how to decompose an amount A

into change. Then start with the chip of highest weight(diamond) and work

your way downwards.

(i) To obtain the number of diamond chips in the change, divide the

amount A

by 20. Let the result be S1and the remainder R1.

(ii) To obtain the number of ruby chips, divide R1 by 10. Let the result be

S2 and the remainder R2.

(i) To obtain the number of gold chips in the change, divide R2 by 5.

Let the result be S3 and the remainder R3.

Then the decomposition of the amount A into change is

S1 diamond chips

S2 ruby chips

S3 gold chips

R3 penny chips.

To illustrate with your original example of 93,

(i) 93/20 = 4 remainder 13 (S1=4, R1=13)

(ii)13/10 = 1 remainder 3 (S2=1, R2=3)

(iii)3/5 = ) remainder 3 (S3=0, R3=3)

Result:

4 diamond chips

1 ruby chip

0 gold chips

3 penny chips.

Re: Change coin types... subtract if available? help!

First, you need a total function to find out if the user has enough $$ to buy

whatever it is they want to buy:

Total $$ = # diamonds * 20 + # rubies * 10 + # gold * 5 + # pennies

assuming user's Total $$ is greater than the cost of the item:

remaining cost = total cost;

# diamonds = remaining cost / 20;

leftover diamonds = user's diamonds - # diamonds;

if leftover diamonds > 0 {

user's diamonds = leftover diamonds;

remaining cost = (remaining cost mod 20);

} else {

user's diamonds = 0;

remaining cost = (remaining cost mod 20) + (user's diamonds * -2);

}

# rubies = remaining cost / 10;

leftover rubies = user's rubies - # rubies;

if leftover rubies > 0 {

user's rubies = leftover rubies;

remaining cost = (remaining cost mod 10);

} else {

user's rubies = 0;

remaining cost = (remaining cost mod 10) + (user's rubies * -2);

}

# gold = remaining cost / 5;

leftover gold = user's gold - # gold;

if leftover gold > 0 {

user's gold = leftover gold;

remaining cost = (remaining cost mod 5);

} else {

user's gold = 0;

remaining cost = (remaining cost mod 5) + (user's gold * -5);

}

# pennies = remaining cost;

leftover pennies = user's pennies - # pennies;

if leftover pennies > 0 {

user's pennies = leftover pennies;

} else {

// throw error. we didn't have enough $$ to buy this item (that's why we

have the first if test.)

}

Re: Change coin types... subtract if available? help!

Total $$ = # diamonds * 20 + # rubies * 10 + # gold * 5 + # pennies

Shouldn't this be

Total $$ GTE ... etc. ?

Re: Change coin types... subtract if available? help!

Total $$ = # diamonds * 20 + # rubies * 10 + # gold * 5 + # pennies

1) T = 20w + 10x + 5y + z doesn't have a unique solution, which I think is a

requirement.

Otherwise the problem is ill-defined.

2) Shouldn't that be

Total $$ GTE ... etc. ?

Re: Change coin types... subtract if available? help!

Sorry, for clarification (and correction), replace these lines in my post:

Total $$ = # diamonds * 20 + # rubies * 10 + # gold * 5 + # pennies

assuming user's Total $$ is greater than the cost of the item:

with these lines:

Total User $$ = # user diamonds * 20 + # user rubies * 10 + # user gold * 5 +

# user pennies

Assuming total user $$ is greater than or equal to the cost of the item:

The rest of my post still stands.