# Big Integer problems

• 02-01-2012, 10:07 PM
Roter
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
Code:

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];

//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++) {
}
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 {
while(true) {
if(line == null) {
break;
}
BigInteger longLine = BigInteger(line);
}
} 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
• 02-01-2012, 10:28 PM
diamonddragon
Re: Big Integer problems
Code:

BigInteger equivalent = BigInteger.valueOf(Long.MAX_VALUE);
• 02-01-2012, 10:41 PM
Roter
Re: Big Integer problems
Ok, and any ideas on how to use BigIntegers in the loops?
• 02-02-2012, 11:55 AM
Tolls
Re: Big Integer problems
What errors are you seeing?
Because you can't expect people to compile this for you.