- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
完成 100,000 的限制(n)需要相当长的时间。
我怀疑问题出在 CalculateAmicable() 上,其中数字变大并且需要更多时间来计算。我可以更改什么以使其速度比这更快?
public static void Main (string[] args)
{
CheckAmicable (1, 100000);
}
public static int CheckAmicable(int start, int end)
{
for (int i = start; i < end; i++) {
int main = CalculateAmicable (i); //220
int temp = CalculateAmicable (main); //284
int compare = CalculateAmicable (temp); //220
if (compare == main) {
if (main != temp && temp == i) {
Console.WriteLine (main + " = " + temp + " = " + compare + " i: " + i);
i = compare + 1;
}
}
}
return 0;
}
public static int CalculateAmicable(int number)
{
int total = 0;
for (int i = 1; i < number; i++) {
if (number%i == 0){
total += i;
}
}
return total;
}
注意:英语不是我的母语!
最佳答案
是的,CalculateAmicable
是低效 - O(N)
复杂度 - 所以你有 fo N
测试1e5 * 1e5 == 1e10
- 百亿 操作很慢。将复杂度降低到 O(log(N))
的方案是
int n = 100000;
//TODO: implement IList<int> GetPrimesUpTo(int) yourself
var primes = GetPrimesUpTo((int)(Math.Sqrt(n + 1) + 1));
// Key - number itself, Value - divisors' sum
var direct = Enumerable
.Range(1, n)
.AsParallel()
.ToDictionary(index => index,
index => GetDivisorsSum(index, primes) - index);
var result = Enumerable
.Range(1, n)
.Where(x => x < direct[x] &&
direct.ContainsKey((int) direct[x]) &&
direct[(int) direct[x]] == x)
.Select(x => $"{x,5}, {direct[x],5}");
Console.Write(string.Join(Environment.NewLine, result));
结果在一秒钟之内(Core i7 3.2Ghz .Net 4.6 IA-64):
220, 284
1184, 1210
2620, 2924
5020, 5564
6232, 6368
10744, 10856
12285, 14595
17296, 18416
63020, 76084
66928, 66992
67095, 71145
69615, 87633
79750, 88730
详细信息GetDivisorsSum
:
private static long GetDivisorsSum(long value, IList<int> primes) {
HashSet<long> hs = new HashSet<long>();
IList<long> divisors = GetPrimeDivisors(value, primes);
ulong n = (ulong) 1;
n = n << divisors.Count;
long result = 1;
for (ulong i = 1; i < n; ++i) {
ulong v = i;
long p = 1;
for (int j = 0; j < divisors.Count; ++j) {
if ((v % 2) != 0)
p *= divisors[j];
v = v / 2;
}
if (hs.Contains(p))
continue;
result += p;
hs.Add(p);
}
return result;
}
和GetPrimeDivisors
:
private static IList<long> GetPrimeDivisors(long value, IList<int> primes) {
List<long> results = new List<long>();
int v = 0;
long threshould = (long) (Math.Sqrt(value) + 1);
for (int i = 0; i < primes.Count; ++i) {
v = primes[i];
if (v > threshould)
break;
if ((value % v) != 0)
continue;
while ((value % v) == 0) {
value = value / v;
results.Add(v);
}
threshould = (long) (Math.Sqrt(value) + 1);
}
if (value > 1)
results.Add(value);
return results;
}
关于c# - 如何加快 Amicable 号码算法的速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41653159/
我想使用 ffmpeg 框架更改视频速度。我为此使用了这个命令: ffmpeg -y -i /storage/extSdCard/Video/1.avi -filter_complex [0:v]fp
我有以下数据数组,有 200 万个条目: [20965 1239 296 231 -1 -1 20976 1239 299 314 147 337 255
我正在使用 Oracle 数据库,并且想获取一个包含 3000 万条记录的表。 library(RODBC) ch <- odbcConnect("test", uid="test_user",
我在 android 上使用 FFmpeg 来: 1- 合并 3 个视频 2-添加音频 3-添加标志 4-修剪 3 个视频之一 5-改变输出的fps 我已经实现了正确的代码,但花了 30 分钟。对于(
我使用 GLPKMathProgInterface 和 JuMP 编写了一个程序来解决 Julia 中的线性程序。 Julia 代码由 python 程序调用,该程序通过多个命令行调用运行多个 Jui
我们使用 POV-Ray 每次运行生成大约 80 张图像,我们将这些图像拼接在一起形成两个移动的 GIF 文件(一个场景的两个 360 度 View )。我们正在寻找尽可能加快此镜像创建的方法(在 h
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我将数据从一个数据库插入到另一个数据库,所以我有 2 个连接(Conn1 和 Conn2)。下面是代码(使用pypyodbc)。 import pypyodbc Conn1_Query = "SE
在我的应用程序中,我显示 EKEvents 列表,我想在 UITableView 中显示一个月的所有事件,每个部分包含各自的日期。嗯,这可行,我得到了我需要的所有数据,但获取速度非常慢。 问题在于事件
我有一个移动速度非常慢的传送带。我不知道什么JS脚本控制速度,我需要它来加速。无法从主题制作者那里获得任何帮助。任何建议都会非常有帮助。谢谢 页面: http://krankgolf2017.wpen
有没有办法加快这段代码的速度?我需要它来删除相同的内容并将其写入单元格,以强制其他 VBA 代码运行另一列上的代码。这就是它的作用,只是 super 慢。有时此表上有 2000 个条目/行。每个单元大
我正在开发一个相当大的程序,它再次从一个相当大的 Excel 电子表格中获取数据。由于一些奇怪的原因,加载这个大的 Excel 文件需要很长时间,我希望能以某种方式加快速度。我做了自己的研究并尝试了
我有下面的代码,将所有按钮(有 10 个)着色为灰色,以清除任何先前着色的按钮,然后将所选按钮着色为蓝色。基本上充当当前选择哪个按钮的指示器。我注意到代码现在需要一些时间才能通过这种修饰添加来运行,我
我有一个 LINQ 查询,它正在搜索包含大约 250,000 条记录的 SQL 表,并且仅搜索 2 个字段。这两个字段都已建立索引,但我发现它的运行速度仍然相当慢。 下面是代码,有人可以提出任何建议来
对于相对较大的 Pandas DataFrame(几十万行),我想创建一个应用函数结果的系列。问题是该功能不是很快,我希望它能以某种方式加快速度。 df = pd.DataFrame({ 'valu
这个问题在这里已经有了答案: Faster weighted sampling without replacement (3 个答案) 关闭 9 年前。 如何在 R 中加快概率加权采样。 # Let
在运行 PhantomJS 提供的 rasterize.js 示例时,我发现我必须等待 20 秒或更长时间才能生成网页图像。 有没有可能在不消耗大量资源的情况下加快速度的方法?我基本上希望快速生成从加
我正在开发一个相当大的程序,它再次从一个相当大的 Excel 电子表格中获取数据。由于一些奇怪的原因,加载这个大的 Excel 文件需要很长时间,我希望能以某种方式加快速度。我做了自己的研究并尝试了
我有下面的代码,将所有按钮(有 10 个)着色为灰色,以清除任何先前着色的按钮,然后将所选按钮着色为蓝色。基本上充当当前选择哪个按钮的指示器。我注意到代码现在需要一些时间才能通过这种修饰添加来运行,我
我有一个 Excel 工作簿,用户通过单击按钮导入文本文件。我的代码完全按照我的需要工作,但是在填写 H 列“阅读日期”时速度非常慢。将文本文件导入 Excel 工作表后,我的 Excel 工作簿如下
我是一名优秀的程序员,十分优秀!