Reputation: 11
The goal of my program is to write a program that will prompt the user to input an integer. The program will read the integer and decide whether it is a prime or not. And if it is not a prime, it will tell the user all the divisors.
This is how far I have got, but when i tested it with numbers like 169 or 289, the program said that they were prime number. I understand that the problem is lying on this line :
int[] div = new int[] { 2,3,4,5,6,7,8,9};
I tried to do something like this :
for (int s = nr; s != 0 ; s--) {
if (nr%s == 0) {
int[] div = new int[]{s}; }
But it didn't work. A little help or a right direction would help a lot. Thank You!
public static void main(String[] args){
System.out.println("enter a number:");
Scanner scanner = new Scanner (System.in);
int nr = scanner.nextInt();
int[] div = new int[] { 2,3,4,5,6,7,8,9};
boolean prime = nr >= 2;
int i = nr;
for(int j = 0; j< div.length && prime && i> div[j]; j++)
if(i % div[j] == 0)
prime = false;
if(prime){
System.out.println(i + " is a prime");
}else{
System.out.print(i + " is divisible");
for(int k = 2; k < i; k++)
if(i % k == 0){
System.out.print( k);
System.out.print(",");}
}
} }
Upvotes: 1
Views: 6456
Reputation: 1131
<edit> I should have looked a little closer... I guess it IS homework, so this answer is not really any good to you, eh? Well, I'm gonna leave it up in case the information here becomes useful to someone somewhere...
</edit>
If you don't actually need to do the calculations yourself (if this isn't homework, that is), you could always opt to use Java's BigInteger class. For example:
public class Prime {
public static void main(String[] args){
long start = System.nanoTime();
System.out.println("enter a number:");
Scanner scanner = new Scanner(System.in);
BigInteger bigInt = scanner.nextBigInteger();
boolean prime = bigInt.isProbablePrime(10);
System.out.println(prime);
}
}
As with most things in life, there are at least a couple caveats:
the probability of reporting a number's primality is not 100%, nor can it be (but we'll talk about that in a moment); and
the more accurate you want the test to be, the longer it will take.
This is detailed in Oracle's Official Documentation:
isProbablePrime
public boolean isProbablePrime (int certainty)
Returns
true
if this BigInteger is probably prime,false
if it's definitely composite. Ifcertainty
is<= 0
,true
is returned.Parameters:
certainty
- a measure of the uncertainty that the caller is willing to tolerate: if the call returnstrue
the probability that this BigInteger is prime exceeds(1 - 1/(2^certainty))
. The execution time of this method is proportional to the value of this parameter.Returns:
true
if this BigInteger is probably prime,false
if it's definitely composite.
I got curious about how accurate it could be and how much time a highly-accurate primality test would take, so I made a quick benchmark and ran some numbers.
I calculated the primality of every odd number under a million for each certainty value between 1 and 50 (0 or less always returns true).
Times are in milliseconds (though the times were obtained via a call to System.nanoTime()
, and then rounded to the nearest millisecond).
Here are my results:
Certainty Time(ms) Chance of Correctness
1 1417 50.0%
2 932 75.0%
3 1064 87.5%
4 1063 93.75%
5 1183 96.875%
6 1195 98.4375%
7 1313 99.21875%
8 1308 99.609375%
9 1441 99.8046875%
10 1443 99.90234375%
11 1567 99.951171875%
12 1571 99.9755859375%
13 1690 99.98779296875%
14 1691 99.993896484375%
15 1817 99.9969482421875%
16 1822 99.99847412109375%
17 1944 99.99923706054688%
18 1941 99.99961853027344%
19 2069 99.99980926513672%
20 2073 99.99990463256836%
21 2197 99.99995231628418%
22 2200 99.99997615814209%
23 2324 99.99998807907104%
24 2340 99.99999403953552%
25 2453 99.99999701976776%
26 2465 99.99999850988388%
27 2647 99.99999925494194%
28 2626 99.99999962747097%
29 2715 99.99999981373549%
30 2710 99.99999990686774%
31 2844 99.99999995343387%
32 2818 99.99999997671694%
33 2950 99.99999998835847%
34 3074 99.99999999417923%
35 3121 99.99999999708962%
36 3167 99.99999999854481%
37 3295 99.9999999992724%
38 3302 99.9999999996362%
39 3334 99.9999999998181%
40 3360 99.99999999990905%
41 3493 99.99999999995453%
42 3583 99.99999999997726%
43 3663 99.99999999998863%
44 3599 99.99999999999432%
45 3783 99.99999999999716%
46 3816 99.99999999999858%
47 3964 99.99999999999929%
48 3898 99.99999999999964%
49 4029 99.99999999999982%
50 3969 99.99999999999991%
total: 124312
As you can see, even at the highest certainty-value that I tested, evaluating the primality of 500,000 numbers only took 4 seconds and each evaluation had a 99.99999999999991% chance of being correct
So if your intended application isn't incredibly performance-critical, you can use a (relatively) high number, like 25. The algorithm will correctly report a number as prime 99.99999701976776% of the time. Not recommended if you're a rocket scientist. And if you are, a rocket scientist, then I hope you find your solution elsewhere :).
Upvotes: 5
Reputation: 36703
Just use the BigInteger.isProbablePrime() library function built into Java... Not 100% sure how to use the certainty parameter though.
BigInteger.valueOf(13).isProbablePrime(10000)
Upvotes: 0
Reputation: 11721
A very easy and basic way to find if a number is prime is this:
Continue dividing n by each number between 2 and n^1/2 inclusive. If any of them divide evenly, then n is not prime because you found a factor. If n has no factors less than its square root, then n is prime. It is sufficient to check only for divisors less than or equal to n^1/2 because if n = a*b, then a and b can't both exceed the square root of n.
for(int i=2;i<=n^1/2;i++)
{
if(num%i)==0 //number not prime
else continue;
}
if none of the if statements in the loop were true, number is prime
Upvotes: 1
Reputation: 5706
The reason you are getting 169 as a prime is because you are only dividing by the values 2-9 and 169 is 13 squared so it isn't divisible by any number 2-9.
you can use this method http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes to find all the prime numbers between 1 and n and use that to decide if your number is prime.
Upvotes: 3
Reputation: 82559
You're only trying numbers up to 9 for divisors. So anything that would have factors of all 10 or higher, such as 169 being 13 by 13, you will not find the 13s.
Instead of storing the divisors in an array, consider using an integer and counting upwards. That is to say, instead of div[j]
just use j as your divisor, and don't let it stop at 10. Let it stop at the highest possible divisor (which is the square root of the number you're trying to find the prime of).
Upvotes: 5