
思路:欧几里得算法
【欧几里得演算法(辗转相除法)】 https://www.bilibili.com/video/BV19r4y127fu/?share_source=copy_web&vd_source=d124eda224bf54d0e3ab795c0b89dbb0

- class Solution {
- public int findGCD(int[] nums) {
- int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
- for (int num : nums) {
- if (num > max) {
- max = num;
- }
- if (num < min) {
- min = num;
- }
- }
- return gcd(max,min);
-
- }
-
- public int gcd(int a, int b) {
- return b == 0 ? a : gcd(b,a%b);
- }
- }
素数是指2开始,除了1和它本身之外没有能整除它的数
合数是指2开始,除了1和它本身之外有能整除它的数

思路:第一种循环暴力
- class Solution {
- public int countPrimes(int n) {
- int count = 0;
- if(n>2){
- count++;
- }else{
- return 0;
- }
- for(int i = 3;i
- if(isPrime(i)){
- count++;
- }
- }
- return count;
- }
-
- public boolean isPrime(int n){
- double N = Math.sqrt(n);
- for(int i = 2;i<=N;i++){
- if(n%i == 0){
- return false;
- }
- }
- return true;
- }
- }
2)埃氏筛
当确定一个数为素数则它的n倍数都不是素数

注意的是每次筛选从i*i开始,因为这是最小的未被筛选过的,例如3开始筛选3的1,2,3,4,5...倍数都被筛选过了,那么5开始筛选就得从5*5开始
- class Solution {
- public int countPrimes(int n) {
- int count = 0;
- int[] nums = new int[n];
- for(int i = 2;i
- if(nums[i] == 0){
- count++;
- if((long) i*i
- for(int j = i*i;j
- nums[j] = 1;
- }
- }
- }
- }
- return count;
- }
- }
3.丑数

思路:可以知道一个数是丑数那么,n = 2^a+3^b+5^c成立
- class Solution {
- public boolean isUgly(int n) {
- if(n == 0){
- return false;
- }
- int[] elements = {2,3,5};
- for(int e : elements){
- while(n%e == 0){
- n = n/e;
- }
- }
- if(n == 1){
- return true;
- }else{
- return false;
- }
- }
- }
-
相关阅读:
嵌入式Linux运行一定需要MMU吗
《C和指针》笔记29:数组名和指针
超分辨率技术在实时音视频领域的研究与实践
LazySnapping算法详解
word模板内容替换
[附源码]java毕业设计网吧购物系统
集合数据丢失distinct与EqualsAndHashCode的Bug问题
毕设 JAVA.JSP花店管理系统
macOS 安装brew和ffmpeg
Fast DDS之Subscriber
-
原文地址:https://blog.csdn.net/Candy___i/article/details/132888237
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU