We consider optimal group testing of individuals with heterogeneous risks for an infectious disease. Our algorithm significantly reduces the number of tests needed compared to Dorfman (Ann Math Stat 14(4):436-440, 1943). When both low-risk and high-risk samples have sufficiently low infection probabilities, it is optimal to form heterogeneous groups with exactly one high-risk sample per group. Otherwise, it is not optimal to form heterogeneous groups, but homogeneous group testing may still be optimal. For a range of parameters including the U.S. Covid-19 positivity rate for many weeks during the pandemic, the optimal size of a group test is four. We discuss the implications of our results for team design and task assignment.