# Thread: Simple pre-algebra help needed

1. ## Simple pre-algebra help needed

Okay, this will be pretty elementary, I'm sure. But for some reason, I am having trouble. I am trying to write a program and I am using the following formula:

Offset = Y * 160 + X * 2

This formula works great for what I'm doing. My question is, what is the formula I use to retrieve X and Y, assuming I know Offset. For example, if Offset = 1500, what is the value of X and Y?

If it helps, what this is is X and Y coordinates for video memory. Video memory is broken down into 80x25 characters and each character takes 2 spaces.....So if I want to write to Y=10 and X=5, using my formula above, the offset to write to would be 1610.

So my question is, given 1610 as an Offset, what formula do I use to make Y=10 and X=5?

Hope this makes sense and thanks in advance

Alan

2. Originally Posted by icemanind
Okay, this will be pretty elementary, I'm sure. But for some reason, I am having trouble. I am trying to write a program and I am using the following formula:

Offset = Y * 160 + X * 2

This formula works great for what I'm doing. My question is, what is the formula I use to retrieve X and Y, assuming I know Offset. For example, if Offset = 1500, what is the value of X and Y?

If it helps, what this is is X and Y coordinates for video memory. Video memory is broken down into 80x25 characters and each character takes 2 spaces.....So if I want to write to Y=10 and X=5, using my formula above, the offset to write to would be 1610.

So my question is, given 1610 as an Offset, what formula do I use to make Y=10 and X=5?

Hope this makes sense and thanks in advance

Alan
So you're asking us to solve an equation with 2 variables? Unfortunately it's not possible to solve for 2 unknowns unless you have 2 equations.

However, if X and Y are restricted to integer values then your equation is a linear diophantine equation (ax+by=c), and these can be solved using the Euclidean Algorithm if c is the greatest common divisor of a and b (or indeed a multiple of the greatest common divisor!) Do you know the algorithm?

3. Originally Posted by Mush
So you're asking us to solve an equation with 2 variables?

Well, if X and Y are restricted to integer values then your equation is a linear diophantine equation, and these can be solved using the Euclidean Algorithm. Do you know the algorithm?

X and Y are both integer. And no, I don't know that algorithm. Please explain it!

4. Originally Posted by icemanind
X and Y are both integer. And no, I don't know that algorithm. Please explain it!
Extended Euclidean algorithm - Wikipedia, the free encyclopedia

Knock yourself out

5. Originally Posted by Mush

I'm sorry man. The answer to this cannot possibly be this complex. Please let me explain this a different way. Imagine you have a piece of paper. And this piece of paper has 160 squares across and 25 rows of these squares going down. Now lets say you write your letters kind of fat and to write one letter on the piece of paper, it will occupy 2 squares. That means you can have 80x25 letters on this paper. Now lets say we give this paper axises. X goes across and Y goes down. Now lets say I want you to draw a letter at coordinate 10,5. But instead of counting 10 squares across and 5 down, just count each individual square. Remember that 1 letter = 2 squares. So you would count to 1610 and make your letter (cause our formula is Offset(1610)=Y * 160 + X * 5).

Now what I'm trying to do is opposite. What if I said, make a letter at offet 1610. Would you count 1610 spaces? the 1610th space is 10 boxes over and 5 down. Does this make sense?

6. Originally Posted by icemanind
Okay, this will be pretty elementary, I'm sure. But for some reason, I am having trouble. I am trying to write a program and I am using the following formula:

Offset = Y * 160 + X * 2

This formula works great for what I'm doing. My question is, what is the formula I use to retrieve X and Y, assuming I know Offset. For example, if Offset = 1500, what is the value of X and Y?

If it helps, what this is is X and Y coordinates for video memory. Video memory is broken down into 80x25 characters and each character takes 2 spaces.....So if I want to write to Y=10 and X=5, using my formula above, the offset to write to would be 1610.

So my question is, given 1610 as an Offset, what formula do I use to make Y=10 and X=5?

Hope this makes sense and thanks in advance

Alan
I'm afraid that the answer given by Mush appears to be how your question needs to be answered.

You could always write (or find) a computer program to implement this algorithm.

7. It might be interesting to note, OP, that even if there was a simpler method for finding X and Y in your equation, that there is not one unique solution. There are in fact INFINITELY many solutions. You may use the regular euclidian algorithm to find one solution.

$ax+by = c$

If you use the regular algorithm, and get a solution(x,y). Then there are other solutions of the form $(x+\frac{kb}{gcd(a,b)},y+\frac{ka}{gcd(a,b)})$ where k is a member of the integers. There are in fact infinitely many integers, and hence there are infinitely many solutions so such an equation.

Hence, even if there was a simpler method, it might take you infinitely many recursions until you got to your desired answer. In your example: (y=10,x=5). Although I do realise that your grid limits the solutions such that the integers are confined to your grid, so perhaps it's not such an impractical endeavour.

If you would like further explanation on how to solve you problem using the regular euclidian algorithm, then just ask.

8. Quite frankly, the simplest way to write a program to find integers X and Y such that Offset = Y * 160 + X * 2 for a given "offset" is first to write X= (Offset- 160Y)/2 and then step through integer values for Y until you find one that gives X integer also.
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 2, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}