-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountPrimeNumbers.java
More file actions
36 lines (33 loc) · 1.02 KB
/
Copy pathCountPrimeNumbers.java
File metadata and controls
36 lines (33 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//You will be given an integer n. You need to return the count of prime numbers less than or equal to n.
public class Solution {
public int solve(int A) {
int countOfPrimeNumber = 0;
for(int i = 1; i <=A; i++){
if(isPrimeNumber(i)){
countOfPrimeNumber++;
}
}
return countOfPrimeNumber;
}
// If A Number has exactly 2 factors then its a Prime Number. 1 is not a prime number as it has only 1 factor.
public boolean isPrimeNumber(int A) {
int count = countFactors(A);
if(count == 2){
return true;
}
return false;
}
public int countFactors(int A){
int count = 0;
for(int i = 1; i <= Math.sqrt(A); i++){
if(A%i == 0){ // check if i is a factor
if(i == A/i){ // check if its a perfect square root
count++;
}else{
count = count+2;
}
}
}
return count;
}
}