The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
February 2002

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

amicable

Posted by tricky on February 09, 2002 at 8:25 AM

simple method :
first you need a function as you mentioned it which returns the sum of divisors when a integer n is given,
e.g. with two functions

public boolean divides (int n, int m)
{
if (m%n>0) {return false;}
else return true;
}

making use of "%" modulo which gives the rest of m divided by n.Here and in the following there could some grammatical(concerning Java) mistakes, but you said you needed the basic idea.

next function
gives the sum of divisors of n

public int sumdiv (int n)
{
int sum=0;
for (i=2;i{
if (divides(n,i)) // making use of the upper function
{
sum=+i;
}
}
return sum;
}

Using these functions
we should answer : Is n amicable ?

public boolean amicable(int n)
{
int m=sumdiv(n);
if (sumdiv(m)==n) return true;
else return false;
}

Added to a "for (..).." in the main method this should do the job.
If you want to write the programm you should create a class.

public class number
{
static int number;
int sum; // sum of all divisors
boolean amicable;
// methods (see above)

}

i hope i could help you



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us