如何计算一个数字java

时间:2011-06-03 05:54:01

标签: java

我需要将数字分解为24到1,2,2,2,3。我的方法是:

static int[] factorsOf (int val) {
          int index = 0;
      int []numArray = new int[index];

      System.out.println("\nThe factors of " + val + " are:");
      for(int i=1; i <= val/2; i++)
      {
          if(val % i == 0)
          {   
              numArray1 [index] = i;
              index++;
          }
      }

      return numArray;

  }
然而,它不起作用。任何人都可以帮助我吗?

6 个答案:

答案 0 :(得分:7)

你有一些错误,你不能创建没有大小的int数组。我改为使用数组列表。

static Integer[] factorsOf(int val) {
    List<Integer> numArray = new ArrayList<Integer>();

    System.out.println("\nThe factors of " + val + " are:");
    for (int i = 2; i <= Math.ceil(Math.sqrt(val)); i++) {
        if (val % i == 0) {
            numArray.add(i);
            val /= i;
            System.out.print(i + ", ");
        }
    }
    numArray.add(val);
    System.out.print(val);
    return numArray.toArray(new Integer[numArray.size()]);
}

根据您的要求使用int []的完整程序。

public class Test2 {
    public static void main(String[] args) {
        int val = 5;
        int [] result = factorsOf(val);
        System.out.println("\nThe factors of " + val + " are:");
        for(int i = 0; i < result.length && result[i] != 0; i ++){
            System.out.println(result[i] + " ");
        }
    }

    static int[] factorsOf(int val) {
        int limit = (int) Math.ceil(Math.sqrt(val));
        int [] numArray = new int[limit];
        int index = 0;

        for (int i = 1; i <= limit; i++) {
            if (val % i == 0) {
                numArray[index++] = i;
                val /= i;
            }
        }
        numArray[index] = val;
        return numArray;
    }
}

答案 1 :(得分:0)

一个工作示例

public class Main
{
public static void main(String[] args)
{
    System.out.println(factorsOf(24));
}

static List<Integer> factorsOf (int val) {

      List<Integer> factors  = new ArrayList<Integer>();
      for(int i=1; i <= val/2; i++)
      {
          if(val % i == 0)
          {
              factors.add(i);
          }
      }

      return factors;

  }

  }

答案 2 :(得分:0)

public int[] primeFactors(int num)
{
    ArrayList<Integer> factors = new ArrayList<Integer>();
    factors.add(1);
    for (int a = 2;  num>1; )
        if (num%a==0)
        {
            factors.add(a);
            num/=a;
        }
        else
            a++;
    int[] out = new int[factors.size()];
    for (int a = 0; a < out.length; a++)
        out[a] = factors.get(a);
    return out;
}

答案 3 :(得分:0)

你看起来更快吗?:

static int[] getFactors(int value) {
    int[] a = new int[31]; // 2^31
    int i = 0, j;
    int num = value;
    while (num % 2 == 0) {
        a[i++] = 2;
        num /= 2;
    }
    j = 3;
    while (j <= Math.sqrt(num) + 1) {
        if (num % j == 0) {
            a[i++] = j;
            num /= j;
        } else {
            j += 2;
        }
    }
    if (num > 1) {
        a[i++] = num;
    }
    int[] b = Arrays.copyOf(a, i);
    return b;
}

答案 4 :(得分:0)

此处建议的大多数方法的时间复杂度为0(n)。可以使用时间复杂度 0(log n)的二进制搜索方法轻松解决此问题。

您基本上在寻找什么叫做 Prime Factors (尽管在主要因子中不考虑1)。

//finding any occurrence of the no by binary search
static int[] primeFactors(int number) {
    List<Integer> al = new ArrayList<Integer>();
    //since you wanted 1 in the res adn every no will be divided by 1;
    al.add(1); 

    for(int i = 2; i< number; i++) {
     while(number%i == 0) {
        al.add(i);
        number = number/i;
     }
  }
  if(number >2) 
     al.add(number);

  int[] res = new int[al.size()];
  for(int i=0; i<al.size(); i++)
    res[i] = al.get(i);

  return res;
}

说输入为24,我们将输入除以2,直到2的所有倍数都消失,将i增加到3

以下是工作代码的链接:http://tpcg.io/AWH2TJ

答案 5 :(得分:-1)

你错过了if中的一步。以下代码是正确的:

System.out.println("\nThe factors of " + val + " are:");

您可以使用val的平方根进行比较,并按值2

启动迭代器
if(val % i == 0)
{   
   numArray1 [index] = i;
   val=val/i; //add this
  index++;
}

但是你需要检查索引是否为2,它是prime