r/Mathematica • • Mar 31 '26

How do we decrease the computation time of this code?

You can answer the question here and here.

I want to decrease the computation time of the following code.

If c is a large constant, how do we show when:

 Clear["Global`*"]
    c=100
    LengthS[r_] := LengthS[r] = {3(r-c)!, r!/2 + 1}
    LengthS1[r_, y_] := LengthS1[r, y] = LengthS[r][[y]]
    LengthS2[j_, y_] := LengthS2[j, y] = LengthS[j][[y]]
     V[r_]:=V[r]=r!+1
     P1=200

and:

   Min11[r_, x_] := 
     Min11[r, x] = 
      FindInstance[LengthS1[r1, x] < V[r] && V[r] < LengthS1[r1 + 1, x] && 
        r - P1 <= r1 && r1 <= r + P1, {r1}, PositiveIntegers]

    Min12[r_, x_] := 
     Min12[r, x] = 
      ArgMin[{RealAbs[LengthS1[r2, x] - V[r]], r - P1 <= r2 <= r + P1}, 
       r2, PositiveIntegers]

    Min21[r_, y_] := 
     Min21[r, y] = 
      FindInstance[LengthS2[r3, y] < V[r] && V[r] < LengthS2[r3 + 1, y] && 
        r - P1 <= r1 && r1 <= r + P1, {r3}, PositiveIntegers]

    Min22[r_, y_] := 
     Min22[r, y] = 
      ArgMin[{RealAbs[LengthS2[r4, y] - V[r]], r - P1 <= r4 <= r + P1}, 
       r4, PositiveIntegers]

then rMin1[r,1]==r+c and rMin2[r,2]==r (e.g., rMin1[r,1]==10+c and rMin2[r,2]==10).

rMin1[r_, x_] := 
rMin1[r, x] = 
Min12[r, x] + Sign[Floor[RealAbs[2 r - Min11[r, x] - Min12[r, x]]/2]]
    
rMin2[r_, y_] := 
rMin2[r, y] = 
Min22[r, y] + Sign[Floor[RealAbs[2 r - Min21[r, y] - Min22[r, y]]/2]]

rMin1[10,1]
rMin1[10,2]

However, it takes too long to compute rMin1[10,1] and rMin2[10,2] and I do not know what are the actual outputs.

1 Upvotes

19 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Mar 31 '26

Do you know about FindInstance?

1

u/Xixkdjfk Mar 31 '26

Honestly, I don't. I'm a begginer.

1

u/[deleted] Mar 31 '26

Well, then check it out in the documentation.