PEG Test - Halloween 2014
Alex and Ben have found a peculiar street in their neighborhood this Halloween night.
On this street, houses on the left side of the street are numbered with
consecutive prime numbers starting from , and houses on the right side
of the street are numbered with consecutive composite numbers starting
from
. Each side of the street has
houses. The index of a house
is the position of a house from the start of the street. For example,
house
is index
on the left side of the street, and house
is index
on the right side of the street. What's even more peculiar, is that
each house will always give the amount of candy equal to its house
number.
Index: 1 2 3 4 5 6 7 8 9
Left House #: 2 3 5 7 11 13 17 19 23
Right House #: 4 6 8 9 10 12 14 15 16
Alex will trick or treat on the left side, and Ben will trick or treat
from the right side. They will begin trick-or-treating at a house with
the same index, and can cover a range of houses. Alex and Ben want
their final yield of candy to be as close as possible. However, because
Alex has run into some trouble with the Brap Lesh Mafia, he must pay the
Brap Lesh Mafia
units of candy immediately after trick-or-treating.
Determine the index of the house that Alex and Ben should start at such
that, after Alex pays units of candy, they finish with the minimum
difference in candy.
Input Specification
There will be one line of input containing two integers and
.
Output Specification
Output a single integer, the index of the house that Alex and Ben should start at such that they finish with the minimum difference in candy amount. If multiple solutions exist, print the smallest one.
Sample Input 1
0 5
Sample Output 1
3
Explanation 1
Alex does not owe any candy in this case. Starting from house at index
, Alex and Ben will both collect
units of candy. This is the optimal
solution.
Sample Input 2
5 2
Sample Output 2
6
Explanation 2
Starting at index , Alex will collect
pieces of candy, and
Ben will collect
pieces of candy. After paying off his
debt, Alex will have only
less piece of candy than Ben. This is the
optimal solution.
Comments