lcm
Calculate the least common multiple of two numbers to find the smallest positive integer divisible by both values.
Instructions
Calculate the least common multiple of two numbers
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| a | Yes | ||
| b | Yes |
Implementation Reference
- src/index.ts:148-165 (handler)Handler function for the 'lcm' tool that computes the least common multiple using an inline GCD function and returns a formatted text response.async ({ a, b }) => { const gcd = (x: number, y: number): number => { x = Math.abs(x); y = Math.abs(y); while (y !== 0) { const temp = y; y = x % y; x = temp; } return x; }; const lcm = (a: number, b: number): number => (a * b) / gcd(a, b); return { content: [ { type: "text", text: `The least common multiple of ${a} and ${b} is ${lcm(a, b)}` } ] }; }
- src/index.ts:143-147 (schema)Input schema for the 'lcm' tool defining parameters a and b as numbers using Zod validation.{ title: "LCM tool", description: "Calculate the least common multiple of two numbers", inputSchema: { a: z.number(), b: z.number() }, },
- src/index.ts:140-166 (registration)Registration of the 'lcm' MCP tool via server.registerTool, including schema, title, description, and inline handler implementation.// LCM (Least Common Multiple) server.registerTool( "lcm", { title: "LCM tool", description: "Calculate the least common multiple of two numbers", inputSchema: { a: z.number(), b: z.number() }, }, async ({ a, b }) => { const gcd = (x: number, y: number): number => { x = Math.abs(x); y = Math.abs(y); while (y !== 0) { const temp = y; y = x % y; x = temp; } return x; }; const lcm = (a: number, b: number): number => (a * b) / gcd(a, b); return { content: [ { type: "text", text: `The least common multiple of ${a} and ${b} is ${lcm(a, b)}` } ] }; } );
- src/index.ts:149-158 (helper)Inline helper function 'gcd' implementing the Euclidean algorithm for greatest common divisor, used in LCM calculation.const gcd = (x: number, y: number): number => { x = Math.abs(x); y = Math.abs(y); while (y !== 0) { const temp = y; y = x % y; x = temp; } return x; };
- src/index.ts:159-159 (helper)Inline helper function 'lcm' that computes least common multiple using the formula LCM(a,b) = |a*b| / GCD(a,b).const lcm = (a: number, b: number): number => (a * b) / gcd(a, b);