Change Point Algorithm from Randy Gallistel rewritten in R