当前位置:网站首页>Explanation and usage of sqrt() function
Explanation and usage of sqrt() function
2022-06-28 21:12:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
sqrt() Function is a function we often use . Now I will introduce its usage and usage specification in detail
Required header file
#include<math.h>The function prototype
double sqrt(double x);effect : sqrt() Used to find the square root of a given value
Common usage errors Output 36 The root of the root
Ignored sqrt() The return value of the function is double type . Make a mistake
The solution is as follows :
Common use sqrt() The standard writing of functions for example : We have to judge whether a number is a prime number , Just judge 2 ~ n Square root If there is a number that can be divided by an integer The wrong way to write :
bool find(int n)
{
for(int i=2;i<=sqrt(n);i++)
{
if(n%i==0)
return false;
}
return true;
}The way it's written is , Actually, it is not recommended to write in this way . Although I often write like this . But in the process of doing questions , Just because of this writing program appeared bug. I'll use the following safe way of writing Correct writing :
bool find(int n)
{
int sql=(int)sqrt(1.0*n);//1.0*n Is the purpose of Implicit conversion to floating point number , Cast to integer after root
for(int i=2;i<=sql;i++)
{
if(n%i==0)
return false;
}
return true;
}Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/132640.html Link to the original text :https://javaforall.cn
边栏推荐
- [Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)
- Is the rapid SSL wildcard certificate genuine in 1981
- How to add logs to debug anr problems
- Leetcode56. consolidation interval
- LeetCode:合并K个升序链表_23
- Is it safe to open a dig money account? Is it reliable?
- No module named ‘PyEMD‘ ;使用plt.figure()TypeError: ‘module‘ object is not callable
- openGauss内核分析之查询重写
- 【筆記:模擬MOS集成電路】帶隙基准(基本原理+電流模+電壓模電路詳解)
- MySQL system error occurred 1067
猜你喜欢

什么是接口?什么是接口测试?

我也差点“跑路”

如何使用 DataAnt 监控 Apache APISIX

Bitbucket 使用 SSH 拉取仓库失败的问题

Mongodb - replica set and sharding

Application practice | 1billion data second level correlation. Huolala's OLAP System Evolution Based on Apache Doris (with PPT download)
![[try to hack] cobalt strike (I)](/img/2b/5d274078b7d7ebd05b7c6d9e020868.png)
[try to hack] cobalt strike (I)

How do I download videos? Look at the super simple method!
How to recover after Oracle delete accidentally deletes table data

Interface test process
随机推荐
Résumé de la stabilité
[book club issue 13] packaging format of video files
rapid ssl通配符证书八百一年是正版吗
LeetCode116. 填充每个节点的下一个右侧节点指针
LeetCode:合并K个升序链表_23
GlobalSign的泛域名SSL证书
No module named ‘PyEMD‘ ; Use plt figure()TypeError: ‘module‘ object is not callable
职场小技巧 | 了解岗位优势三板斧之“识人”
LeetCode226. Flip binary tree
LeetCode188. 买卖股票的最佳时机IV
Leetcode daily question - 324 Swing sort II
ref属性,props配置,mixin混入,插件,scoped样式
ID access card copied to mobile phone_ How to turn a mobile phone into an access card mobile NFC copy access card graphic tutorial
LeetCode226. 翻转二叉树
postman简介与安装步骤
认识Web自动化测试
软件watchdog和ANR触发memory dump讲解
Web automation tool selection
二叉树类题目 力扣
LeetCode213. House raiding II