Why isn't this working? I am trying to check terms of the sequence a_j)=38^j +31 against values in the array. I am saying trying to get that if the specific term in the sequence is not divisible by any of these values in the array then print it.
public class SmallestPrime {
public static void main(String[] args) {
long m =0;
int [] b = {2,3,4,5,6,7,8};
for(int j=0;j<6;j++) {
m = (long) Math.pow(38,j) + 31;
if(m % b[j] !=0) {
System.out.println(m);
}
}
}
}
Your problem is that you are looping only once.
For example when j = 2
you only check if 1475 is divisible by b[2]
which happens to be 4 and 1475 is not divisible by 4 thus printing your value.
You'll need to use a nested loop to achieve what you are trying to do.
Here is a bit of code to help out:
private static void smallestPrime() {
long m = 0;
int[] b = { 2, 3, 4, 5, 6, 7, 8 };
for (int j = 0; j <= 6; j++) {
m = (long) Math.pow(38, j) + 31;
boolean prime = true;
for (int i = 0; i <= 6; i++) {
if (m % b[i] == 0) {
prime = false;
break;
}
}
System.out.println(m + " : " + prime);
}
}