- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
找出给定段 [low,high] 内的理想整数数。理想数是只有 3 和 5 作为质数除数的正整数。一个理想的数字可以用 3^x*5^y 的形式表示,其中 x 和 y 是非负整数。例如,15,45 和 75 是理想数,但 6,10,21 不是。
我认为这个算法是 O(n*m);
我们可以将其实现为 O(n+m)
static long getIdealNums(long low, long high) {
int count = 0; // counter
for (long i = low; i <= high; i++) {
long num = i;
// While num is divisible by 3, divide it by 3
while (num % 3 == 0)
num /= 3;
// While num is divisible by 5, divide it by 5
while (num % 5 == 0)
num /= 5;
// If num got reduced to 1 then it has
// only 3 and 5 as prime factors
if (num == 1)
count++;
}
return count;
}
最佳答案
我继续解决这个问题。这是我想出的解决方案,但是我不能保证它对所有可能的值都能正常工作;)。
喜欢 @JoopEggen
描述了它更快地迭代已知的理想数。
在下面的例子中,我试图找出 x
的有效范围对于固定值 y
.
public static double logb(double a, double b) {
return (a == 0) ? 0 : Math.log(a) / Math.log(b);
}
public static int getIdealNums(long low, long high) {
int y = 0;
int maxY = (int) Math.floor(logb(high, 5));
int count = 0;
do {
long exp = (long) Math.pow(5, y);
int min = (int) Math.ceil(logb(low / exp, 3));
int max = (int) Math.floor(logb(high / exp, 3));
if (min <= max) {
count += max-min+1;
}
} while (++y <= maxY);
return count;
}
Math.log
有点挣扎返回
-Infinity
,所以如果
a
我只是返回 0 而不是最终是
0
因为精度不够。
min
范围内没有有效的理想数可能大于
max
.我想知道这是否可以用于提前终止,但我想它可以忽略不计。
关于java - 有没有另一种算法来实现这个功能(求理想整数的个数。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59286523/
从许多帖子中我可以读到有关最大池大小的公式,它取决于 CPU 的数量 + 1。这是一个明确的解释。 但是如果应用程序定义了许多执行器或线程池执行器怎么办。然后我们还必须考虑应用程序共享相同的硬件。这对
我一直在寻找垂直 Accordion 内容 slider 。我找到了一个:http://www.marghoobsuleman.com/jQuery-common-accordion ,而且还不错。我
在 not having a whole bunch of luck 之后找到任何可充当 SNMP 代理的 .NET SNMP 库(可在 OSS 项目中使用 - 这意味着专有库已经过时),我现在正在考
目前我正在考虑: VMWare 管理程序 ESXi 中央操作系统 5.5 内存缓存 1.4.5和依赖 这就是全部。我还需要什么/任何替代品吗? 最佳答案 Memcached在linux上运行的很好,所
我是一名优秀的程序员,十分优秀!