I'm wrote a program to check if a number is a prime. I wanted to use the following method for checking to see if a number is prime: for number P take the factorial of p-1 then add 1 to your result. Finally divid the result by p. for dose of you that don't know, if the result is a whole number then its a prime.
anyways my code works up to prime number 167 but the gives me an NaN error for any number greater than 167.
can any body spot whats wrong?
import java.util.Scanner; public class work { public static void main(String arg[]) { System.out.println("Please enter a number to check if its a prime"); Scanner in = new Scanner( System.in ); int num = in.nextInt(); double t = 1; for (int i=1; i<num; i++){ t = i * t; } t = t+1; t = t / num; System.out.println(t%1); if ((t%1 ==0.0) && (t!= 2)){ System.out.println("it is prime"); } else{ System.out.println("it is not a prime"); } } }
doublecan be, right?t / numis whole or not. This method will fall over completely iftis an integral type - not just for very large numbers.