Denmin Group Interview Question
Software Engineer / Developersprolific coder, i dont kniow if your logic is working. I will write pseduo code for what i answered:
1. i greated three methods
a. public Int getAlmostPrimes(int start, int last) - this method iterates through the rate and return an int number of all the almost primes in the range
b. private boolean isPrime(int n) - this method test to see the number is prime - i created an array of the prime numbers and just tested the inputted number to the array
c. private boolean isAlmostPrime(int n) - this method just test to see if the number is an almost prime
2. in the public method - first create an int counter, second loop through range, if almost prime increase counter by 1, return counter
3. loop logic - if the number in the range is not a prime and almost prime add one to counter
4. isAlmostPrime - **only divide by lower primes 2 - 13 **
a. test for reminder first - n%prime number - if their is a reminder then go to next number, if there isn't a reminder divide n/prime without reminder, then test to see the answer is a prime. if the answer is a prime return true
basic code of above psuedo code:
class variable:
//array of all prime numbers 0 to 100
int[] primes = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
//logic to count the almost prime numbers in the range
public int getAlmostPrimes(int start, int last){
int counter = 0;
for(int i = start; i <= last; i++){
if(!isPrime(i)){
if(isAlmostPrime(i)){
count++;
}
}
i++;
}
//figure out if the given number is a prime number
private boolean isPrime(int n){
boolean prime = false;
for (int item : this.primes) {
if(n == item){
prime = true;
}
}
return prime;
}
//logic to find if the number is an almost prime
private boolean isAlmostPrime(int n){
boolean almostPrime = false;
int value = 0;
if(n < 4){
return almostPrime;
}else if(n % 2 == 0){
value = n / 2;
if(isPrime(value)){
almostPrime = true;
}
}
//repeat logic for lower prime number 3,5,7,11,13
}
}
Sorry bout the alignment, hope you guys can read my code good. I forgot to add the return of the boolean in the isAlmostPrime method. in the almostprime method if the number is less then 4 then you know it can not be an almostprime number - it would of been beeter to add the logic to the get loop method
Sorry bout the alignment, hope you guys can read my code good. I forgot to add the return of the boolean in the isAlmostPrime method. in the almostprime method if the number is less then 4 then you know it can not be an almostprime number - it would of been beeter to add the logic to the get loop method
This solution seems to be working, though I have some shaky logic.
love to be proven wrong :)
//Assuming start and end gives the range inclusieve and we have primes given in hashset
//if not we need to create a hashset in the code
//Also end should be less than 101 since we are given only the first 100 primes
}
- prolific.coder May 18, 2010