Write a function in C
int mod(int m, int n)
that takes exactly two positive integers m, n
such that m >= n and,
returns 0 if m is a multiple of n else,
returns 1.
Do not use the inbuilt modulo operator.
Do not use any library functions or floating point operations.
Do not use any additional memory including temporary variables.
Do this in O(1) time complexity.
int mod(int m, int n) { return (n * (m/n) == m); }
m = 3, n = 2; mod(3,2) = 1 // False
m = 4, n = 2; mod(4,2) = 0 // True
S
snalli
@snalli
6
Reputation
109
Posts
329
Profile views
0
Followers
0
Following
Posts made by snalli

implement the modulo operator in O(1) spacetime

implement modulo operator in O(1) spacetime
Write a function in C int mod(int m, int n) that takes exactly two positive integers m, n such that m >= n and, returns 0 if m is a multiple of n else, returns 1. Do not use the inbuilt modulo operator. Do not use any library functions or floating point operations. Do not use any additional memory including temporary variables. Do this in O(1) time complexity. int mod(int m, int n) { return (n * (m/n) == m); } m = 3, n = 2; mod(3,2) = 1 // False m = 4, n = 2; mod(4,2) = 0 // True