给定三个连续的偶数。从数学上证明,其中至少一个可以被6整除。
例子:
Input : {2, 4, 6}
Output : 6 is divisible by 6
Input : {8, 10, 12}
Output : 12 is divisible by 6
问题来源:亚马逊面试经验383套(实习校园)
如果您看到任意三个连续数字,则可以找出其中至少一个可以被6整除的数字。
我们可以使用数学归纳法对其进行数学证明。
对于要被6整除的数字,应该将其与2和3整除。
由于所有数字均为偶数,因此该数字可以被2整除。
为了将数字除以3,
考虑以下证明:
Consider 3 consecutive even numbers :
P(i) = {i, i+2, i+4} (i is divisible by 2)
If one of these three numbers is divisible by 3,
then their multiplication must be divisible by 3
Base case : i = 2
{2, 4, 6}
Multiplication = (2*4*6) = 3*(2*4*2)
So, it is divisible by 3
For i = n
P(n) = {n, n+2, n+4}
multiplication = (n*(n+2)*(n+4))
since P(n) is divisible by 3
means P(n) = n*(n+2)*(n+4) = 3k for positive number k
If the statement holds for i = n, it should hold for
next consecutive even number i.e. i = n + 2
P(n+2) = (n+2)*(n+4)*(n+6)
It can be written as
P(n+2) = n*(n+2)*(n+4) + 6*(n+2)*(n+4)
P(n+2) = P(n) + 6*x
where x = (n+2)*(n+4)
So, P(n+2) = 3*k + 6*x
both the summation elements of P(n+2) are
divisible by 3, so P(n+2) is divisible by 3
Hence, there is atleast one number among three
even consecutive numbers which is divisible by 6.