Floating point 检查数字是否为“0”时浮点数的标准化;调和除数(Ore数)“;

Floating point 检查数字是否为“0”时浮点数的标准化;调和除数(Ore数)“;,floating-point,exponent,normalize,mantissa,Floating Point,Exponent,Normalize,Mantissa,我的函数isOre是检查数字是否为“调和除数(Ore数)” (). 我无法正确地比较“结果变量”是否为整数,因为它的部分在指数内。() 我的问题是: 1.我怎样才能解决它? 2.有其他的解决办法吗 #include <iostream> using namespace std; bool isOre(unsigned int n){ double numbersSum = 0; int number = 0; for (int i = 1; i <= n; ++i){

我的函数isOre是检查数字是否为“调和除数(Ore数)” (). 我无法正确地比较“结果变量”是否为整数,因为它的部分在指数内。()

我的问题是: 1.我怎样才能解决它? 2.有其他的解决办法吗

#include <iostream>
using namespace std;

bool isOre(unsigned int n){
double numbersSum = 0;
int number = 0;
for (int i = 1; i <= n; ++i){
    if (n % i == 0){
        numbersSum += (double)1 / i ;
        //cout << "numbersSum " << numbersSum << endl;
        ++number;
        //cout << "number " << number << endl;

    }
}

double result = number / numbersSum;
int result1 = result;


cout << "       " << result << "        " << result1 << "       ";
if (result == result1) return true;
else return false;
}

int main() { 
//Test sequence of Ore numbers from https://oeis.org/A001599
int array[34] = {1, 6, 28, 140, 270, 496, 672, 1638, 2970, 6200, 8128,
8190, 18600, 18620, 27846, 30240, 32760, 55860, 105664, 117800, 167400,
173600, 237510, 242060, 332640, 360360, 539400, 695520, 726180, 753480, 950976,
1089270, 1421280, 1539720};

cout << "array[i]" << " " << "double" << "  " << "toInt" << "   " << "Result" << endl;

for (int i = 0; i < 34; ++i){
cout << array[i];
cout << isOre(array[i]) << endl;
}

return 0;
}
#包括
使用名称空间std;
布尔isOre(无符号整数n){
双倍数字总和=0;
整数=0;

对于(int i=1;i,正如您参考的页面所述,n的除数的调和平均值可以表示为n•(σ0(n)/σ1(n),其中σ0(n)是除数的数量,σ1(n)是除数之和。因此,只需使用整数算术计算分子和分母,然后测试除法的剩余部分是否为零

请注意,算术将因大数而崩溃,因此您的程序必须对此进行测试,并且在无法继续时停止,或者必须使用其他算术方法来支持大数。整数或浮点算术都是如此。但是,如果您逐个迭代候选数并使用64位整数r算术,您将不会达到溢出是一个问题的程度