Consider the sequence 1/2, 1/3 + 1/2, 1/4 + 1/3 + 1/2, and so on. In mathematical symbols, this is


where S is the function that makes the sequence.

Outputting this sequence as a fraction would not be complicated. To add depth to the challenge, you must output as a repeating decimal.

To represent non-repeating decimals, simply output the decimal. To represent repeating decimals, put an R before the repeating part.

For example, 1/7 can be represented by 0.R142857, and 11/9 by 1.R2

Your task is to output the nth number in this sequence, and because this is code-golf the shortest program will win.

  • 1
    \$\begingroup\$ I strongly feel that this is a duplicate of codegolf.stackexchange.com/q/168700/78410 and codegolf.stackexchange.com/q/89215/78410. OP contains a slight variation of both (sum formula is offset by one, and notation for repeating decimal is different), but both changes are minor compared to the core of the tasks. \$\endgroup\$
    – Bubbler
    Jan 11 at 0:27
  • \$\begingroup\$ Need it be simpliest? \$\endgroup\$
    – l4m2
    Jan 11 at 0:45
  • \$\begingroup\$ @I4m2 The decimals do not need to be in simplest form. \$\endgroup\$
    – Jakav
    Jan 11 at 0:47
  • \$\begingroup\$ @Bubbler I'm voting to close this as a duplicate; I think that's the best idea for this question. \$\endgroup\$
    – Jakav
    Jan 11 at 0:50


Browse other questions tagged or ask your own question.