Saturday, June 2, 2007

Q2: Prime numbers between 100 and 200

Q: How many prime numbers are there between 100 and 200?

A:

public class PrimeNumber {
private static boolean isPrimeNumber(int num){
int e = (int)Math.pow(num, 0.5) + 1;
for(int i = 2; i< e; i++){
/* detect if the number is divided by a integer */
if( (num%i) == 0)
return false;
}
return true;
}
}

public static int getPrimeNumberQuantity(int root, int floor){
if(floor < 2) return 0;

int q = 0;
for(int i=root; i < floor; i++){
if(isPrimeNumber(i)){
q++;
System.out.print(i + ", ");
}
}
System.out.println();
return q;

}

public static void main(String[] args) {

int m = PrimeNumber.getPrimeNumberQuantity(100, 200);
System.out.println("There are " + m + " prime numbers between 100 and 200.");
}
}


Result:

101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199,
There are 21 prime numbers between 100 and 200.

3 comments:

Anonymous said...

Hello, this little page on prime numbers really helped me. I had homework on prome numbers 100-200 and this gave me the answers well helping me along the way.

Anonymous said...

Hello, this little page on prime numbers really helped me. I had homework on prome numbers 100-200 and this gave me the answers well helping me along the way.

ROSiiE said...

omg you just saved my butt fora homework I was doing!

Tysm! ;)

x