Results 1 to 4 of 4
  1. #1
    Roter is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Big Integer problems

    Hello, Ive been trying this project for a while now, and I can't figure out a couple of things. How could you do a conditional loop with Biginteger values?
    Heres my code
    Java Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.BufferedWriter;
    import java.math.BigInteger;
    import java.util.ArrayList;
    
    public class Prime {
    
        public static void main(String[] args) { 
            //BigInteger N = BigInteger.parseLong(args[0]);
    	String file = args[0];
    	//reads numbers from file
     	ArrayList<BigInteger> x = read(file);
    
    	//turns numbers into primes
    	ArrayList<BigInteger> primes = turnPrimes(x);
    
    	//writes the primes
    	writePrimes(file, primes);
       }
    	public static ArrayList<BigInteger> turnPrimes(ArrayList<BigInteger> originalPrimes) { 
    		ArrayList<BigInteger> primes = new ArrayList<BigInteger>();
    		for(int i = 0; i < originalPrimes.size(); i++) {
    			primes.add(NextPrime(originalPrimes.get(i)));
    		}
    		return primes;
    	}
    
    	public static void writePrimes(String file, ArrayList<BigInteger> primes) {
    		String fileName = file;
    		if(file.contains(".txt")) {
    			fileName = fileName.substring(0, file.length() - 4);
    		}
    		try {
    			BufferedWriter writer = new BufferedWriter(new FileWriter(fileName + "primes.txt"));
    			for(int i = 0;i < primes.size(); i++) {
    				String number = String.valueOf(primes.get(i));
    				writer.write(number);
    				writer.newLine();
    			}
    			writer.close();
    		} catch(Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	public static ArrayList<BigInteger> read(String file) {
    		ArrayList<BigInteger> originalList = new ArrayList<BigInteger>();
    		try {
    			BufferedReader reader = new BufferedReader(new FileReader(file));
    			while(true) {
    				String line = reader.readLine();
    				if(line == null) {
    					break;
    				}
    				BigInteger longLine = BigInteger(line);
    				originalList.add(longLine);
    			}
    		} catch(Exception e) {
    			e.printStackTrace();
    		}
    		return originalList;
    	}
    			
    
        private static BigInteger BigInteger(String line) {	
    		return null;
    	}
    	public static java.math.BigInteger NextPrime(BigInteger bigInteger) {
            	BigInteger N = bigInteger;
            	boolean isPrime = true;
            	if (N < 2) {
    			isPrime = false;
    			}
    		while(N <= BigInteger.MAX_VALUE) {
    			isPrime = true;
           			for (int i = 2; i*i <= N; i++) {
           			    // if i divides evenly into N, N is prime, so break out of loop
            		    if (N % i == 0) {
            		        isPrime = false;
            		    }
            		}
    			if(isPrime) {
    				break;
    			}
    			N++;
    		}
    	return N;
         }
    }
    Could you provide any advice on how to work around this? and what would be the equivalent of Long.MAXVAULE with BigInteger? Any help would be greatly appreciated

  2. #2
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Big Integer problems

    Java Code:
    BigInteger equivalent = BigInteger.valueOf(Long.MAX_VALUE);

  3. #3
    Roter is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: Big Integer problems

    Ok, and any ideas on how to use BigIntegers in the loops?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Big Integer problems

    What errors are you seeing?
    Because you can't expect people to compile this for you.

Similar Threads

  1. Integer Comparison, Outputting Largest Integer Not Working
    By killingthemonkey in forum New To Java
    Replies: 4
    Last Post: 10-16-2011, 09:59 PM
  2. add integer value on each run
    By aconti in forum New To Java
    Replies: 4
    Last Post: 07-12-2011, 09:47 AM
  3. Big integer
    By penguinSlack in forum New To Java
    Replies: 1
    Last Post: 03-14-2011, 05:08 AM
  4. InputStream/Jar Problems/File IO Problems
    By rdjava in forum Advanced Java
    Replies: 31
    Last Post: 01-17-2011, 12:12 PM
  5. convert unsigned integer to signed integer in java?
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-17-2010, 01:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •