开发者

how to write the code for this program specially in mathematica?

开发者 https://www.devze.com 2023-02-07 13:03 出处:网络
I implemented a solution to the problem below in Mathematica, but it takes a very long time (hours) to compute f of kis or the set B for large numbers.

I implemented a solution to the problem below in Mathematica, but it takes a very long time (hours) to compute f of kis 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-1

For 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

««««««««««««««««««««««««««««««««««««


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]]].

k = List[k1, k2, k3, k4, k5, k6, k7, k8, k9, k10, k11, k12, k13, k14, 
   k15, k16, k17, k18, k19, k20];
fk = ParallelMap[f, k]; (* precompute *)

i = 1;
count = 0;
For[j = i, j <= 20, j++, 
  If[fk[[j]] - fk[[i]] > 0, i = j; Print["k", i];
   count = count + 1]];
Print["count= ", count]
0

精彩评论

暂无评论...
验证码 换一张
取 消