- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
不等式为:nlogn <= a(n为自然数,log以10为底)。问题:n 的最大值是多少?
我的解决方案是扫描 n = 1 到无穷大(第 1 步),直到到达 nlogn > a 的点。返回的结果将是 n - 1
但我发现当 a 非常大时,这样做效率不高。有没有人知道如何解决它?
最佳答案
我正确地为comingstorm的解决方案做了代数计算并实现了。在我的机器上,牛顿法比二进制搜索高 4 倍。我已经在所有非负 32 位整数上测试了 newton()
。
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <time.h>
static int newton(double a) {
if (a < 2.0 * log10(2.0)) {
return 1;
} else if (a < 3.0 * log10(3.0)) {
return 2;
}
double a_log_10 = a * log(10);
double x = a / log10(a);
x = (x + a_log_10) / (1.0 + log(x));
x = (x + a_log_10) / (1.0 + log(x));
double n = floor(x);
if (n * log10(n) > a) {
n--;
} else if ((n + 1.0) * log10(n + 1.0) <= a) {
n++;
}
return n;
}
static int binarysearch(double a) {
double l = floor(a / log10(a));
double u = floor(a) + 1.0;
while (1) {
double m = floor((l + u) / 2.0);
if (m == l) break;
if (m * log10(m) > a) {
u = m;
} else {
l = m;
}
}
return l;
}
static void benchmark(const char *name, int (*solve)(double)) {
clock_t start = clock();
for (int a = 1 << 22; a >= 10; a--) {
int n = solve(a);
assert(n * log10(n) <= a);
assert((n + 1) * log10(n + 1) > a);
}
printf("%s: %.2f\n", name, (clock() - start) / (double)CLOCKS_PER_SEC);
}
int main(int argc, char *argv[]) {
benchmark("newton", newton);
benchmark("binarysearch", binarysearch);
}
关于algorithm - 你如何有效地解决这个对数不等式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7455344/
所以我正在为“石头剪刀布蜥蜴 Spock”制作一个 Javascript 程序,并且使用 Math.random()。我需要使用if/else/else if's。有人可以帮我解决不平等问题吗?我
我们正在使用一个代码分析器,它具有这样的规则“不检查浮点相等性/不等性”。下面是给出的示例。 float f = 0.100000001f; // 0.1 double d = 0.100000000
我们正在使用一个代码分析器,它具有这样的规则“不检查浮点相等性/不等性”。下面是给出的示例。 float f = 0.100000001f; // 0.1 double d = 0.100000000
C++20 为我们提供了飞船操作符,甚至允许我们 default它,使用默认语义生成所有比较,这将从我们的代码库中删除很多样板,太棒了! 但是平等和不平等呢? C++20 是否也为我们提供了一种方式
我今天在写一些 Python 时意识到可以将不等运算符写成 a!=b 或 not a==b。这让我很好奇: 这两种方式的行为是否完全相同,还是存在一些细微差别? 是否有理由使用一个而不是另一个?一种比
假设我们有几个线性不等式,比如 2x-5y=0 ,我们如何绘制这两个不等式?为了扩展这一点,如果我们有多个这样的不等式,我们如何尝试以图形方式解决这个问题? 最佳答案 你可以试试ImplicitEqu
为什么以下简化 不起作用,或者如何解决: >>> x = Symbol('x', real=True) >>> y = Symbol('y', real=True) >>> simplify(x -
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
这个问题在这里已经有了答案: Draw 3d Inequality on Matlab (1 个回答) matlab - plot inequality in 3d with surf (1 个回答
考虑使用这两个表和以下查询: SELECT Product. * FROM Product WHERE EXISTS ( SELECT * FROM Codes
我在笔记本界面中使用 Mathematica 7,我想重新排列一个不等式,以便在一侧获得某个变量。例如。 FullSimplify[x^3+L+r>3x^3+2r] 给 L > r + 2 x^3 但
我是一名优秀的程序员,十分优秀!