PDA

View Full Version : riddle



ifym
02-21-2003, 07:09 AM
got this riddle at work, looking for a vbscript to solve it.....
you have to buy 100 women with $100.
girls are $5 each
brides are $2 each
old ladies are 10 for 1$

i wrote a simple script that figured it out but it required my input, i want one thta will figure it out all by itself. this is my script....

do until numb = 100
girls = inputbox ("girls")
brides= inputbox("brides")
old=inputbox("old ladies")
Dtotgirls=girls*5
Dbrides = brides*2
Dold=old * .10
numb= dtotgirls + dbrides + dold
wscript.echo numb
loop

it saved me some time, took about 20 minutes to figure it out using this. BUT i know there would be a easy way to write a script to just figure out every posibility and when it does it returns it to you. I have the idea for it, but i cant seem to get it to work on paper. anybody up for the challenge of writing it? doesnt seem to hard im just new to scripting.

Andy-S
02-21-2003, 07:33 AM
I'm not very good at scripting but this may help with your thought process:

The equation you are trying to solve is:

100 = 5x + 2y + 0.1z

Where x = girl, y = bride, z = ladies

You should run through a loop like this:

For x = 1 to 20

For y = 1 to 50

For z = 10 to 1000

If 5x + 2y + 0.1z = 100 then print out x, y and z and quit
x=x+1
y=y+1
z=z+10


So it should start out with x=1 and then y=1 followed by Z=10 to 1000.

Then x=1, y=2 and z=10 to 1000 etc.

edit:

You have to check that x + y + z = 100 also.

Cheers
Andy<P ID="edit"><FONT class="small"><EM>Edited by Andy-S on 02/21/03 07:34.</EM></FONT></P>

jdharm
02-21-2003, 03:13 PM
Is there even a solution? In you original post, the way you were doing it you did indeed get a price of $100 after 20 minutes, but did your total of women = 100? There are a couple dozen ways to get a total of $100, but as far as I could tell there was no way that any of these solutions involved exactly 100 women.

Since our total cost must be $100 and our total women must be 100 I used the following equation:

Total price = Total Women = 100

I got no solution for every combination, up to 100 of each.

When I leave off the 100 and just have the two part equality:

total price = total women (i.e. generalization of 100 women for 100 dollars)

I get only one solution: 1 girl + 5 brides + 10 old ladies = 16 women for $16.

This is the vbscript I used (the blue I left off to get the 16 for $16 solution):

dim x
dim y
dim z
for x = 1 to 100
for y = 1 to 100
for z = 10 to 100
If 5*x + 2*y + 0.1*z = x + Y + Z <font color=blue>= 100 </font color=blue>then Result = MsgBox(x & " girls at $5 each = $" & 5*x & vbNewLine & Y & " brides at $2 each = $" & 2*y & vbNewLine & z & " old ladies at 10 for $1 = $" & .1*z & vbNewLine & "Total = $" & 5*x + 2*y + 0.1*z, 0, "Buy, Buy, Buy")
z=z+10
Next
y=y+1
Next
x=x+1
Next


So what is the solution?


DISCLAIMER: By participating in this discussion I am in no way saying that I feel the buying and selling of women is acceptable or moral. And even if it was, I am in no way stating that these prices are fair and equitable. I'm pretty sure I would pay more than $2 for bride material. Probably.


Josh

The proceeding was from the "For What It's Worth" files, which by definition may be worthless. Take with a grain or ten of salt.

Andy-S
02-21-2003, 04:29 PM
Josh,

Try again with 1000 for the old ladies. If you only have 100 then the cost of buying 100 old ladies is $10. It may be that you need more old ladies.

Cheers
Andy

ifym
02-23-2003, 03:33 PM
yes there is a real solution, with 100 ladies and $100. the answer is............................(stop reading if u dont want to know)

by the way, your right the script didnt cehck to see if there was 100 women total but i just added it up in my head while i was doing it


11 girls, 19 brides, and 70 old ladies.
11*5=55
19*2=38
70*.1=07
------------
100 = 100

Marc_J
02-24-2003, 12:12 AM
Andy,

Even though they are cheap, you can't buy more than 100 old ladies as this will exceed the maximum total number of women you are allowed to buy according to the riddle.

I haven't programmed for years and I know nothing of vbscript or any other languages for that matter. But the equation is simple enough, even good old QBasic was able to handle it.

Hear is my BASIC program:

CLS

FOR a = 1 TO 20
FOR b = 1 TO 50
FOR c = 10 TO 100 STEP 10

IF (5 * a) + (2 * b) + (c / 10) = 100 AND a + b + c = 100 THEN PRINT a; b; c

NEXT c
NEXT b
NEXT a

END

ifym
02-24-2003, 05:50 AM
marc_j i havent used qbasic in about 8 years so maybe im wrong, but that dont look like it would work does it? it looks like it would try 1/1/10, then 2/2/20/, then 3/3/30/, etc until it gets to 20/50/100. If it does do that, then it will never get to try 11/19/70 because a and b will always be the same. Maybe im wrong, thats just how it looks to me.

Marc_J
02-24-2003, 07:31 AM
Nope.. look at the program a little closer. The sequence of tries would be 1/1/10, 1/1/20, 1/1/30 and so on.

This can be confirmed by printing a; b; c on every cycle resulting in:
1 1 10
1 1 20
1 1 30
1 1 40
1 1 50
1 1 60
1 1 70
1 1 80
1 1 90
1 1 100
1 2 10
1 2 20.... etc

Also - I didn't want to post something that may not work so I made sure I ran it before posting and it returned the correct result.

ifym
02-24-2003, 07:48 AM
your right, like i said i havent used qbasic in a while and even then i was far from a pro. Im guessing when it see's next C it goes back to c, until there all done in wich case it'll go to next B until there all done, etc etc. Thanks. I would still like to see how it oculd be done with vbscript, i dont think it would be that easy though.

Andy-S
02-24-2003, 09:09 AM
I would think that it would be quite simple to do now that all the groundwork has been done.

Unfortunately I don't have a clue about scripting but I'm sure one of the regulars will have a good idea at how to solve it.

Cheers
Andy

jdharm
02-25-2003, 09:51 AM
I'm an idiot. Mark_J's post got me to thinking that I should check the step by step output of the script to see what was going on. I then saw I had the x=x+1 and others in the script when I didn't need it. The FOR/NEXT loop took care of this.

I cleaned up the script. This one gets the solution, and the If statement does check to make sure the total money is $100 and the total women is 100. Copy the following in a notepad session and save as *.vbs

dim x
dim y
dim z
for x = 1 to 100
for y = 1 to 100
for z = 10 to 100 step 10
If 5*x + 2*y + 0.1*z = 100 and x + y + z = 100 then Result = MsgBox(x & " girls at $5 each = $" & 5*x & vbNewLine & Y & " brides at $2 each = $" & 2*y & vbNewLine & z & " old ladies at 10 for $1 = $" & .1*z & vbNewLine & "Total = $" & 5*x + 2*y + 0.1*z, 0, "Buy, Buy, Buy")
Next
Next
Next



Josh

The proceeding was from the "For What It's Worth" files, which by definition may be worthless. Take with a grain or ten of salt.

Andy-S
02-25-2003, 10:15 AM
Great job Josh. Works perfect.

Cheers
Andy

jdharm
02-25-2003, 10:30 AM
Whew! Finally. This is the type of thing that gets under my skin and really bugs me till I figure it out, kinda like a piece of popcorn in my teeth.

Josh

The proceeding was from the "For What It's Worth" files, which by definition may be worthless. Take with a grain or ten of salt.

ifym
02-25-2003, 06:42 PM
wow you are the man thanks

just a note in case anybody wants to see how it works out, put a wscript.echo x, y, z right before the first next statement. warning your gonna have to end task unless you want to hit enter a thousand tims<P ID="edit"><FONT class="small">Edited by ifym on 02/25/03 18:51.</FONT></P>

Marc_J
02-26-2003, 12:13 AM
Well done that man!

I was wondering though guys (since I stumbled across this post by pure chance), being as I haven't programmed in years I though it might be worth learning vbscript. Any knowledge of a "teach yourself" site online?

jdharm
02-26-2003, 10:32 AM
There are a ton for free on the net, including Microsoft's own library: <a target="_blank" href=http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vtorivbscriptfundamentals.asp>CLICK HERE</a>

But truth be told, I only know what I know (which is very little, BTW) by using what I remember from using BASIC on a Commodore64 in high school, the VBScript reference guide here at Winguides.com, and the snippets posted by the various members here in the forums. I suffer from a deplorable lack of ambition which keeps me from sitting down with a course or a text book and learning properly.

Josh

The proceeding was from the "For What It's Worth" files, which by definition may be worthless. Take with a grain or ten of salt.