Question

I created a simple prime factorization program in java for the fun of it. Right now I am using the Random class and nextLong() method to assign "temp" a random number ranging in the quintillions and factors it amazingly quickly. What data type or algorithm or method should I use to obtain significantly larger values?

import java.util.Random;
import javax.swing.JOptionPane;

public class factor {
public static void main(String[] args) {
   Random gen = new Random();
   String factors = "";
   long temp = 0;
   String hello = JOptionPane.showInputDialog("Type a random number(Must be smaller than 4,611,686,018,427,387,904), or type 1 for a random large number.");
  temp = Long.parseLong(hello);
   if(temp < 2)
   temp = Math.abs(gen.nextLong());
   long temp2 = temp;
   System.out.println("  The factors of   \n\n  " + temp2 + "   are:");
   System.out.println("");
   while(temp != 1){
       //System.out.print(temp);
   for(long ii = 2; ii <= (Math.ceil(Math.sqrt(temp))); ii++){
  // if(ii%12345 == 0)
 // System.out.println(temp + " " + ii + " " + factors);
        if(temp%ii == 0){
            factors = factors + "  " + ii;
           // System.out.println(temp + " " + ii + " " + factors);
            temp = temp/ii;
            ii = temp + 1;       
  }else{
   if(ii == Math.ceil(Math.sqrt(temp))){
   factors = factors + " " + temp;
            System.out.println(factors);
            temp = 1;
            ii = temp + 1;   
   }}
        }
   }}}
Was it helpful?

Solution

You can use the random constructor for BigInteger:

BigInteger(int numBits, Random rnd)

If you need any more information, you can check out the API: BigInteger

OTHER TIPS

It's useless and sadly coded. You can play with big number and say it's fast but the point is not "how big is my number"... it's "how bigs are the factors". Just take a big prime number and sleep until your code works.

For the big number generation... there is BigInteger

public class Factor {

    public static BigInteger generateBigOne() {
        final Random random = new Random();

        long loop = random.nextLong();
        String result = "";
        for (int i = 0; i < loop; i++) {
            result += random.nextLong();
        }
        return new BigInteger(result);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top