0

Can someone help me figure out the error in this problem of finding the gap of 100 consecutive composite number between 2 primes

The code is in java .Help find out the error import java.util.List; import java.util.ArrayList; public class Prime100 { private List<Long> primes = new ArrayList<>(); private long targetGap; public Prime100(long targetGap) { this.targetGap = targetGap; primes.add(2L); primes.add(3L); } boolean isPrime(long n) { boolean ok = true; for(long p: primes) { if(p * p > n) { break; } if(n % p == 0) { ok = false; break; } } if(ok) { long lastPrime = primes.get(primes.size() - 1); long gap = n - lastPrime; if(gap == targetGap) { System.out.println(lastPrime + " -> " + n + ": gap = " + gap); } primes.add(n); } return ok; } public static void main(String[] args) { Prime100 primeChecker = new Prime100(102); long n = 5; while(true) { primeChecker.isPrime(n++); } } }

3rd Dec 2021, 8:28 AM
Ahsan Habib Laskar
Ahsan Habib Laskar - avatar
2 odpowiedzi
0
Yeah yeah it is correct i entered somewrong punctuation bdw thank for looking over 💕💕
3rd Dec 2021, 3:24 PM
Ahsan Habib Laskar
Ahsan Habib Laskar - avatar
0
Is there a bug? In eclipse it works without any problems. The only thing is, it's hard to read. I just formatted the code for easy reading. Maybe someone else will find the error. But the search for prime numbers is correct. package soloHelp; import java.util.ArrayList; import java.util.List; public class Prime100 { private List<Long> primes = new ArrayList<>(); private long targetGap; public Prime100(long targetGap) { this.targetGap = targetGap; primes.add(2L); primes.add(3L); } boolean isPrime(long n) { boolean ok = true; for (long p : primes) { if (p * p > n) { break; } if (n % p == 0) { ok = false; break; } } if (ok) { long lastPrime = primes.get(primes.size() - 1); long gap = n - lastPrime; if (gap == targetGap) { System.out.println(lastPrime + " -> " + n + ": gap = " + gap); } primes.add(n); } return ok; } public static void main(String[] args) { Prime100 primeChecker = new Prime100(100); long n = 5; while (true) { primeChecker.isPrime(n++); } } }
3rd Dec 2021, 2:53 PM
Alexey Kopyshev
Alexey Kopyshev - avatar