w[n_] := Expand[Sum[Binomial[n - k - 1, k]*(-1)^k*A^(n - 2*k - 1), {k, 0, n - 1}]]
f[x_, y_, z_] :=PolynomialRemainder[(w[z] - 1)*(w[y] - 1), (w[x] - 1), A]
For[i = 3, i < 450, i++,For[j = 3, j < 450, j++,For[k = 3, k < 450, k++,If[i < j < k,
Print[{i, j, k},
N[Max[FindInstance[{Abs[f[i, j, k]] - Abs[w[i] - 1]} == 0 &&
3 <= A, {A}]]] ]]] ]]
Above code gives max A for all each cases. My aim is to find max A for all cases.
Q2: How can i reduce the time for above code? Thank you.
Answer
Artes’ comment suggests there is a problem with the concept of optimum you are trying to find. Let me make the more general point about how your code may be improved.
First, if you have a triply nested
For
loop, you are probably doing it wrong. ConsiderTable
instead, or some of the other constructs mentioned here.Second, you don't need to go through every combination of
{i, j, k}
since you only want the cases wherei
. I think you can get this set of combinations using Subsets[Range[3,450],{3}]
which gives the ordered subsets of exactly length 3.Third, functional programming is usually more efficient. Create the list of indices and then
Map
the desired function to the elements of that list.Fourth,
Print
doesn't store anything. You need to save the results for each case as part of a list somewhere and then find the maximum value of that list. The mapping- onto-a-list approach will actually store the results, if only temporarily.
Putting all of this together, something like this should work:
Max[yourfunction /@ Subsets[Range[3, 450], {3}]]
Comments
Post a Comment