Take any three consecutive integers: $n - 1$, $n$, and $n + 1$, where $n$ is the middle integer.
If we square each integer and add them together:
$$S = (n - 1)^2 + n^2 + (n + 1)^2$$
$$(n^2 - 2n + 1) + n^2 + (n^2 + 2n + 1) = 3n^2 + 2$$
In the 1970 MAA High School Mathematics Contest (Problem #4), students were asked to determine the divisibility of numbers in this set $S$:
Can $3n^2 + 2$ ever be divisible by 2? By 3? By 5? By 7? By 11?
Using a for-loop and an if-statement with the modulo operator (%), we can test these divisibility properties over a range of numbers!
sum = (n - 1)^2 + n^2 + (n + 1)^2 if sum % 3 == 0 then print("Divisible by 3!") end
Move the mouse over a dotted box for more information.
Notice the algebraic formula: $S = 3n^2 + 2 = 3(n^2) + 2$. When dividing by 3, the remainder is always 2, regardless of what integer $n$ is! This means that no member of $S$ is ever divisible by 3. But what about 11? Let's check with code.
Now you try.
Replace ???? in both if conditions to test divisibility by 3 and 11. Run the code to verify that no sum is divisible by 3, but some (like $n=5$: $4^2 + 5^2 + 6^2 = 77$) are divisible by 11!
Type your code here:
See your results here:
This code will not run yet because the if conditions contain ????. Replace ???? with the proper modulo tests using sum % 3 == 0 and sum % 11 == 0.
Share your code
Show a friend, family member, or teacher what you've done!