Commit 74a5fef7 authored by Rasmus Villemoes's avatar Rasmus Villemoes Committed by Linus Torvalds
Browse files

lib/lcm.c: ensure correct result whenever it fits

Ensure that lcm(a,b) returns the mathematically correct result, provided
it fits in an unsigned long.  The current version returns garbage if a*b
overflows, even if the final result would fit.
Signed-off-by: default avatarRasmus Villemoes <>
Cc: Martin K. Petersen <>
Signed-off-by: default avatarAndrew Morton <>
Signed-off-by: default avatarLinus Torvalds <>
parent 7b212edf
......@@ -7,7 +7,7 @@
unsigned long lcm(unsigned long a, unsigned long b)
if (a && b)
return (a * b) / gcd(a, b);
return (a / gcd(a, b)) * b;
else if (b)
return b;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment