I have a # which can be 1-20 and im trying to make a variable equal to 1,2, or 3 depending on which number is selected.
So
1 would equal 1
2 would equal 2
3 would equal 3
4 would equal 1
5 would equal 2
6 would equal 3
7 would equal 1
8 would equal 2
9 would equal 3
10 would equal 1
11 would equal 2
12 would equal 3
…
up to 20

so something like

switch ($circuit) {
case 1:
$leg = '1';
break;
case 2:
$leg = '1';
break;
case 3:
$leg = '2';
break;
case 4:
$leg = '2';
break;
case 5:
$leg = '3';
break;
case 6:
$leg = '3';
break;
...
}

would work (but ineficient)
Would this be a better way?

Modulo is “The remainder after division” (it’s a bit more complex than that, but thats the simple way to think about it.)
7 % 3 = 7 / 3 = 2 R 1, so return 1.

oh sorry, right. the inversion point’s backwards. ((x-1) % 3)+1.

((x+a) % b) + c: x is your input variable. a offsets the list b determines the reset point, c manipulates the values.

It’s a bit like moving a parabola, if you did that in your maths classes.

so:
we have an X from 1 to infinity; we have a B that is 3 if we simply did x%3:
1 = 1
2 = 2
3 = 0
4 = 1
5 = 2
6 = 3
…

desired output is 1 2 3 1 2 3. value at the reset point is “1”, which means we have added 1 to all values in the actual list (0+1 1+1 2+1 0+1 1+1 2+1), so factor out the 1. x % 3 +1 gives us 2 3 1 2 3 1. Right numbers, wrong alignment. We need to shift the pattern to the right by 1, so we subtract 1 from the input: (x-1) % 3 + 1 gives us 1 2 3 1 2 3. We now match desired output.

I have a box with a bunch of circuit breakers on it, each circuit breaker is on a circuit (blue numbers). Each circuit breaker would also be assigned a leg (the red numbers which are grouped by 3 (1,2,3,1,2,3,1,2,3…)

Note: This ISNT the math that you asked for, or what you’ve accepted as the answer. What you seem to be indicating in your drawing is a pattern of:

1 1 2 2 3 3 1 1 2 2 3 3

which is slightly different.

More accurately, what you’re trying to say is that if circuit numbers are distributed row-first, what row is a given circuit on, modulo the number of legs, 1-indexed.

function getLeg(int $circuit, int $numberofcolumns = 2, int $numberoflegs = 3) {
if ($numberofcolumns < 1 || $circuit < 1 || $numberoflegs < 1) {
// Ideally I'd do this in three different checks with useful exception messages, but for the sake of bevity...
throw new InvalidArgumentException('All arguments to getLeg() must be strictly positive');
}
return floor(($circuit-1) / $numberofcolumns) % $numberoflegs + 1;
}

Because I was writing code that won’t throw errors? shrug (It doesnt actually prevent any checks, it’s just hiding them in the system, which is fair enough)

woaw, another way (simplier)
Is just to create an array of all the leg values (there will always be 20 circuits)
then just do an array lookup, is that ok?

See, information we didn’t have…
Personally, i’d do it a lazy way ($legs = str_repeat("123",7);), because you can index a string like an array. But then you’d have to convert to int as well during the pull.

I wrote the function in a hurry and should have paid more attention to meaningful variable names.

The logic behind the function was gained from the image. Two important factors were the maximum values of the columns and the row number (legs).

Supposing there were five $columns then the tripping points would be 5, 10, 15, 20.

When the $circuit values are equal to any of the tripping points then the modulus would be zero or
FALSE. Therefore the result $row = $circuit / $rows.

When $modulus is positive then maximum tripping point is calculated from the following:

$max = $circuit + $cols - $mod;

/=====================================================
function getResult
(
int $circuit, // 1..20
int $cols = 3 // MAX RANGE
)
: int // $row 1..3
{
// IS RESULT LESS THAN MAX RANGE
if($mod = $circuit % $cols) :
$max = $circuit + $cols - $mod;
else:
$max = $circuit;
endif;
return $row = $max / $cols;
}//

Edit:

I still cannot understand the logic behind the return value of your function