[Python] A function that finds a number's second largest divisor (besides itself)

Discussão em 'Python' iniciado por Stack, Fevereiro 20, 2025.

  1. Stack

    Stack Membro Participativo

    I am looking for how to modify a function to find the second largest divisor and not the largest as it is outputting for me:

    n = int(input("Enter an integer:"))
    result = 1
    for i in range(2, n):
    if ((n % i) == 0):
    result = i
    print(result)


    Enter an integer:36
    18


    Using an example input of 36 the output will be 18. I'm wanting to find the next largest divisor instead, which would be 12.

    How would I go about modifying my function to make the output 12 instead of 18 (assuming the input is 36)?

    Continue reading...

Compartilhe esta Página