Java 连续因子检验

Java 连续因子检验,java,algorithm,logic,Java,Algorithm,Logic,正数n是连续因子当且仅当它有因子i和j,其中i>1,j>1和j=i+1。我需要一个返回1的函数,如果它的参数是连续的系数,否则它返回0。例如,24=2*3*4和3=2+1,因此在这种情况下它必须返回1 public class ConsecutiveFactor { public static void main(String[] args) { // TODO code application logic here Scanner myscan =

正数n是
连续因子
当且仅当它有因子i和j,其中
i>1,j>1和j=i+1
。我需要一个
返回1
的函数,如果它的参数是连续的系数,否则它
返回0
。例如,
24=2*3*4
3=2+1
,因此在这种情况下它必须
返回1

public class ConsecutiveFactor {

    public static void main(String[] args) {
        // TODO code application logic here

        Scanner myscan = new Scanner(System.in);
        System.out.print("Please enter a number: ");
        int num = myscan.nextInt();
        int res = isConsecutiveFactored(num);
        System.out.println("Result: " + res);

    }
    static int isConsecutiveFactored(int number) {
        ArrayList al = new ArrayList();
        for (int i = 2; i <= number; i++) {
            int j = 0;
            int temp;
            temp = number % i;

            if (temp != 0) {
                continue;
            } 

            else {

                al.add(i);
                number = number / i;
                j++;

            }
        }

        Object ia[] = al.toArray();
        System.out.println("Factors are: " + al);
        int LengthOfList = al.size();
        if (LengthOfList >= 2) {
            int a = ((Integer) ia[0]).intValue();
            int b = ((Integer) ia[1]).intValue();

            if ((a + 1) == b) {
                return 1;
            } else {
                return 0;
            }
        } else {
            return 0;
        }

    }
}
我试过这个:

public class ConsecutiveFactor {

    public static void main(String[] args) {
        // TODO code application logic here

        Scanner myscan = new Scanner(System.in);
        System.out.print("Please enter a number: ");
        int num = myscan.nextInt();
        int res = isConsecutiveFactored(num);
        System.out.println("Result: " + res);

    }
    static int isConsecutiveFactored(int number) {
        ArrayList al = new ArrayList();
        for (int i = 2; i <= number; i++) {
            int j = 0;
            int temp;
            temp = number %i;

            if (temp != 0) {
                continue;
            } 

            else {

                al.add(i);
                number = number / i;
                j++;

            }
        }


        System.out.println("Factors are: " + al);
        int LengthOfList = al.size();
        if (LengthOfList >= 2) {
            int a =al(0);
            int b = al(1);
            if ((a + 1) == b) {
                return 1;
            } else {
                return 0;
            }
        } else {
            return 0;
        }

    }
}
public class ConsecutiveFactor {

    public static void main(String[] args) {
        // TODO code application logic here

        Scanner myscan = new Scanner(System.in);
        System.out.print("Please enter a number: ");
        int num = myscan.nextInt();
        int res = isConsecutiveFactored(num);
        System.out.println("Result: " + res);

    }
    static int isConsecutiveFactored(int number) {
        ArrayList al = new ArrayList();
        for (int i = 2; i <= number; i++) {
            int j = 0;
            int temp;
            temp = number % i;

            if (temp != 0) {
                continue;
            } 

            else {

                al.add(i);
                number = number / i;
                j++;

            }
        }

        Object ia[] = al.toArray();
        System.out.println("Factors are: " + al);
        int LengthOfList = al.size();
        if (LengthOfList >= 2) {
            int a = ((Integer) ia[0]).intValue();
            int b = ((Integer) ia[1]).intValue();

            if ((a + 1) == b) {
                return 1;
            } else {
                return 0;
            }
        } else {
            return 0;
        }

    }
}
公共类连续因子{
公共静态void main(字符串[]args){
//此处的TODO代码应用程序逻辑
Scanner myscan=新扫描仪(System.in);
System.out.print(“请输入一个数字:”);
int num=myscan.nextInt();
int res=isconsecurivefactored(num);
System.out.println(“结果:+res”);
}
静态整数isConsecutiveFactored(整数编号){
ArrayList al=新的ArrayList();
对于(int i=2;i=2){
int a=al(0);
int b=al(1);
如果((a+1)=b){
返回1;
}否则{
返回0;
}
}否则{
返回0;
}
}
}

有人能帮我解决这个问题吗?

首先检查它是否相等,然后尝试试用除法

if(n%2!=0) return 0;
for(i=2;i<sqrt(n);++i) {
  int div=i*(i+1);
  if( n % div ==0) { return 1; }
}
return 0;
public class ConsecutiveFactor {

    public static void main(String[] args) {
        // TODO code application logic here

        Scanner myscan = new Scanner(System.in);
        System.out.print("Please enter a number: ");
        int num = myscan.nextInt();
        int res = isConsecutiveFactored(num);
        System.out.println("Result: " + res);

    }
    static int isConsecutiveFactored(int number) {
        ArrayList al = new ArrayList();
        for (int i = 2; i <= number; i++) {
            int j = 0;
            int temp;
            temp = number % i;

            if (temp != 0) {
                continue;
            } 

            else {

                al.add(i);
                number = number / i;
                j++;

            }
        }

        Object ia[] = al.toArray();
        System.out.println("Factors are: " + al);
        int LengthOfList = al.size();
        if (LengthOfList >= 2) {
            int a = ((Integer) ia[0]).intValue();
            int b = ((Integer) ia[1]).intValue();

            if ((a + 1) == b) {
                return 1;
            } else {
                return 0;
            }
        } else {
            return 0;
        }

    }
}
如果(n%2!=0)返回0;

对于(i=2;i我已经用上面的代码解决了我的问题。下面是代码

public class ConsecutiveFactor {

    public static void main(String[] args) {
        // TODO code application logic here

        Scanner myscan = new Scanner(System.in);
        System.out.print("Please enter a number: ");
        int num = myscan.nextInt();
        int res = isConsecutiveFactored(num);
        System.out.println("Result: " + res);

    }
    static int isConsecutiveFactored(int number) {
        ArrayList al = new ArrayList();
        for (int i = 2; i <= number; i++) {
            int j = 0;
            int temp;
            temp = number % i;

            if (temp != 0) {
                continue;
            } 

            else {

                al.add(i);
                number = number / i;
                j++;

            }
        }

        Object ia[] = al.toArray();
        System.out.println("Factors are: " + al);
        int LengthOfList = al.size();
        if (LengthOfList >= 2) {
            int a = ((Integer) ia[0]).intValue();
            int b = ((Integer) ia[1]).intValue();

            if ((a + 1) == b) {
                return 1;
            } else {
                return 0;
            }
        } else {
            return 0;
        }

    }
}
公共类连续因子{
公共静态void main(字符串[]args){
//此处的TODO代码应用程序逻辑
Scanner myscan=新扫描仪(System.in);
System.out.print(“请输入一个数字:”);
int num=myscan.nextInt();
int res=isconsecurivefactored(num);
System.out.println(“结果:+res”);
}
静态整数isConsecutiveFactored(整数编号){
ArrayList al=新的ArrayList();
对于(int i=2;i=2){
inta=((整数)ia[0]).intValue();
intb=((整数)ia[1]).intValue();
如果((a+1)=b){
返回1;
}否则{
返回0;
}
}否则{
返回0;
}
}
}

我首先要说的是,如果这样一对因素存在,那么它是唯一的。这应该指向它们的价值。提示2:你可以使用
Math.sqrt
Math.floor
Math.ceil
对你有利。@MukulGoel我已经添加了我尝试过的代码,但它不起作用。@all:我认为我们应该只警告新的合作伙伴如果他们不知道否决投票的事情,我们应该通过警告他们不要再这样做来鼓励他们,但不要通过否决simplyNote来劝阻他们只需要将k=sqrt(n)检查为k*(k+1)>n.所以你可能会因为我之前的评论而恨我,但是看,kiddo,你自己解决了它,现在你肯定是一个更好的程序员,你的方法非常棒,carryon@HussainAkhtarWahidthanx的动机。我并没有因为你的评论而恨你。;)20岁会怎么样?有2,4,5,10,20个因子。根据定义,前4、5项为连续因素。但是,您的算法返回0。