Python List filled with duplicates
Here is the code.
x=0
result=[]
for n in range(1,5):
x=x+n;
for i in range(1,10):
if x%i==0:
result.append(i)
print(x,result)
Here I have generated triangular numbers.I want to find the divisors of
each triangular number.but when I execute the code I am getting the
following output.
1 [1]
3 [1, 1]
3 [1, 1, 3]
6 [1, 1, 3, 1]
6 [1, 1, 3, 1, 2]
6 [1, 1, 3, 1, 2, 3]
6 [1, 1, 3, 1, 2, 3, 6]
10 [1, 1, 3, 1, 2, 3, 6, 1]
10 [1, 1, 3, 1, 2, 3, 6, 1, 2]
10 [1, 1, 3, 1, 2, 3, 6, 1, 2, 5]
Also same triangular number is repeated for several times.So I need an
output looks like,
1 [1]
3 [1, 3]
6 [1, 2, 3, 6]
10 [1, 2, 5]
How can I get the output like this?thank you.
No comments:
Post a Comment