Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Where did my proper divisor sum program went wrong?

Post

Where did my proper divisor sum program went wrong?

+5
−1

Here in Python, I created a program for this challenge and I'm having trouble debugging it. I already fixed most errors I have on my program but here's what I have left:

x=y=z=[];i=0.0;a=int(input());b=0
for c in range(1,a+1):
	for d in range(1,a):
		i+=1
		if a/c==i and c!=a:b+=c
	if b<a:x.append(c)
	elif b>a:y.append(c)
	else:z.append(c)
	i=b=0
print("[",end='')
for j in x:
	print(j,end='')
	if j!=len(x):print(" ",end='')
print("], [",end='')
for j in y:
	print(j,end='')
	if j!=len(y):print(" ",end='')
print("], [",end='')
for j in z:
	print(j,end='')
	if j!=len(z):print(" ",end='')
print("]",end='')

Here's the unshortened code with comments:

x=y=z=[] # Initialize up 3 empty lists
i=0.0 # initialize a float to make sure the numbers would be checked if it's a proper divisor
a=int(input()) # input an integer
b=0 # initialize the value for the proper divisor sum
for c in range(1,a+1): # start a for loop that checks which list should the number in the range of 1 and the input
	for d in range(1,a): # start a for loop to get the proper divisor sum of each number in the range of 1 and the input
		i+=1 # increment 1 for rechecking		
		if a/c==i and c!=a: # check if the number is a proper divisor
			b+=c # if it is, then add it to the sum
	if b<a:
		x.append(c) # add to the list if the number is deficient
	elif b>a:
		y.append(c) # add to the list if the number is abundant
	else:
		z.append(c) # add to the list if the number is perfect
	i=b=0 # reset the numbers for rechecking
print("[", end = '') # start the output by adding a bracket without a newline
for j in x: # print all deficient numbers as a list
	print(j, end = '') # print the number according to the for loop without a newline
	if j != len(x):
		print(" ", end = '') # print a space to separate numbers without a newline except for the last number in the list
print("], [", end = '') # End the list, proceed to the next one
for j in y: # print all abundant numbers as a list
	print(j, end = '') # print the number according to the for loop without a newline
	if j != len(y):
		print(" ", end = '') # print a space to separate numbers without a newline except for the last number in the list
print("], [", end = '') # End the list, proceed to the next one
for j in z: # print all perfect numbers as a list
	print(j, end = '') # print the number according to the for loop without a newline
	if j != len(z):
		print(" ", end = '') # print a space to separate numbers without a newline except for the last number in the list
print("]", end = '') # end it with a bracket

Basically, I'm trying to make a number checker so I can get the inputted number's proper divisor sum, which in my understanding is the sum of divisors that results in a quotient that can be formatted as n.00000..., but the resulting output leaves me with all the numbers from 1 to the input. See it for yourself! (SIFY)

I've been trying to figure out where the problem lies and I suspect it has to do with the first for loop, but I don't know where exactly to fix. Where did I mess up or how do I fix this?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (5 comments)
General comments
FoggyFinder‭ wrote almost 3 years ago

Disclaimer: I don't know Python. There're couple of errors. One of them, most obvious, x=y=z=[] - you actually have 1 reference instead of 3 independent list. You can use copy or assign the new list to each variable. The second error is that fact that your second loop is the same for all iterations. Without looking to task it's complicated to say what are you trying to achieve.

FoggyFinder‭ wrote almost 3 years ago

I can try to help you to understand what went wrong but I think it would be better if you add some comments to the algorithm so I (or anyone else around) can follow your thoughts.

deleted user wrote almost 3 years ago

@Alexei wouldn’t it fit to "Code Reviews" category? I think Code Reviews is for this type of question. Isn't it?

FoggyFinder‭ wrote almost 3 years ago

I think Code Reviews is for this type of question. @Istiak‭ No, Code Review is about improving, receiving feedback in general for code that basically works.

Skipping 5 deleted comments.

Alexei‭ wrote almost 3 years ago

@Istiak Yes, Code Review requires code that works ("does the job"), but requires review (improvements, patterns, performance, readability etc.). Currently, the provided code does not work as expected, so it is a good fit here (on main Q&A).