/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
 * Name: COS 126 Staff                                                       *
 * Login: cos126                                                             *
 * Precept: P00                                                              *
 *                                                                           *
 * Description: Calculates the period of an aliquot sequence.                *
 *                                                                           *
 * Dependencies: Perfect.java                                                *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

public class Cuddly {

    // maximum number of steps to use when searching for periods
    private static final int MAX = 30;
    
    // calculates the period of an aliquot sequence
    public static int aPeriod(int n) {
        int current = n;
        
        for (int count = 1; count <= MAX; count++) {
            current = Perfect.aliquot(current);
            if (current == n) return count;
            if (current == 0) return -1;
        }
        
        // if we haven't found n or 0 after MAX steps, return -1
        return -1;    
    }
    
    // returns true if a number is sociable (aliquot cycle > 2)
    public static boolean isSociable(int n) {
        return aPeriod(n) > 2;
    }
    
    // returns true if a number is cuddly (perfect, amicable, or sociable)
    public static boolean isCuddly(int n) {
        return aPeriod(n) != -1;
    }
}