# How to simplify 2 modular operators?

Is it possible to simplify the following expression? I feel like this can be simplified.

$$N = (7 - ((3X - C)\mod 7))\mod 7$$

$$A = \left\lfloor\frac{X + O}{7}\right\rfloor$$

I'll explain a bit for sake of completeness:

The formula is used to get key signature value, where:

• $0\leq N \lt7$ starting from note C to B

• $-7\leq X\leq 7$ number of sharps or flats. Negative value means flats and positive means sharps.

• $C = 0$ for major scales, $C = 5$ for minor scales.

• $-1\leq A\leq 1$ negative value indicates flat key, positive indicates sharp key.

• $O = 1$ for major scales, $O = 4$ for minor scales.

I think It is possible to somehow simplify $\mod7 \mod7$ part. the formula I wrote is experimental but it works fine for defined ranges that I tested.

Example:

X = 4; // 4 sharps
C = 5; // minor scale
O = 4; // minor scale


$N = (7 - ((3\times 4 - 5)\mod 7))\mod 7\implies N = 0$ , therefore Note C

$A = \left\lfloor\frac{4+4}{7}\right\rfloor\implies A = 1 , A > 0$, therefore is sharp.

So key signature is C# minor

• your example is wrong, you have X=4 – JMP May 13 '17 at 21:59
• thanks for correction. yes c# minor has 4 sharps. 3 was typo @JonMarkPerry – M.kazem Akhgary May 14 '17 at 2:04

The first $7$ is redundant, you can use:
$$C-3X \pmod 7$$
• some mod implementations do this automatically; in maths we write: $$-3\equiv 4 \pmod 7$$, you can also use, if (retValue<0) retvalue+=7; but I'm glad you've worked it out. – JMP May 14 '17 at 5:33