连续因素测试

问题描述 投票:0回答:3

正数 n 是

consecutive-factored
当且仅当它有因数 i 和 j,其中
i > 1, j > 1 and j = i +1
。我需要一个函数,如果它的参数是连续分解的,那么它是
returns 1
,否则它是
returns 0
。例如,
24=2*3*4
3 = 2+1
所以它具有在这种情况下必须
return 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++;

            }
        }


        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;
        }

    }
}

谁能帮我解决这个问题?

java algorithm logic
3个回答
3
投票

先检查是否偶数,再试试除法

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;

效率很低,但对小数字来说很好。除此之外,尝试来自 http://en.wikipedia.org/wiki/Prime_factorization.

的因式分解算法

2
投票

我已经用上面的代码解决了我的问题。以下是代码。

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;
        }

    }
}

0
投票

我试过同样的问题,这是我想出的答案。

 public static int val(int a) {

for(int i =2 ; i<=a/2;i++){
  if(a%i== 0){
    
    if(a%(i+1) == 0)return 1;  
  }
}
return 0;

}

© www.soinside.com 2019 - 2024. All rights reserved.