Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Two sets

x = 1,2,3,4,5...
y = 1,4,7,10,13...

I need to write a function

$f(x_n) = y_n$

I found that if I

  1. take a number from x
  2. double it
  3. subtract 2
  4. add the result to the original number
  5. I get the corresponding number from y

Here's what I have so far (it's in ruby code), it works but is there a better way of doing it.

def f(x)
  if x > 1
    return x + ((2 * x) - 2)
    return 1

y = f(x)
share|cite|improve this question
Not sure what better way you are looking for. What you have seems fine, expect we can multiply by 3 instead of doubling and then adding itself. I guess Ruby does not have overflow issues. – Aryabhata Jan 9 '11 at 1:12
up vote 3 down vote accepted

Your values y form what is called an arithmetic progression, namely a sequence where each element is obtained from the previous one just by adding always the same constant. In your case the constant is 3, namely: 1, 4=1+3, 7=4+3, 10=7+3 and so on.

Since the FIRST time you add 3 corresponds to x=2, the formula is just


or (equivalently)


share|cite|improve this answer
Wow thanks, I can't believe I didn't think about 3x instead of 2x + x lol. – Seth Archer Brown Jan 9 '11 at 3:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.