Problem overview
The function parity receives a non-negative integer n and returns the string "even" or "odd". A number is even when it is divisible by 2 and odd otherwise. The example is n = 12: 12 divides by 2 with nothing left over, so the function returns "even". For 13 it returns "odd".
Approach
Dividing a number by 2 either leaves no remainder or leaves a remainder of 1. Go's remainder operator % gives that remainder directly: 12 % 2 is 0 and 13 % 2 is 1. So the whole problem is one test, n % 2 == 0. If it is true the number is even; if not, it is odd.
The check takes constant time and needs no loop or extra data.
Step by step
- Write the
ifwith the remainder test. The condition isn % 2 == 0. Two different operators are at work:%computes the remainder, and==compares that remainder with 0. Leave the body empty for the moment. - Return
"even"inside the block. The function's result type isstring, so the value goes in double quotes. - Return
"odd"after theif. This line is only reached when the condition was false. An even number has already left the function through the firstreturn, so noelseis needed.
The solution in Go
func parity(n int) string {
if n % 2 == 0 {
return "even"
}
return "odd"
}
Go details to notice:
- Early return replaces else. After a
returninside theif, the code below runs only for the other case. Writing the finalreturn "odd"without anelseis the common Go style, and adding anelsearound it would behave the same. - The function must end with a return. Since the result type is
string, the compiler requires every path to return. The last line guarantees that. Without it you would see amissing returnerror. - Operator precedence.
%binds tighter than==, son % 2 == 0means(n % 2) == 0with no parentheses needed. Go's formatter would print it asn%2 == 0; both spellings are the same program. - Strings use double quotes.
"even"is a string. Single quotes would make a rune, a different type.
Another way to see it
An integer is even exactly when its lowest binary digit is 0, so n&1 == 0 gives the same answers as n % 2 == 0, including for negative numbers: for 12, 0, -4 it is true, and for 13, 1, -3 it is false. The remainder form is the one to learn first because it says what you mean, divisibility by 2, in words anyone can read. The same % pattern also answers "is this divisible by 3?" or "by 10?" by changing the number.
Edge cases and common mistakes
- Zero.
0 % 2is 0, soparity(0)returns"even", which is correct since 0 is divisible by 2. - One.
1 % 2is 1, so the result is"odd". - Testing for oddness with
n % 2 == 1. For the non-negative numbers in this lesson it works. In Go, though, the sign of%follows the dividend, so-3 % 2is-1, not 1, and a test against 1 would call -3 even. Comparing against 0 is correct for every integer:parity(-3)returns"odd"because-1is not0. - Using
/instead of%./is integer division and gives the quotient:12 / 2is 6. It says nothing about divisibility. - Using
=instead of==.=assigns, and Go refuses an assignment as a condition. - Returning the wrong type. Returning
trueor a number won't compile, because the function promises astring.
Testing
parity(12)returns"even".parity(13)returns"odd".parity(0)returns"even".parity(1)returns"odd".parity(-3)returns"odd", even though the lesson only promises non-negative input.