0
$\begingroup$

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

and:

P1=200    

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 <= r3 && r3 <= 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.

$\endgroup$
4
  • $\begingroup$ @MIchaelE2 I fixed it. I want rMin1[r,1]==r+c and rMin2[r,2]==r (e.g., rMin1[10,1]==10+c and rMin2[10,2]==10) $\endgroup$ Commented Mar 31 at 15:18
  • $\begingroup$ Why don't you simply use the Stirling’s approximation of the factorial? $\endgroup$ Commented Mar 31 at 17:18
  • $\begingroup$ @yarchik You could, but I want this to work for large and small c. (Sorry if I wasn't specific enough. I cannot make more edits.) $\endgroup$ Commented Mar 31 at 17:24
  • $\begingroup$ Cross-posted: community.wolfram.com/groups/-/m/t/3672512 $\endgroup$ Commented Mar 31 at 20:43

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.