-1

I'm trying to write a function that calculates all the prime factors of a given number n. I have that part down, however, I don't know how to do this with really big numbers like 244825004422. How could I optimize myPrimeFactors method to work with really huge numbers? Here is my code so far. Thanks.

What I have so far doesn't work as it says my number is out of range for my parameter.

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);
    }
}
Baby
  • 5,062
  • 3
  • 30
  • 52
Binka
  • 31
  • 1
  • 5

1 Answers1

1

have you tried?:

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

Also, with your example, you should pass the BigInteger value you created:

myPrimefactors(new BigInteger("244825004422"));
Rogue
  • 11,105
  • 5
  • 45
  • 71