[Python] Using recursion to determine a semiperfect number

Discussão em 'Python' iniciado por Stack, Dezembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    I am in need of your help again. I'm writing a code that determines whether a number is a semiperfect number or not by returning a boolean value. So first I thought I would make a list of the factors of the number excluding the number itself

    def isSemiPerfect(n):
    factor = []
    for i in range(n-1, 0, -1):
    if n%i == 0:
    factor.append(i)


    If I were to check for the number 12, it would return

    factor = [1, 2, 3, 4, 6]


    Then I need to make a code to use recursion to check if you add certain numbers it would be equal to 12

    True = 6 + 4 + 2 or 6 + 3 + 2 + 1


    Can someone tell me how I can use recursion to do trial and error? Like I'll always start with the biggest number and try a path with the next biggest number until I've tried all combinations.

    I know there isn't a lot to go on with I just hope that you can tell me how I can use recursion effectively. Thank you!

    Continue reading...

Compartilhe esta Página