# Math/Riddle Question (If ya know Visual Basic, read this :D)

**URL:** <https://boards.straightdope.com/t/math-riddle-question-if-ya-know-visual-basic-read-this-d/330284>\
**Category:** Factual Questions\
**Created:** [November 9, 2005, 9:34pm UTC](https://boards.straightdope.com/t/math-riddle-question-if-ya-know-visual-basic-read-this-d/330284 "2005-11-09T21:34:09Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Surbey](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Surbey](https://boards.straightdope.com/u/Surbey)\
**Post date:** [November 9, 2005, 9:34pm UTC](https://boards.straightdope.com/t/math-riddle-question-if-ya-know-visual-basic-read-this-d/330284/1 "2005-11-09T21:34:09Z")

</div>

I have a riddle that I’ve been asked, and I can’t it out. Here it is

A man writes a check for x dollars and y cents. He then hands it to the dyslexic teller who gives him y dollars and x cents back. He then goes to a gas station, purchases an item for 3.50. He looks at whats left and sees that it is exactly double the amount that he wrote on the check ( x dollars and y cents)

How much was the original check?  
If anyone knows Visual Basic, could I solve this with some sort of loop?

---

<div class="post-metadata">

**Author:** ![ftg](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ftg/32/2801_2.png) [@ftg](https://boards.straightdope.com/u/ftg)\
**Post date:** [November 9, 2005, 9:38pm UTC](https://boards.straightdope.com/t/math-riddle-question-if-ya-know-visual-basic-read-this-d/330284/2 "2005-11-09T21:38:27Z")

</div>

This is a great _homework_ problem for an Algebra class.

You therefore use _Algebra_ to solve it. Programming would be completely idiotic.

---

<div class="post-metadata">

**Author:** ![Giles](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/giles/32/60_2.png) [@Giles](https://boards.straightdope.com/u/Giles)\
**Post date:** [November 9, 2005, 9:40pm UTC](https://boards.straightdope.com/t/math-riddle-question-if-ya-know-visual-basic-read-this-d/330284/3 "2005-11-09T21:40:07Z")

</div>

$32.14

---

<div class="post-metadata">

**Author:** ![Gangster\_Octopus](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@Gangster\_Octopus](https://boards.straightdope.com/u/Gangster_Octopus)\
**Post date:** [November 9, 2005, 9:42pm UTC](https://boards.straightdope.com/t/math-riddle-question-if-ya-know-visual-basic-read-this-d/330284/4 "2005-11-09T21:42:25Z")

</div>

Isn’t it just a formula:

100y+x - 350 = 200x+2y

or

y = 199x/98 + 350/98

with presumably multiple solutions?

---

<div class="post-metadata">

**Author:** ![Giles](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/giles/32/60_2.png) [@Giles](https://boards.straightdope.com/u/Giles)\
**Post date:** [November 9, 2005, 9:44pm UTC](https://boards.straightdope.com/t/math-riddle-question-if-ya-know-visual-basic-read-this-d/330284/5 "2005-11-09T21:44:44Z")

</div>

Incidentally, the problem leads to a Diophantine equation with two unknown variables. Ordinary algebra leads you part of the way, but doesn’t solve it. I basically used brute force in an Excel spreadsheet: 99 rows, with every possibe value of one variable, together with a formula saying what the other variable must be, and choosing the one which had an integer value. However, there are more sophisticated ways of solving these equations.

---

<div class="post-metadata">

**Author:** ![Gangster\_Octopus](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@Gangster\_Octopus](https://boards.straightdope.com/u/Gangster_Octopus)\
**Post date:** [November 9, 2005, 9:45pm UTC](https://boards.straightdope.com/t/math-riddle-question-if-ya-know-visual-basic-read-this-d/330284/6 "2005-11-09T21:45:57Z")

</div>

ok, I forgot the assumed caveat that the must be whole numbers and that (I assume) y must be less than 100.

---

<div class="post-metadata">

**Author:** ![bouv](https://avatars.discourse-cdn.com/v4/letter/b/bc8723/32.png) [@bouv](https://boards.straightdope.com/u/bouv)\
**Post date:** [November 9, 2005, 9:47pm UTC](https://boards.straightdope.com/t/math-riddle-question-if-ya-know-visual-basic-read-this-d/330284/7 "2005-11-09T21:47:41Z")

</div>

**Giles** , could you tell me how you got your answer? We have two unknowns, yet only one equation relating the two, so how is it solveable with just one answer?

---

<div class="post-metadata">

**Author:** ![bouv](https://avatars.discourse-cdn.com/v4/letter/b/bc8723/32.png) [@bouv](https://boards.straightdope.com/u/bouv)\
**Post date:** [November 9, 2005, 9:49pm UTC](https://boards.straightdope.com/t/math-riddle-question-if-ya-know-visual-basic-read-this-d/330284/8 "2005-11-09T21:49:44Z")

</div>

> [@bouv](#):
>
> **Giles** , could you tell me how you got your answer? We have two unknowns, yet only one equation relating the two, so how is it solveable with just one answer?

:smack:

Preview next time…

---

<div class="post-metadata">

**Author:** ![Surbey](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Surbey](https://boards.straightdope.com/u/Surbey)\
**Post date:** [November 10, 2005, 5:38am UTC](https://boards.straightdope.com/t/math-riddle-question-if-ya-know-visual-basic-read-this-d/330284/9 "2005-11-10T05:38:16Z")

</div>

However, when I plug your answer into the riddle, it doesn’t come out right.

---

<div class="post-metadata">

**Author:** ![Surbey](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Surbey](https://boards.straightdope.com/u/Surbey)\
**Post date:** [November 10, 2005, 5:40am UTC](https://boards.straightdope.com/t/math-riddle-question-if-ya-know-visual-basic-read-this-d/330284/10 "2005-11-10T05:40:01Z")

</div>

nvm, I musta did something wrong. Works just fine 😃 Thankyou

---

<div class="post-metadata">

**Author:** ![Antonius\_Block](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@Antonius\_Block](https://boards.straightdope.com/u/Antonius_Block)\
**Post date:** [November 10, 2005, 8:09am UTC](https://boards.straightdope.com/t/math-riddle-question-if-ya-know-visual-basic-read-this-d/330284/11 "2005-11-10T08:09:17Z")

</div>

Actually, I think the answer that \*\*Giles[/] gives in post #3 is the amount the cashier _wrongly gave him_ in the first step, not the amount on the check, which I’m pretty sure would have been $14.32.

To answer the OP, it can be solved by a really simple Visual Basic program: just set a loop counter to step x from 0 to 99, and check for integer solutions less than 100 (same as the Excel method, but slightly less code to write).

---

<div class="post-metadata">

**Author:** ![CookingWithGas](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/cookingwithgas/32/485_2.png) [@CookingWithGas](https://boards.straightdope.com/u/CookingWithGas)\
**Post date:** [November 10, 2005, 5:28pm UTC](https://boards.straightdope.com/t/math-riddle-question-if-ya-know-visual-basic-read-this-d/330284/12 "2005-11-10T17:28:51Z")

</div>

> [@ftg](#):
>
> You…use _Algebra_ to solve it. Programming would be completely idiotic.

I’m still looking for **ftg** ’s solution. :dubious:

---

<div class="post-metadata">

**Author:** ![Cabbage](https://avatars.discourse-cdn.com/v4/letter/c/f07891/32.png) [@Cabbage](https://boards.straightdope.com/u/Cabbage)\
**Post date:** [November 10, 2005, 7:41pm UTC](https://boards.straightdope.com/t/math-riddle-question-if-ya-know-visual-basic-read-this-d/330284/13 "2005-11-10T19:41:11Z")

</div>

Here’s how you solve it algebraically:

I think it’s already been shown that x,y must satisfy -199x + 98y = 350. Since x,y are integers, this is a linear Diophantine equation.

First, we need to find the greatest common divisor (GCD) of the coefficients of x and y. The GCD of -199 and 98 is 1. In order for our equation to have a solution, this GCD must divide the constant, 350, which of course it does, so we’re in luck.

Next, write the GCD as a linear combination of -199 and 98. It doesn’t matter how you do it, just find a way. It can be done systematically using the Euclidean algorithm, if you like.

Anyway, I got

-33(-199) - 67(98) = 1.

Multiply both sides of this by 350:

-11550(-199) - 23450(98) = 350

So x=-11550, y=-23450 is a solution to our equation. Of course, it obviously doesn’t make any sense as an answer to the original question, so we’ve still got work to do.

Let’s call the above solution x’, y’ (= -11550, -23450, respectively). There are others, and we’re gonna have to find them.

If I change x’ to, say, x’ + a, then y’ is gonna have to change to if we want to keep the equation balanced, call it y’ + b:

-199(x’ + a) + 98(y’ + b) = 350

A little algebra shows:

199a = 98b

Since 199 and 98 are relatively prime, this means 98 divides a, so let’s write a as 98t:

19502t = 98b

Solve for b, and you get

b = 199t.

t, of course, can be any integer.

So our general solution is:

x = -11550 + 98t, y = -23450 + 199t (t any integer).

Both x and y have to be between 0 and 100 for our answer to make any sense. If you look at the equation for y, you’ll see this can only happen for one value of t, t=118. This gives x=14, y=32, so the check was originally written for $14.32.

---

<div class="post-metadata">

**Author:** ![ftg](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ftg/32/2801_2.png) [@ftg](https://boards.straightdope.com/u/ftg)\
**Post date:** [November 10, 2005, 11:39pm UTC](https://boards.straightdope.com/t/math-riddle-question-if-ya-know-visual-basic-read-this-d/330284/14 "2005-11-10T23:39:06Z")

</div>

> [@CookingWithGas](#):
>
> I’m still looking for **ftg** ’s solution. :dubious:

And I’m still waiting for the day that people will catch on that answering other people’s _homework problems_ is a Bad Thing.

---

<div class="post-metadata">

**Author:** ![Tevildo](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Tevildo](https://boards.straightdope.com/u/Tevildo)\
**Post date:** [November 11, 2005, 1:36am UTC](https://boards.straightdope.com/t/math-riddle-question-if-ya-know-visual-basic-read-this-d/330284/15 "2005-11-11T01:36:25Z")

</div>

I realise that we shouldn’t use this forum to compensate for our not listening in high school, but my lifelong antipathy towards mathematics might be ameilorated a little if I were to understand two of the steps in this solution:

> [@Cabbage](#):
>
> Next, write the GCD as a linear combination of -199 and 98. It doesn’t matter how you do it, just find a way. It can be done systematically using the Euclidean algorithm, if you like.
> 
> Anyway, I got
> 
> -33(-199) - 67(98) = 1.

What does “linear combination” mean here? Why -33 and -67, rather than any of the other (presumably infintely many) numbers that could be in this equation?

> [@](#):
>
> 199a = 98b
> 
> Since 199 and 98 are relatively prime, this means 98 divides a, so let’s write a as 98t:

What does “relatively prime” mean, and _why_ does this fact mean that 98 divides a? Could the original problem be solved if this wasn’t the case?

Please feel free to point me at a more appropriate site, if this sort of question is too elementary. 🙂

---

<div class="post-metadata">

**Author:** ![Cabbage](https://avatars.discourse-cdn.com/v4/letter/c/f07891/32.png) [@Cabbage](https://boards.straightdope.com/u/Cabbage)\
**Post date:** [November 11, 2005, 2:18am UTC](https://boards.straightdope.com/t/math-riddle-question-if-ya-know-visual-basic-read-this-d/330284/16 "2005-11-11T02:18:31Z")

</div>

> [@Tevildo](#):
>
> I realise that we shouldn’t use this forum to compensate for our not listening in high school, but my lifelong antipathy towards mathematics might be ameilorated a little if I were to understand two of the steps in this solution:
> 
> What does “linear combination” mean here? Why -33 and -67, rather than any of the other (presumably infintely many) numbers that could be in this equation?

A linear combination of -199 and 98 means “an integer times -199 plus an integer times 98”, i.e., -199x + 98y, where x and y are any integers.

An interesting thing about the GCD of two integers: It is the smallest positive number that can be written as a linear combination of the two integers. That’s how I know -199x + 98y = 1 has a solution with x and y both integers.

To find it, we can use the Euclidean algorithm. The Euclidean algorithm is a method for finding the GCD of two numbers. Here’s how it works for 199 and 98: First divide the smaller into the larger, and get a remainder:

199 = 98 \* 2 + 3

Now do the same thing with 98 and 3 (98 was the number we just divided by, 3 was the remainder): Divide 98 by 3:

98 = 3 \* 32 + 2

Again, with 3 and 2 (like before, 3 was the number we just divided by this time, and 2 was our remainder):

3 = 2 \* 1 + 1

We just divided by 2, and 1 was our remainder, so that’s what I use next: 2 divided by 1:

2 = 1 \* 2 + 0

Eventually you’ll get zero as a remainder (like we just did). The last nonzero remainder we had is the GCD of 199 and 98. This was 1, so we just showed 1 is their GCD.

Now work backwards to write 1 as a linear combination of 199 and 98. From the next to last equation:

1 = 3 - 2

From the equation before that:

1 = 3 - (98 - 3_32) = -98 + 33_3

And from the equation before that:

1 = -98 + 33\*(199 - 98_2) = 33_199 - 67\*98

So -199x +98y = 1 has a solution x=-33, y=-67. This particular solution isn’t really special, it just happens to be the one I found. All we need is _any_ linear combination of -199 and 98 which equals 1.

> [@](#):
>
> What does “relatively prime” mean, and _why_ does this fact mean that 98 divides a? Could the original problem be solved if this wasn’t the case?
> 
> Please feel free to point me at a more appropriate site, if this sort of question is too elementary. 🙂

We had 199a = 98b. “Relatively prime” just means their GCD is 1, like we showed above. It means they have no prime factors in common.

98 factors into 2_7_7. 199 factors into 199 (it is prime, itself).

The prime factorization of an integer is unique. Since 199a = 98b, there’s got to be a prime factor of 199 in 98b. It obviously isn’t in 98, so it’s gotta be in b (so 199 divides b).

Similarly, 199a must have prime factors 2, 7, and another 7. They obviously ain’t in 199, so they gotta be in a. This means 2_7_7 = 98 divides a.

If we had gotten to that stage of the problem and didn’t have relatively prime numbers, we could still go on. For example, if we had instead ended up with:

30a = 12b

30 and 12 obviously aren’t relatively prime. Their GCD is 6. Divide both sides by 6:

5a=2b.

Once you’ve done that, from here it’s just like before. 5 and 2 are relatively prime, so we can say, for example, 5 divides b. Write b=5t. Then of course, a=2t.
