0

I have been trying to get this to work for days now and I still cannot figure out the error. When I output the code, it prints, but it will not find the amicable pairs (divisor of first == second, and vice versa).

#include <stdio.h>
#include <stdlib.h>
#define _USE_MATH_DEFINES
#include <math.h>

int sumDivisors( int num );

int sumDivisors( int num )
{
  int counter, total;
  for( counter = 1; counter < num; counter++ )
    {
      if( num % counter == 0 )
        {
          total += counter;
        }
    }
  return ( total );
}

int main( void )
{
  int higher, lower, lowx, lowy, x, y, numOfPairs = 0;

  printf( "This program finds all amicable numbers within a range. \n" );
  printf( "Please enter a lower limit: \n" );
  scanf( "%d", &lower );
  printf( "Please enter a higher limit: \n" );
  scanf( "%d", &higher );


  for( lowx = lower; lowx <= higher; lowx++ )
    {
      for( lowy = lower; lowy <= higher; lowy++ )
        {
          if( sumDivisors( lowx ) == sumDivisors( lowy ) )
            {
              numOfPairs++;
              printf( "Pair #%d: (%d, %d)\n", numOfPairs, lowx, lowy );
            }
        }
    }

  printf( "There are %d amicable pairs from %d to %d\n", numOfPairs, lower, higher );
  system("pause");
  return ( 0 );
}
4
  • Consider writing the inner loop as for( lowy = lowx + 1; lowy <= higher; lowy++ ) to avoid repetitions. Commented Sep 20, 2016 at 22:04
  • Good point, it makes it more efficient. Thanks! Commented Sep 20, 2016 at 22:50
  • On further thinking, you may have misinterpret the definition of amicable numbers, which are two different numbers so related that the sum of the proper divisors of each is equal to the other number. You can find a list of the first pairs at OEIS - A063990. Look at this Q&A for a more interesting optimization. Commented Sep 21, 2016 at 11:39
  • Yah, that was a logical error. I changed if( sumDivisors( lowx ) == sumDivisors( lowy ) ) to if( sumDivisors( lowx ) == lowy && if( sumDivisors( lowy ) == lowx), and that worked Commented Sep 22, 2016 at 12:02

1 Answer 1

6

You are not assigning any value to total in your code:

int sumDivisors( int num )
{
  int counter, total;
  for( counter = 1; counter < num; counter++ )
    {
      if( num % counter == 0 )
        {
            total += counter;
        }
    }
  return ( total );
}

so it contains garbage not predictable value!

it should be like: int counter, total = 0;

Sign up to request clarification or add additional context in comments.

4 Comments

Oh gosh... thank you... you saved me. I am used to null being zero
@Submersed24 What does it have to do with null being zero?
I used to think variables are defaulted at 0, so you don't need to set them to zero
@Submersed24, I'm glad that it helped! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.