Enter each divisor and create a program to output the sum of divisors in the output.
Currently, only the last value entered is printed.
How can I output each input value?
N=int(input())
sum_list = [ ]
num_list = [ ]
for_in range(N):
num=int(input())
divider=[i for i in range(1, num) if num%i==0]
divider_list=list(divisor)
sum_divisor_list=sum(divisor_list)
print(sum_divisor_list)
Purpose
Input value
3
28
16
10
Output
28
15
8
Please let me know.
python python3
The reason why only the values entered later are printed is because num
is rewritten N times in the for statement, and the previous input is erased and overwritten.
If your question is intended to "Find all divisors of a particular number (not prime numbers) and add up the divisors (except for the particular number itself), you can rewrite the code as follows:
Modify Example
Enter N=int(input())#3
for_in range(N):
num = int(input(">")# Enter 28, 16, and 10 in 3 loops, respectively
divider=[i for i in range(1, num//2+1) if num%i==0] #Tips To find the divisor, loop up to half the num
divider_list=list(divisor)
sum_divisor_list=sum(divisor_list)
print(divisor_list)# approximate code
print(sum_divisor_list)# Output Desired
output results
3
>28
[1, 2, 4, 7, 14]
28
>16
[1, 2, 4, 8]
15
>10
[1, 2, 5]
8
The package SymPy has a function called proper_divisors()
.
sympy.ntheory.factor_.proper_divisors
Return all dividers of n except n, sorted by default.
The code in the questionnaire has a list called num_list
, so use it (also sum_list
).
from sympy import prop_divisors
num_list = [ ]
N = int(input())
for_in range(N):
num_list.append(int(input())))
sum_list = [ ]
for num in num_list:
sum_divisors=sum(proper_divisors(num))
print(sum_divisors)
sum_list.append(sum_divisors)
© 2024 OneMinuteCode. All rights reserved.