假设我们有一个合数(n>3),可以写成:
现在,我们的任务是计算a和b的值,使函数
我已经实现了如下方法。
但希望将其减少到 或更好的情况下,如果可能的话。
n = a*b
,其中a和b是任意整数。现在,我们的任务是计算a和b的值,使函数
f(a,b) = |a-b|
最小化。我已经实现了如下方法。
int n;
cin >> n; // Take it from the user
/* Now, find the value of the a and b */
int a = 1;
int b = n;
int temp_a;
int temp_b;
for(temp_a=1; temp_a<=sqrt(n); temp_a++) {
if(n % temp_a == 0) {
temp_b = n / temp_a;
if((temp_b - temp_a) < (b - a)) {
b = temp_b;
a = temp_a;
}
}
}
print a and b
但希望将其减少到 或更好的情况下,如果可能的话。