【问题标题】:Consecutive factor test连续因子检验
【发布时间】:2012-11-20 05:46:56
【问题描述】:

一个正数 n 是consecutive-factored 当且仅当它有因子 i 和 j 其中i > 1, j > 1 and j = i +1。我需要一个 returns 1 的函数,如果它的参数是连续因子,否则它是 returns 0。例如,24=2*3*43 = 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;
        }

    }
}

谁能帮我解决这个问题?

【问题讨论】:

  • 我首先要说,如果存在这样一对因素,那么它就是独一无二的。这应该指向它们的价值。
  • 提示 2:您可以使用 Math.sqrtMath.floorMath.ceil 为您带来好处。
  • @MukulGoel 我已经添加了我尝试过的代码,但它不起作用。
  • @all : 人们我认为我们应该只警告他们不知道投反对票的新人,我们应该通过警告不要再这样做来鼓励他们,但不要通过简单的投反对票来阻止他们跨度>

标签: java algorithm logic


【解决方案1】:

先判断是否偶数,再试除法

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 的分解算法。

【讨论】:

  • 注意只需要检查 upto k=sqrt(n) as k*(k+1) > n.
【解决方案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;
        }

    }
}

【讨论】:

  • 所以你可能因为我以前的 cmets 而讨厌我,但是看孩子,你自己解决了,现在你绝对是一个更好的程序员,你的方法非常棒,继续
  • @HussainAkhtarWahid 感谢激励。我肯定不会因为你的 cmets 而恨你。 ;)
  • 20 会发生什么?这具有因子 2、4、5、10、20。根据顶部 4,5 的定义,是连续因子。但是你的算法返回 0。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-30
  • 2019-07-30
  • 2016-11-18
  • 2019-04-12
  • 1970-01-01
相关资源
最近更新 更多