Perfect numbers is printing for each dividen...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • XxChelseaxX
    New Member
    • Jan 2010
    • 4

    Perfect numbers is printing for each dividen...

    Code:
    # count the divisors of a number
    def countDivisors(n):
        count = 0
        for possible in range(1, n):
            if n % possible == 0:
                count = count+possible
    
            # print Perfect if sum of divisors it equal to dividen
            if count == n:
                 print n
    any ideas on how to get this to only print once I am really confused.
    Last edited by bvdet; Jan 28 '10, 01:08 AM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Please use code tags when posting code. Se out posting guidelines.

    BV - Moderator

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Your problem is the indentation.

      Code:
      # count the divisors of a number
      def countDivisors(n):
          count = 0
          for possible in range(1, n):
              if n % possible == 0:
                  count += possible
       
          # print Perfect if sum of divisors it equal to dividen
          if count == n:
              print n
      
              
      countDivisors(496)
      Output:
      Code:
      >>> 496
      >>>

      Comment

      • XxChelseaxX
        New Member
        • Jan 2010
        • 4

        #4
        That didnt work

        I trided that and it doesnt seem to work.

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          What error do you receive when you run the code? Are you running it at the command prompt or in an IDE?

          Comment

          • XxChelseaxX
            New Member
            • Jan 2010
            • 4

            #6
            Its not an error its just prints the number for each number it runs. So for exaple, 6 is a perfect number for 1,and 2 it prints nothing because its not perfect for 3, 4, and 5 It prints 6 because 4 and 5 dont devide in its still perfect. I want it to only print when it has the total count not each one, because after I fix this, I will make a loop and it should tell me all the perfect numbers from 1-1000.

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              Your post led me to believe that you wanted to check if a number is perfect. If you are looking for perfect numbers below some limit, I would approach it a different way, but still brute force.

              Function A returns a list of proper factors for a given number
              Code:
              def factors(num):
                  .......................
              Function B returns a list of perfect numbers below an upper limit
              Code:
              def proper_nums(upper):
                  output = []
                  for i in range(2, upper+1):
                      if sum(factors(i)) == i:
                          output.append(i)
                  return output
              
              print proper_nums(100000)
              The output should be:
              Code:
              >>> [6, 28, 496, 8128]
              To calculate the proper factors of a given number, you need only loop through the square root of the number.
              Code:
              for i in xrange(1, int(num**0.5)+1)

              Comment

              • XxChelseaxX
                New Member
                • Jan 2010
                • 4

                #8
                Apparently I should not have taken this class. I have no idea what you just said. This is not like math, it is way harder and apparently my brain is not ment for this.I'll just drop the class LOL

                Comment

                Working...