Basically you need to divide the problem into two parts.
1. Determining whether a number is prime
This is the part already solved by other issues. Based on your algorithm, a possible implementation would be:
boolean ehPrimo(long n) {
long raiz = (long) Math.sqrt(n);
for (long k = 2; k <= raiz; k++) {
if (n % k == 0) return false;
}
return true;
}
2. Generating the numerical sequence according to the two formulae
When you define a formula or equation, it is not done with multiple lines as you did. It could have written much more simply.
If b = a + 37
and c = a + b
, we can replace b
in the second equation and obtain c = a + a + 37
.
The same applies to the calculation of d
, where we can simply write that d = c + 8
.
Now we have to a
is the variable and we can make a loop such that a
range of 1
to 100
. With each iteration, we can calculate the values of c
and d
as necessary.
Finally, we can add the values of c
primes in a list and the values of d
cousins on another list.
Code example:
List<Long> primeiraLista = new ArrayList();
List<Long> segundaLista = new ArrayList();
for (long a = 1; a <= 100; a++) {
long c = a + a + 37;
if (ehPrimo(c)) {
primeiraLista.add(c);
} else {
long d = c + 8;
if (ehPrimo(d)) {
segundaLista.add(d);
}
}
}
Finally, we can print the lists as needed. For example:
System.out.printf("Primeira lista %s%n", primeiraLista);
System.out.printf("Segunda lista %s%n", segundaLista);
Updating
After question update to BigInteger
the code could be amended as follows::
public class StrangePrimeFormulaWithBigInteger {
public static void main(String[] args) {
List<BigInteger> primeiraLista = new ArrayList();
List<BigInteger> segundaLista = new ArrayList();
BigInteger limit = new BigInteger("100");
BigInteger n37 = new BigInteger("37");
BigInteger n8 = new BigInteger("8");
for (BigInteger a = BigInteger.ONE; a.compareTo(limit) <= 0; a = a.add(BigInteger.ONE)) {
BigInteger c = a.add(a).add(n37);
if (ehPrimo(c)) {
primeiraLista.add(c);
} else {
BigInteger d = c.add(n8);
if (ehPrimo(d)) {
segundaLista.add(d);
}
}
}
System.out.printf("Primeira lista %s%n", primeiraLista);
System.out.printf("Segunda lista %s%n", segundaLista);
}
public static boolean ehPrimo(BigInteger n) {
BigInteger raiz = sqrt(n);
for (BigInteger k = new BigInteger("2"); k.compareTo(raiz) <= 0; k = k.add(BigInteger.ONE)) {
if (n.mod(k).equals(BigInteger.ZERO)) return false;
}
return true;
}
/**
* Extraído de https://stackoverflow.com/a/16804098/1683070
*/
public static BigInteger sqrt(BigInteger x) {
BigInteger div = BigInteger.ZERO.setBit(x.bitLength()/2);
BigInteger div2 = div;
// Loop until we hit the same value twice in a row, or wind
// up alternating.
for(;;) {
BigInteger y = div.add(x.divide(div)).shiftRight(1);
if (y.equals(div) || y.equals(div2))
return y;
div2 = div;
div = y;
}
}
}
Some considerations:
- Now the lists are with the new type:
List<BigInteger>
.
- Numbers need to be instantiated and not directly assigned:
BigInteger n8 = new BigInteger("8");
.
- I set up the numbers
37
, 8
and 100
before loop to avoid instantiating them repeatedly at each iteration.
- All mathematical operations need to use the methods of
BigInteger
as add
, divide
, mod
(remnant).
- Comparisons are made using
equals
and compareTo
, nay ==
.
a.compareTo(limit) <= 0
is the same as a <= limit
- Java does not provide a method to calculate the root of this type of variable. For a reliable solution you can use a library such as Apache Commons Math or Google Guava. In this case I inserted an implementation available in an answer from Soen.
- Variable calculations
BigInteger
and BigDecimal
can reach several orders of magnitude slower than with primitive types, although in this case whose computation is very light this difference is almost imperceptible.
Td well @utluiz , I sent a reply. thanks for helping..
– Lorenoobster
I managed to make the code work!!! Thank you very much!! But now I only needed one more thing: Apply everything to Biginteger to be able to do it with larger numbers, how do I apply Biginteger without breaking the code? (adding the formula, checking if it’s prime, all in Biginteger..)
– Lorenoobster
@Lorenoobster I’m glad you managed to make it work. However, for other questions you’d better ask another question. Put your current code on it.
– utluiz
Thanks @utluiz I sent another question this time more direct, I’m not able to change the code to Biginter that always gives error...
– Lorenoobster
@Lorenoobster You actually changed this question, which caused my answer to be out of context. Next time, create another question.
– utluiz
Hello, @utluiz.. I’m sorry for the hesitation to change the question... I really wanted to thank you, that’s what I wanted and I was having a lot of difficulty but I really got it thanks to you!!! Thank you so much for your help!!! May God bless you!!!!
– Lorenoobster