{"record":{"id":"d56f04e1a0e3aabf","repo":"TheAlgorithms/JavaScript","slug":"a-is-not-coprime-of-26","errorCode":null,"errorMessage":"${a} is not coprime of 26","messagePattern":"(.+?) is not coprime of 26","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Ciphers/AffineCipher.js","lineNumber":49,"sourceCode":"\n/**\n * Argument validation\n * @param {String} str - String to be checked\n * @param {Number} a - A coefficient to be checked\n * @param {Number} b - B coefficient to be checked\n * @return {Boolean} Result of the checking\n */\nfunction isCorrectFormat(str, a, b) {\n  if (typeof a !== 'number' || typeof b !== 'number') {\n    throw new TypeError('Coefficient a, b should be number')\n  }\n\n  if (typeof str !== 'string') {\n    throw new TypeError('Argument str should be String')\n  }\n\n  if (!CoPrimeCheck(a, 26)) {\n    throw new Error(a + ' is not coprime of 26')\n  }\n\n  return true\n}\n\n/**\n * Find character index based on ASCII order\n * @param {String} char - Character index to be found\n * @return {Boolean} Character index\n */\nfunction findCharIndex(char) {\n  return char.toUpperCase().charCodeAt(0) - 'A'.charCodeAt(0)\n}\n\n/**\n * Encrypt a Affine Cipher\n * @param {String} str - String to be encrypted\n * @param {Number} a - A coefficient","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Ciphers/AffineCipher.js#L31-L67","documentation":"Thrown by isCorrectFormat() when multiplier a is not coprime with 26 (the alphabet size). Affine decryption requires the modular inverse of a mod 26 to exist; that inverse exists only when gcd(a, 26) === 1, i.e. a shares no factor 2 or 13 with 26. Non-coprime a makes decryption ambiguous/impossible.","triggerScenarios":"Passing a = 2, 4, 6, 8, 10, 12, 13, 14, ... any even number or any multiple of 13. Valid values are 1,3,5,7,9,11,15,17,19,21,23,25.","commonSituations":"Picking an arbitrary key, using a = 0 (degenerate), or auto-generating a without the coprimality constraint.","solutions":["Use one of the valid multipliers: 1,3,5,7,9,11,15,17,19,21,23,25.","When generating keys, loop until gcd(a, 26) === 1.","Validate coprimality at the call site before encrypt."],"exampleFix":"// before\nencrypt(text, 2, 8) // 2 shares factor with 26\n// after\nencrypt(text, 5, 8) // 5 coprime with 26","handlingStrategy":"validation","validationCode":"function gcd(x, y) { return y === 0 ? x : gcd(y, x % y); }\nconst validAffineA = [1,3,5,7,9,11,15,17,19,21,23,25];\nfunction isCoprime26(a) { return Number.isInteger(a) && gcd(((a % 26) + 26) % 26, 26) === 1; }","typeGuard":"/** @param {unknown} a @returns {boolean} */\nconst isValidAffineMultiplier = a =>\n  Number.isInteger(a) && [1,3,5,7,9,11,15,17,19,21,23,25].includes(((a % 26) + 26) % 26);","tryCatchPattern":"try { return encrypt(text, a, b); }\ncatch (e) {\n  if (e instanceof Error && /not coprime of 26/.test(e.message)) {\n    return encrypt(text, 5, b); // fall back to a known-good multiplier\n  }\n  throw e;\n}","preventionTips":["Choose a from the valid set {1,3,5,7,9,11,15,17,19,21,23,25}.","When generating keys, loop until gcd(a, 26) === 1.","Never use even numbers or multiples of 13 as the multiplier."],"tags":["cipher","affine","math","coprime","modular-inverse"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}