O(n^1/2) isPrime
来源:互联网 发布:云数据库语言 编辑:程序博客网 时间:2024/06/05 23:59
/** O(n^0.5) solution * Only need to consider if n can be divided by numbers from 2 to sqrt(n). * If n % even == 0, not prime; * If n % odd == 0, not prime; * 1 is not a prime number; * 2 is a prime number. */ public static boolean isPrime(int n) { // 2 is prime number if (n == 2) { return true; } // 1 is not prime number, even number greater than 2 is not prime number if (n == 1 || (n & 1) == 0) { return false; } // Starts from 3 to sqrt(n), check if n can be divied by odd numbers for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; }
0 0
- O(n^1/2) isPrime
- isPrime
- isPrime
- isprime
- isPrime
- o(1), o(n), o(logn), o(nlogn)
- 摩尔投票法, O(n) O(1) 求出现次数大于n/2,n/3次数的数
- O(N)求1~N逆元
- Edit Distance (O(n^2) and O(n) space)
- C++ Fibonacci数列 O(2^n) 和 O(n)解法
- 冒泡排序 O(n^2)
- 选择排序 O(n^2)
- 插入排序 O(n^2)
- O(n^2)排序算法
- LCIS O(n ^ 2) 详解
- 时间复杂度:O(1)、O(n)、O(n²)、O(nlogn)等是什么意思,白话文解释专业术语。
- 算法时间复杂度的表示法O(n²)、O(n)、O(1)、O(nlogn)
- O(n^3)、O(n^2)和O(n)求最长回文子串
- tar解压失败:gzip: stdin: not in gzip format
- 【codeforces 785A】Anton and Polyhedrons
- 【codeforces 785B】Anton and Classes
- 【codeforces 785C】Anton and Fairy Tale
- Coursera ML笔记 -----week3 Logistic Regression
- O(n^1/2) isPrime
- UC_COS移植到STM32
- 算法导论-归并排序
- [Amazon] Reverse Words in a Stri
- 数据结构:二叉树
- ceph-deploy出错UnableToResolveError Unable to resolve host
- string库之模拟实现strcmp、strncmp和memcmp
- Unity5.x AssetBundle 的变化
- Cookie实现 购物车