How to find relation between 2 numbers

12.7k Views Asked by At

I have been practicing programming for many months now and what I found difficult is not about solving problem. But it is how to find the "how to solve problem" to make computer solves that for me!

Example: If I want to make a program that draw a pyramid of 'X' letter in command prompt.

In the first line I need to draw 1 'X'

The 2nd line I need to draw 3 'X'

The 3rd line I need to draw 5 'X'

The 4th line I need to draw 7 'X' and so on...

So I only have the line 1,2,3,4 and I want 1,3,5,7. I must find the relationship between them (function that do something with the input and return the output). I randomly think and know that the output is just the input*2-1. That was an easy task to find what function it is. But I cannot explain how I got that relationship.

Don't get me wrong, This is about maths not programming. When I get a harder relationship such as 57 to 1, 56 to 2, 55 to 3, 54 to 0, 53 to 1, 52 to 2, 51 to 3, ... and so on. I cannot figure what is the function needed to compute that 57 into 1 and make 56 into 2. I had to think about it so long before I actually know what the relationship of it is. Finally, I know that the output is just the absolute(input-58) mod 4.

WHAT I WANT TO KNOW from my story above is do you know how to find relationship between 2 number easier that I can explain it to my friends so that they understand how I got it. Is there any subject I can learn to improve my relationship guessing ability? If you don't understand please tell me because I'm not a good explainer.

2

There are 2 best solutions below

0
On BEST ANSWER

Well, one start is slope: given two pairs of numbers -- like, say, $(1,1)$ and $(2,3)$, you can find the value

$$m = \frac{y_2-y_1}{x_2-x_1} = \frac{3-1}{2-1} = 2$$

THen you can find a starting point using one of the points, using the point-slope formula $y-y_1 = m(x-x_1)$ and solve that for $y$:

$$y-3=2(x-2)$$ $$y=2x-4+3=2x-1$$

This of course doesn't work for everything. Sometimes the actual values are fuzzy; sometimes the sequence isn't linear. Further research: Finding the next number in a sequence has several procedures for exact but non-linear sequences; Regression analysis is statistical techniques for handling fuzzier sequences; the Online Encyclopedia of Integer Sequences for interesting and hard-to-guess sequences of numbers.

0
On

Getting the relationship by inspection is fine. (There are assumptions with the relationship you've found, but they're reasonable.)

You can take the difference of consecutive values and note it's constant: $2$. This suggests that your formula is $a_n = nc_1 + c_2$, where $c_1$ is your constant difference and $c_2 = 1-2 = -1.$