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

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. All Contests
  2. ProjectEuler+
  3. Project Euler #30: Digit Nth powers
  4. Discussions

Project Euler #30: Digit Nth powers

Problem
Submissions
Leaderboard
Discussions

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

  • leduckhai
    2 years ago+ 0 comments

    You can still use brute-force. Just do some experiments to determine lower and upper limit for i, then you will pass all cases.

    def Digit_Nth_Powers(n,lower,upper):
      sumList = 0
    
      for i in range(lower,upper):
        digits = list(map(int,str(i)))
        tempSum = 0
    
        for j in digits:
          tempSum += j**n
        
        if i == tempSum:
          sumList += i
            
      return sumList
    
    if __name__ == '__main__':
      n = int(input())
      
      # Lower and upper limit is determined by experiments for faster code execution
      if n == 3:
        lower,upper = [1*(10**2), 1*(10**3)]
      elif n == 4:
        lower,upper = [1*(10**3), 1*(10**4)]  
      elif n == 5:
        lower,upper = [2*(10**3), 2*(10**5)]
      elif n == 6:
        lower,upper = [1*(10**5), 6*(10**5)]
        
      sumList = Digit_Nth_Powers(n,lower,upper)
      print(sumList)
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy