From 7421efa2cc1d46fa256ddebbefbb7fd8f0198eee Mon Sep 17 00:00:00 2001 From: Devin Rathnayake <54368055+devin989@users.noreply.github.com> Date: Fri, 8 Oct 2021 11:04:41 +0530 Subject: [PATCH] Create prime_num.java --- prime_num.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 prime_num.java diff --git a/prime_num.java b/prime_num.java new file mode 100644 index 0000000..98788a2 --- /dev/null +++ b/prime_num.java @@ -0,0 +1,22 @@ +public class Main { + + public static void main(String[] args) { + System.out.println(isPrime(5)); + } + public static boolean isPrime(int x){ + if( x <= 1) + return false; + else if( x ==2 ) + return true; + else if (x % 2 == 0) + return false; + + // If not, then just check the odds + for (int i = 3; i <= Math.sqrt(x); i += 2) + { + if (x % i == 0) + return false; + } + return true; + } +}