Home Article Practice java speed

java speed

2021-12-13 13:30  views:593  source:1448501248    

package Main8;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
while(a!=0||b!=0) {
perfect(a,b);
amicable(a,b);
a=sc.nextInt();
b=sc.nextInt();
}
sc.close();
}
static int fSum(int n) {
int sum=0;
for(int i=1;i<Math.sqrt(n);i++) {
if(n%i==0) {
sum+=i+n/i;
}
}
return sum-n;
}
static void perfect(int start,int end) {
if(start>end) {
int t=start;
start=end;
end=t;
}
for(int i=start;i<=end;i++) {
if(fSum(i)==i) {
System.out.print(i+" ");
}
}
System.out.println();
}
static void amicable(int start,int end) {
if(start>end) {
int t=start;
start=end;
end=t;
}
for(int i=start;i<=end;i++) {
int j=fSum(i);
if(fSum(j)==i&&i<j) {
System.out.print(i+","+j+" ");
}
}
System.out.println();
}
}



Disclaimer: The above articles are added by users themselves and are only for typing and communication purposes. They do not represent the views of this website, and this website does not assume any legal responsibility. This statement is hereby made! If there is any infringement of your rights, please contact us promptly to delete it.

字符:    改为:
去打字就可以设置个性皮肤啦!(O ^ ~ ^ O)