1. Member
Join Date
Jan 2012
Posts
8
Rep Power
0

## 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.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

2. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
7

## Re: Big Integer problems

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

3. Member
Join Date
Jan 2012
Posts
8
Rep Power
0

## Re: Big Integer problems

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

4. Moderator
Join Date
Apr 2009
Posts
13,532
Rep Power
24

## Re: Big Integer problems

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

#### Posting Permissions

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