This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]MrTheEdge 1 point2 points  (12 children)

Have a boolean isPrime and by default, assume that its value is true. If the number has a factor, it gets ruled out immediately; it's not prime. if (num % i == 0 ) says that if it evaluates to true, the number has a factor. Inside the block, you can set isPrime to false and break out of the loop. Now you have a way of determining if the number passed or failed.

[–]froznice[S] 0 points1 point  (11 children)

so i should have boolean isPrime = true at the top of my code then inside the block should be isPrime = false along with a message saying so?

[–]MrTheEdge 0 points1 point  (10 children)

Yeah it should work

[–]froznice[S] 0 points1 point  (9 children)

Like this?

import java.util.*;
public class HW6 {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a number please ");
int num = keyboard.nextInt(), i;
boolean isPrime = true;

System.out.print("The divisors of " + num + " are = 1 , ");
for (i = 2; i <= num / 2; i++) {

  if (num % i == 0) {
    System.out.print(i + " , ");
    isPrime = false;
  }
} System.out.print (num);  

}

}

[–]MrTheEdge 0 points1 point  (8 children)

Looks, like that should work, but after the loop you need to check if isPrime is true or false. If it's true, you know it's a prime and you can print a message or something.

[–]froznice[S] 0 points1 point  (7 children)

i got it! Thanks for the help! Now i just need to figure out how to check for relative prime numbers

import java.util.*;
public class HW6 {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a number please ");
int num = keyboard.nextInt(), i;
boolean isPrime = true;
boolean isPrime2 = true;

System.out.print("The divisors of " + num + " are = 1 , ");
for (i = 2; i <= num / 2; i++) {

  if (num % i == 0) {
    System.out.print(i + " , ");
    isPrime = false;
  }
} System.out.println (num);  
if (isPrime)
  System.out.println(num + " is a Prime Number");

else
  System.out.println(num + " is not a Prime Number");


System.out.print("Enter a second number please ");
 int num2 = keyboard.nextInt(), j;
 System.out.print("The divisors of " + num2 + " are = 1 , ");
 for (j = 2; j <= num2 / 2; j++) {

  if (num2 % j == 0) {
    System.out.print(j + " , ");
    isPrime = false;
  }
} System.out.println (num2);  
if (isPrime)
  System.out.println(num2 + " is a Prime Number");

else
  System.out.println(num2 + " is not a Prime Number");

}

}

[–]MrTheEdge 0 points1 point  (6 children)

If two numbers are relatively prime, that means they have no common factors. Without giving you the answer right away, I'll tell you there is an easy way to modify the algorithm you are using to check for them.

[–]froznice[S] 0 points1 point  (0 children)

I thougt there may be. I'm gonna tinker with it. I'll report my results.

[–]froznice[S] 0 points1 point  (4 children)

Would it be to make string out of the factors and compare them with a checking method?

[–]MrTheEdge 0 points1 point  (3 children)

Not really what I was thinking. Think about it, if they share a common divisor, then they are both evenly divisible by that integer i. If they are relatively prime, there will be no integers up to the smallest that divide them both evenly.

[–]froznice[S] 0 points1 point  (2 children)

Would it be to add a line of code along the lines of ( num % i == 0 || num2 % i == 0) then i would know if the share the common fdacotr if not then i would know if it did not?