当前位置:网站首页>找100以内的素数,求约数,求两个数的最大公约数
找100以内的素数,求约数,求两个数的最大公约数
2022-07-22 00:26:00 【Cola_Foever】
1.100内的素数
bool isPrime(int num)
{
int tmp = sqrt(num);
//只遍历到开方出,不能被开方左边的数整除,则一定不能被开方右边的数整除
for (int i = 2; i <= tmp; i++)
{
if (num %i == 0)
return false;
}
return true;
int main()
{
int n = 100;
for(int i = 0 ; i <= 100 ; i++)
{
if(ifPrime(i))
{
//....
}
}
}
2.求一个数除了1和本身的约数
void GetDiv(vector<int> &div, int num)
{
for(int i = 2 ; i <= sqrt(num) ; i++)
{
if(num % i == 0)
{
div.push_back(i);
//以16为例 当2满足了, 需要判断16/2 即8是否满足,满足就存起来。但如果是4这种情况只需存一次就可以
if(num / i != i)
{
div.push_back(num / i);
}
}
}
}
3.求两个数的最大公约数
用大数对小数求余,若余数为0,则除数为最大公约数。若余数不为0,将此余数作为除数,小数作为被除数,重新求余,直到余数为0为止。此时的最大公约数为余数。
//辗转相除法
int GCD(int a, int b)
{
if(a<b)
swap(a,b);
int c;
while(c = a % b)
{
a = b;
b = c;
}
return b;
}
边栏推荐
- Network planning - application layer
- Regular delete all elements after a specific character (case insensitive)
- 易基因 | 简化基因组DNA甲基化测序(RRBS)实验怎么做?
- Prometheus 2.37.0 new features
- Simple message queue implementation nodejs + redis =mq
- 飞行员兄弟
- 并发开篇——带你从0到1建立并发知识体系的基石
- Interview 3 (multiple processes call the same dynamic library)
- Make a reliable delay queue with redis
- 8. ZABBIX distributed
猜你喜欢
4.部署agent2
DOM -- event chain (capture target and capture)
Interview project preparation | how to show the project to the interviewer?
After reading this, I can't DVMA yet. Please eat melon
The 22 pictures show you in-depth analysis of prefix, infix, suffix expressions and expression evaluation
Yolov7 experiment test II: remote sensing image detection application (yolov7 tiny silu.yaml)
文件操作下(C语言)
NVIDIA可编程推理加速器TensorRT学习笔记(一)——开始
Load and display the label information of raincityspaces
Http实战之缓存、重定向
随机推荐
Simple message queue implementation nodejs + redis =mq
MySQL advanced (c)
Under file operation (C language)
C language: leak detection and filling (II)
[take you to the cloud native system] Part 4: kubernetes from entry to mastery
JUC concurrent contracting
Add Chinese and other fonts to R plot [showtext]
R Plot添加中文及其他字体【showtext】
Anomaly detection and self encoder (2)
飞行员兄弟
Salted fish self rescue guide -- typescript about interfaces
RenderFlex children have non-zero flex but incoming height constraints are unbounded.
MySQL调优(D)
JUC no lock
Go language go command line tool
Go语言 Go命令行工具
Network counting -- network layer
实习打怪之路:ES6中的Sym详解
dom——事件代理
Seven schemes of Web real-time message push