Project Euler #29: Distinct powers

  • + 0 comments

    Nice challenge. For those who are struggling like I did. I counted the maximum Exponent number and then checked if b can be combined by any number below this max exponent. For example 8 = 2^3 so 8^b can be combined by 2^1*c or 2^2*d.

    int findMaxExponent(int num) {
        int root = sqrt(num);
        if(root == 1) return 1;
        
        for(int i = 2; i <= root; i++) {
            int cached_num = num;
            int expo_count = 1;
            while(cached_num % i == 0) {
                expo_count++;
                cached_num /= i;
                if(cached_num == i) {
                    return expo_count;
                }
            }
        }
        return 1;
    }