2014-04-02 38 views
-1

我想写一个函数来计算给定数字n的所有素数因子。但是,我不知道如何用244825004422这样的大数字来做到这一点。我怎样才能优化myPrimeFactors方法来处理真正庞大的数字?这是我的代码到目前为止。谢谢。将大数字作为函数中的参数传递?

我到目前为止没有工作,因为它说我的号码超出了我的参数范围。

import java.math.BigInteger; 
public class myFactors { 
    public static void main(String [] args){ 
     BigInteger reallyBig = new BigInteger("244825004422"); 
     myPrimefactors(244825004422); 
    } 

public static BigInteger myPrimefactors(int n){ 
     while (n % 2 == 0){ 
      System.out.println("2"); 
      n = n/2; 
     } 
     for (int i = 3; i <= Math.sqrt(n); i = i + 2) { 
      while (n % i == 0){ 
       System.out.println(i); 
       n = n/i; 
      } 
     } 
     if (n > 2) 
      System.out.println(n); 
    } 
} 

回答

1

你试过?:

public BigInteger myPrimefactors(BigInteger n) {...} 

另外,与你的榜样,你应该通过您创建的BigInteger的值:

myPrimefactors(new BigInteger("244825004422")); 
+0

是。当我这样做时,它告诉我不是像%,sqrt和/ work这样的操作符。 – Binka

+0

尝试重载运算符%和/ for the class BigInteger – datahaki

相关问题