1.判断素数
如果它不是素数,那么它一定可以表示成两个数(除了1和它本身)相乘,这两个数必然有一个小于等于它的平方根。所以我们循环判断是否存在不大于平方根的那个数,若存在,则不是素数。
eg:16=44=28
2<根号16=4
代码:
bool IsPrimeNumber(int n)
{
if(n <= 1)
return false;
for(int i = 2 ; i < sqrt(n) ; i++)
{
if(n % i == 0)
return false;
}
return true;
}
2.向上取整+向下取整+四舍五入
转自:https://www.yuque.com/docs/share/2301581d-74ab-47e3-adb8-7ea8f3e4bf6d
3.log函数
首先要知道exp()函数
exp(n)值为e^n次方;
另外log函数包括两种函数 一种以e为低的log()函数
另一种为以10为底的log 10()函数;
具体用法见下面这个小程序
#include
#include
using namespace std;
int main()
{
double a=9,b=10;
cout<<log(a)<<endl;
cout<<log(exp(a))<<endl;
cout<<log10(b)<<endl;
return 0;
}
另外如果自定义以m为底,求log n的值
需要double a=log(n)/log(m);
举例如下:
#include
#include
using namespace std;
int main()
{
double a=2,b=2;//以2为底的对数函数
for(b=2;b<=16;b=b+2)
{
cout<<“b=”<<b<<“时,以2为底的对数函数=”<<log(b)/log(a)<<endl;
}
return 0;
}