如何专门在mathematica中编写该程序的代码?
我在 Mathematica 中实现了以下问题的解决方案,但计算 ki
的 f
或大数的集合 B 需要很长时间(几个小时)。
有人建议用 C++ 实现这一点,不到 10 分钟就能得到解决方案。 C++ 是学习解决这些问题的好语言吗?或者可以改进我的 Mathematica 代码来解决性能问题吗?
我对 C 或 C++ 一无所知,开始学习这些语言应该很困难。我更喜欢在 mathematica 中改进或编写新代码。
问题描述
令 $f$ 为算术函数并且 A={k1,k2,...,kn} 是整数 增加订单。
现在我想从 k1 开始并且 将 f(ki) 与 f(k1) 进行比较。如果 f(ki)>f(k1),设ki为k1。
现在从 ki 开始,比较 f(kj) 其中 f(ki),对于 j>i。如果f(kj)>f(ki), 将 kj 设为 ki,然后重复此操作 程序。
最后我们将得到一个子序列 A 的 B={L1,...,Lm} 通过此属性: f(L(i+1))>f(L(i)),对于任意 1<=i<=m-1
例如,令 f 为除数 整数函数。
在这里,我放置了我的代码的一部分,这只是一个示例,我的程序中的问题可能比这些更大:
««««««««««««««««««««««««««««««««««««
f[n_] := DivisorSigma[0, n];
g[n_] := Product[Prime[i], {i, 1, PrimePi[n]}];
k1 = g[67757] g[353] g[59] g[19] g[11] g[7] g[5]^2 6^3 2^7;
k2 = g[67757] g[353] g[59] g[19] g[11] g[7] g[5] 6^5 2^7;
k3 = g[67757] g[359] g[53] g[19] g[11] g[7] g[5] 6^4 2^7;
k4 = g[67759] g[349] g[53] g[19] g[11] g[7] g[5] 6^5 2^6;
k5 = g[67757] g[359] g[53] g[19] g[11] g[7] g[5] 6^4 2^8;
k6 = g[67759] g[349] g[53] g[19] g[11] g[7] g[5]^2 6^3 2^7;
k7 = g[67757] g[359] g[53] g[19] g[11] g[7] g[5] 6^5 2^6;
k8 = g[67757] g[359] g[53] g[19] g[11] g[7] g[5] 6^4 2^9;
k9 = g[67757] g[359] g[53] g[19] g[11] g[7] g[5]^2 6^3 2^7;
k10 = g[67757] g[359] g[53] g[19] g[11] g[7] g[5] 6^5 2^7;
k11 = g[67759] g[349] g[53] g[19] g[11] g[7] g[5]^2 6^4 2^6;
k12 = g[67757] g[359] g[53] g[19] g[11] g[7] g[5]^2 6^3 2^8;
k13 = g[67757] g[359] g[53] g[19] g[11] g[7] g[5]^2 6^4 2^6;
k14 = g[67757] g[359] g[53] g[19] g[11] g[7] g[5]^2 6^3 2^9;
k15 = g[67757] g[359] g[53] g[19] g[11] g[7] g[5]^2 6^4 2^7;
k16 = g[67757] g[359] g[53] g[23] g[11] g[7] g[5] 6^4 2^8;
k17 = g[67757] g[359] g[59] g[19] g[11] g[7] g[5] 6^4 2^7;
k18 = g[67757] g[359] g[53] g[23] g[11] g[7] g[5] 6^4 2^9;
k19 = g[67759] g[353] g[53] g[19] g[11] g[7] g[5] 6^4 2^6;
k20 = g[67763] g[347] g[53] g[19] g[11] g[7] g[5] 6^4 2^7;
k = Table[k1, k2, k3, k4, k5, k6, k7, k8, k9, k10, k11, k12, k13, k14, k15, k16, k17, k18, k19, k20];
i = 1;
count = 0;
For[j = i, j <= 20, j++,
If[f[k[[j]]] - f[k[[i]]] > 0, i = j; Print["k",i];
count = count + 1]];
Print["count= ", count]
《》《》《《》《》《》《》《》《》
结果是:
k2
k5
k7
k8
k9
k10
k12
k13
k14
k15
k16
k17
k18
计数=13
««««««««««««««««««««««««««««««««««««
I implemented a solution to the problem below in Mathematica, but it takes a very long time (hours) to compute f
of ki
s or the set B for large numbers.
Somebody suggested that implementing this in C++ resulted in a solution in less than 10 minutes. Would C++ be a good language to learn to solve these problems, or can my Mathematica code be improved to fix the performance issues?
I don't know anything about C or C++ and it should be difficult to start to learn this languages. I prefer to improve or write new code in mathematica.
Problem Description
Let $f$ be an arithmetic function and
A={k1,k2,...,kn} are integers in
increasing order.Now I want to start with k1 and
compare f(ki) with f(k1). If
f(ki)>f(k1), put ki as k1.Now start with ki, and compare f(kj)
with f(ki), for j>i. If f(kj)>f(ki),
put kj as ki, and repeat this
procedure.At the end we will have a sub sequence
B={L1,...,Lm} of A by this property:
f(L(i+1))>f(L(i)), for any 1<=i<=m-1For example, let f is the divisor
function of integers.
Here I put some part of my code and this is just a sample and the question in my program could be more larger than these:
««««««««««««««««««««««««««««««««««««
f[n_] := DivisorSigma[0, n];
g[n_] := Product[Prime[i], {i, 1, PrimePi[n]}];
k1 = g[67757] g[353] g[59] g[19] g[11] g[7] g[5]^2 6^3 2^7;
k2 = g[67757] g[353] g[59] g[19] g[11] g[7] g[5] 6^5 2^7;
k3 = g[67757] g[359] g[53] g[19] g[11] g[7] g[5] 6^4 2^7;
k4 = g[67759] g[349] g[53] g[19] g[11] g[7] g[5] 6^5 2^6;
k5 = g[67757] g[359] g[53] g[19] g[11] g[7] g[5] 6^4 2^8;
k6 = g[67759] g[349] g[53] g[19] g[11] g[7] g[5]^2 6^3 2^7;
k7 = g[67757] g[359] g[53] g[19] g[11] g[7] g[5] 6^5 2^6;
k8 = g[67757] g[359] g[53] g[19] g[11] g[7] g[5] 6^4 2^9;
k9 = g[67757] g[359] g[53] g[19] g[11] g[7] g[5]^2 6^3 2^7;
k10 = g[67757] g[359] g[53] g[19] g[11] g[7] g[5] 6^5 2^7;
k11 = g[67759] g[349] g[53] g[19] g[11] g[7] g[5]^2 6^4 2^6;
k12 = g[67757] g[359] g[53] g[19] g[11] g[7] g[5]^2 6^3 2^8;
k13 = g[67757] g[359] g[53] g[19] g[11] g[7] g[5]^2 6^4 2^6;
k14 = g[67757] g[359] g[53] g[19] g[11] g[7] g[5]^2 6^3 2^9;
k15 = g[67757] g[359] g[53] g[19] g[11] g[7] g[5]^2 6^4 2^7;
k16 = g[67757] g[359] g[53] g[23] g[11] g[7] g[5] 6^4 2^8;
k17 = g[67757] g[359] g[59] g[19] g[11] g[7] g[5] 6^4 2^7;
k18 = g[67757] g[359] g[53] g[23] g[11] g[7] g[5] 6^4 2^9;
k19 = g[67759] g[353] g[53] g[19] g[11] g[7] g[5] 6^4 2^6;
k20 = g[67763] g[347] g[53] g[19] g[11] g[7] g[5] 6^4 2^7;
k = Table[k1, k2, k3, k4, k5, k6, k7, k8, k9, k10, k11, k12, k13, k14, k15, k16, k17, k18, k19, k20];
i = 1;
count = 0;
For[j = i, j <= 20, j++,
If[f[k[[j]]] - f[k[[i]]] > 0, i = j; Print["k",i];
count = count + 1]];
Print["count= ", count]
««««««««««««««««««
the result is:
k2
k5
k7
k8
k9
k10
k12
k13
k14
k15
k16
k17
k18
count=13
««««««««««««««««««««««««««««««««««««
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
代码中的大部分时间都花在 DivisorSigma 上,因为它需要对整数进行因式分解。但它只需要对它们进行因式分解,因为你已经将它们相乘并丢失了信息。
但解决问题的直接方法是预先计算 f[k[[i]]]。
Most of the time in your code is spent in DivisorSigma because it needs to factor your integers. But it only needs to factor them because you have already multiplied them together and lost the information.
But immediate fix for your problem is to precompute f[k[[i]]].