#include <iostream>#include <vector>#include <math.h>using namespace std;vector <int> factors;int main(){ double num = 600851475143; for (int i=1; i<=num; i++) { if (fmod(num,i)==0) { factors.push_back(i); } } for (int i=0; i<factors.size(); i++) { if (sqrt(factor[i])) // ??? }}
有人可以告诉我如何通过我的if语句确定一个数字是否可以平方根?
解决方法int s = sqrt(factor[i]);if ((s * s) == factor[i])
正如霍布斯在评论中指出的那样,
Assuming that double is the usual 64-bit IEEE-754 double-precision float,for values less than 2^53 the difference between one double and the next representable double is less than or equal to 1. Above 2^53,the precision is worse than integer.
因此,如果您的int是32位,那么您是安全的.如果您必须处理大于2 ^ 53的数字,则可能会出现一些精度错误.
总结以上是内存溢出为你收集整理的c – 确定平方根是否为整数全部内容,希望文章能够帮你解决c – 确定平方根是否为整数所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)