0

Given a number A that increases after a number B has cycled through a range of values (for example, 1 to 4, like below), I'd like to calculate a number C that increases when value B is half way through the cycle.

How can I calculate this?

enter image description here

  • don't understand. Do you have $a,b$ and need to calculate $c$? or do you have the row number from $0$ to $7$, or from $1$ to $8$, and need to calculate $c$? Also, is the length of cycle known (e.g. is it always $4$)? – gt6989b Feb 18 '20 at 20:39
  • $c=2(a-1)+\left\lceil\frac b2\right\rceil$ works, I believe. – saulspatz Feb 18 '20 at 20:44
  • @gt6989b correct, I have A and B and want to calculate C. The length of the cycle can vary. In the actual problem I'm trying to solve, it's 1-128, I've just reduced it to 4 here. – Patrick Hund Feb 18 '20 at 20:46
  • @saulspatz result works for the 4-cycle, what would your data look like in a 5- or 8-cycle? – gt6989b Feb 18 '20 at 20:48
  • @saulspatz please forgive my ignorance, I'm a programmer, not a mathematician – what is the name of the “half square brackets” in your formula around b/2? Ultimately, I'll try to write a Python function to calculate this – Patrick Hund Feb 18 '20 at 20:49
  • 1
    It's the ceiling function. In python is math.ceil – saulspatz Feb 18 '20 at 20:50
  • @saulspatz your solution works fine for a sequence 1-4, but how can I generalize it for an arbitrary number range? My actual range is 1-128 – Patrick Hund Feb 18 '20 at 21:05
  • Ah, I got it, the 2 at the beginning remains constant, while the 2 at the end is half the range, so for 1-128, it's 64 – Patrick Hund Feb 18 '20 at 21:11
  • Great, thank you! – Patrick Hund Feb 18 '20 at 21:12

1 Answers1

1

If $M$ is the cycle and $M$ is an even number than there is an $K$ so that $M = 2K$.

By division algorithm for any integer $n$ we have $n = qM + r$ where $r$ is the remainder after dividing by $M$ and $n = pK + s$ where $s$ is the remainder after dividing by $K$.

Your sequences are $A_n = q+1$ and $B_n = r$ if $r\ne 0$ and $M$ if $r=0$ and $C_n = p + 1$.

Or you can write it as $A_n = \lfloor \frac nM \rfloor + 1$ and $B_n =\lfloor \frac {2n}M\rfloor$+1. (This will actually work if $M$ is odd.)

fleablood
  • 124,253