Given a positive integer n
, return the smallest positive integer that is a multiple of both n
and 2.
n
?n
?n
.n
and 2.To find the smallest multiple of both n
and 2:
n
is already even, then n
itself is already a multiple of both n
and 2.n
is odd, then multiplying n
by 2 will make it even.Thus, the strategy is simple:
n
is even. If so, return n
.n
is odd, return 2 * n
.def smallest_even_multiple(n: int) -> int:
if n % 2 == 0:
return n
else:
return 2 * n
This solution should be efficient even for very large values of n
.
Let’s test the function with a few examples:
# Test cases
print(smallest_even_multiple(5)) # Expected output: 10 (since 5 is odd, 5 * 2 = 10)
print(smallest_even_multiple(6)) # Expected output: 6 (since 6 is even)
print(smallest_even_multiple(1)) # Expected output: 2 (since 1 is odd, 1 * 2 = 2)
print(smallest_even_multiple(8)) # Expected output: 8 (since 8 is even)
This should cover the basic functionality of the solution. Feel free to provide additional test cases or any other specific requirements!
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?