We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. All Contests
  2. ProjectEuler+
  3. Project Euler #29: Distinct powers
  4. Discussions

Project Euler #29: Distinct powers

Problem
Submissions
Leaderboard
Discussions

    You are viewing a single comment's thread. Return to all comments →

  • christian_koehl1
    4 months ago+ 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;
    }
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy