From ef74e4045628399275386dfddef4e95374238efe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=96=87=E8=B5=B5?= <1392936858@qq.com> Date: Sat, 25 Apr 2020 23:49:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A4=E6=96=AD=E6=98=AF=E5=90=A6=E6=98=AF?= =?UTF-8?q?=E8=B4=A8=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../algorithmsfourthedition/Chapter01.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/main/java/com/ximo/datastructuresinaction/algorithmsfourthedition/Chapter01.java diff --git a/src/main/java/com/ximo/datastructuresinaction/algorithmsfourthedition/Chapter01.java b/src/main/java/com/ximo/datastructuresinaction/algorithmsfourthedition/Chapter01.java new file mode 100644 index 0000000..88f96fc --- /dev/null +++ b/src/main/java/com/ximo/datastructuresinaction/algorithmsfourthedition/Chapter01.java @@ -0,0 +1,41 @@ +package com.ximo.datastructuresinaction.algorithmsfourthedition; + +/** + * @author xikl + * @date 2020/4/25 + */ +public class Chapter01 { + + public static boolean isPrime(int n) { + // 大于1的数字才是 + if (n < 2) { + return false; + } + + // 2 和 3 本身只有1和本身 + if (n == 2 || n == 3) { + return true; + } + + // 排除偶数 + if (n % 2 == 0) { + return false; + } + + // 根据定理 + // 避免求根号,改为平方 + for (int i = 2; i * i <= n; i++) { + if (n % i == 0) { + return false; + } + } + + return true; + } + + public static void main(String[] args) { + System.out.println(isPrime(13)); + } + + +}