-5

I am a beginner. I am trying to solve a problem where I need to find the perfect numbers within a given range. The problem is ,whenever I declare sum=0 at the top, I get no result. But If I declare sum = 0 inside the loop (pictures attached) ,it works perfectly.. Can anyone explain why and give some similar problems resources? Thanks... [ 1: https://i.stack.imgur.com/HsbXt.png[\]\[1\]][2]: https://i.stack.imgur.com/cONPo.png[2]

Md Anik
  • 61
  • 5
  • 3
    Please edit your code into the question as text. – Retired Ninja Sep 04 '21 at 09:56
  • 2
    Welcome to SO. Please do not post pictures of code or text output. Instead copy the code as formatted text into your question. Same for the output. You can use the `edit` button to fix this. – Gerhardh Sep 04 '21 at 09:56
  • You should run your code in a debugger and keep an eye on the variables. Especially for `sum` you will notice that you cannot get useful results if you do never reset the value. – Gerhardh Sep 04 '21 at 09:58

1 Answers1

2

You are making quite some mistakes here:

  • You are calling your variables mn and mx. Don't do that, but use better names, like range_min and range_max (that will increase the readability of your code).
  • Same story for your counters, n and i. You might opt for counter_range instead of n.
  • Very important: you write a program, and expect it to work from the first attempt. Although this is a noble attitude, it doesn't work that way: you write a program, you debug it and you see what's going wrong. Let's go a bit deeper into that debugging.
    • One of the way to do debugging, is using a debugger and follow the values of the variables.
    • One of the ways to do the debugging, is to add extra output on screen. When doing this, you might automatically see an output like this (for the case where you set your sum outside of the loop):
Sum equals [0].
Check if [2] is a perfect number.
As [1] is a counter of [2], it gets added to the sum: Sum equals [1].
Check if [3] is a perfect number.
As [1] is a counter of [2], it gets added to the sum: Sum equals [2].
Check if [4] is a perfect number.
As [1] is a counter of [4], it gets added to the sum: Sum equals [3].
As [2] is a counter of [4], it gets added to the sum: Sum equals [5].
Check if [5] is a perfect number.
As [1] is a counter of [5], it gets added to the sum: Sum equals [6].
Check if [6] is a perfect number.
As [1] is a counter of [6], it gets added to the sum: Sum equals [7].
As [2] is a counter of [6], it gets added to the sum: Sum equals [9].
As [3] is a counter of [6], it gets added to the sum: Sum equals [12].
...

So: when you set the sum outside of the loop, you are not calculating the sum of the divisors of that particular number, but the sum of the sum of the divisors of all numbers, who are smaller.

As you might see, one of the things I'm emphasising here, is readability: make your source code and your output readable, and you'll have much more chance to discover the bugs you will inevitably make.

Good luck

Dominique
  • 16,450
  • 15
  • 56
  • 112