Hi everyone.
I am currently studying java and still at the beginner level but would love it if I could get help with some of my work. As of now I am trying to write down a code that detects numbers that are nearly prime numbers. As prime numbers are numbers that are only divisible by itself and one, I want to write a method that detects numbers that can be divisible by only 3 positive numbers.
So numbers such as 4 and 9 are examples of these since they can be divided by 3 numbers (1, 2, 4, and 1, 3,9). I already have a formula for prime numbers which I just found online but am having trouble figuring out a method to detect numbers that are close to prime numbers.
Any suggestions on how to code something like this? It'll be greatly appreciated.
Detecting almost prime numbers 
Post a Reply 
Replies 
Oldest Newest Rating 
+1 
Çağlan Turgut
· August 15, 2014
Made that simple console application that divides each number by each number that are smaller. That returns the remainders as a list and if the list has 3 elements then puts the bigger number into another list. Then prints that list. Sorry if I couldn't explain well. Here's the code:

0 
Ashy 
· August 15, 2014
Thank you so much for the helping code. Though in all honesty it's a little bit hard for me to understand. Right now I'm trying to change it so that when the user inputs the number it will tell them if the number is nearly a prime number or not. So if they gave the number 4 or 9 it will return true, but if they gave 6 or 2 it would return false. Can you try to explain to me your code a little more so I can try and complete this?

0 
Çağlan Turgut
· August 15, 2014
First I'm creating an integer array list to hold the outputted values. I'm not creating an array because as far as I know there is no way to change the size of an array easily. (Correct me if I'm wrong. I'm a beginner in java too )
Then I'm creating a for loop for each number till x.
I will divide the number “i” by each number that are lesser than i and get their remainder. To do that I'm creating an int called rmd to hold the remainder.
I'm creating the second number that we are going to divide i by a.k.a. the smaller number i will name it counter.
This second list will hold the counter values that gave the remainder zero when we divided i by counter. This will be reset for every number i:
I will create a loop in loop for the division by each counter. Counter will get bigger untill it reaches i: while(counter <= i){ To get the remainder of the division I'll use %.
Then I will add that counter value to list2 if the remainder is zero.
After that, I will pass to next counter.
Now, I'm checking if this “i”(One of the number's till x) has three dividers that gives the remainder “0”.
if it has, I will add this i to my first list.
If you have watched the enhanced for loop tutorial of bucky #31 you can understand what I'm doing here. That is basically a for loop for each Integer in list1:
number is the temporary name for each element in list1. We will print them:
So if we want the almost prime numbers to 4: 1.The Program will first get 1 and divide it to 1. The remainder is zero so 1 will be stored in list2. 2.It will check if there are three elements in list2 and it won't be able to find it. 3.Then it will go to 2 and divide 2 by 1. The remainder is 0. 1 will be written to list2. 4.It will divide 2 by 2. The remainder is 0. 2 will be written to list2. 5.It will check if list two has 3 element but it will only find two so it will pas to three... 6.When the program comes to four It will check 1 and add to list2, then check 2 and add to list2, check 3 and pass, check 4 and add to list2. Then will check if 4 has 3 dividers in list2. Yes there are 3 dividers in list2 so it will add 4 to list1. Then it will print 4. Maybe this is not the most efficient way to do that but that is all I can think of. I'm a beginner in java too so don't know very much about it. If you have any questions feel free to ask. PS. lesser than and greater than signs are a little bit bugged I think. They're looking like: < and > Happy Coding... 
0 
Ashy 
· August 16, 2014
Thank you so much for summarizing it down for me. It really helped a lot. As of now I'm still trying to analyze the code and change it so that it returns true or false when the user inputs an almost prime number or not, but I'm sure I'll be able to do that in due time.
Also, I've been told by my peers that there are a few other ways to do this. If anything I want to try and learn other methods I can use so it can help me in the future when I try to think of alternate solutions. If you have any other idea's on how to do this I would love to hear them. Thank you so much! 
+1 
Çağlan Turgut
· August 17, 2014
Yes, I've thought about other ways too but I wasn't able to put other methods on code. I am not that good on maths and formulas so I don't think I can help any further. There are quite a few formulas and ways to detect prime numbers. Maybe you can manipulate them for detecting almost prime numbers?
You can take a look at these links: http://rosettacode.org/wiki/Almost_prime#Icon_and_Unicon http://mathoverflow.net/questions/36738/detectingalmostprimesquickly http://mathoverflow.net/questions/3820/howhardisittocomputethenumberofprimefactorsofagiveninteger 
 1