jeudi 19 mars 2015

Simple java program- Amicable numbers to print pairs


I am trying to write a program that prints the amicable pairs (i,j) for n values, the command line arg and here is my code:


public class AmicablePairs { public static void main(String[] args) {



int n=Integer.parseInt(args[0]);
int sum = 0;
for (int i=1; i<=n; i++)
{
if (n%i == 0)
{
sum += i;
System.out.print("("+i);

}
}
for(int j =1;j<=n;j++)
{ if ( sum%j ==0)
{ sum+=j ; }
System.out.println(","+j+")");


} }


}


The solution should come to this: n being 3000


(220 , 284)


(1184 , 1210)


(2620 , 2924)


But I am not getting this, I get :


(1(2(4(5(10(20(25(50(100,1)


,2)


,3)


,4)


,5)


etc.until


,100)


I have tried this for a while now and I am not getting the right answer. Please help me understand where I am going wrong, I'm new to coding . I looked at other relateable questions and they are a little different.




Aucun commentaire:

Enregistrer un commentaire