0

The code for my program is as follows:

Clear[f];
{$RecursionLimit = Infinity};
f[n_] := If[n >= 1, f[n] = n - 1 + (-1)^n 2 f[n - 1], f[n] = 0];
FullSimplify[f[2020]/2]

I am using Mathematica 12.0, and trying to find f[2020]/2, but when I run the program I get an un-simplified result, the same using the Simplify, FullSimplify, and no function at all.

4815609167711586848007869227032356256312743227141422634144178841639258733223064376890242310095267513944017583269163671060520344846023756428821109590895218122099470699921398772560089491365798131644138341901312406104325088656339013004576875915896321903255827106838867819739516957333842785448961317408670542466925730316291502478820826826477731689044263368148553678106934675474617807970711635671594529280688929069927871781358399593472235076472408459246709587161732797507513416515412957925372883934815425197732231405475243618346154282741695439549613768814420303038299401914064527250128757745765469699137785079955/2

How can I get the result simplified without the "/2"?

To further clarify what I mean by simplify, I mean that the above number would turn into an exact decimal, but not go into scientific form.

Ex: Long number .5 instead of (the original number)/2

  • what should it simplify to? It's an odd number divided by 2, MMA will leave it as such, just like $3/2$. If you want a numerical approximation, use N. – MarcoB Feb 04 '20 at 18:47
  • The numerator of the fraction you got is an odd number. Were you expecting it to be even? (Otherwise, your function is working properly, as checked with First[RecurrenceTable[{f[n] == n - 1 + (-1)^n 2 f[n - 1], f[0] == 0}, f, {n, 2020, 2020}]].) – J. M.'s missing motivation Feb 04 '20 at 18:47
  • I didn't even think to look at that. Is there a way to find an exact decimal from a fraction? Could you multiply it by 0.5 instead of 1/2? – Lincoln Fuller Feb 04 '20 at 19:05
  • As mentioned by Marco, you can use N[f[2020]/2] to see a decimal; otherwise, you have it backwards, since in Mathematica, fractions are exact, while decimals are inexact numbers. – J. M.'s missing motivation Feb 04 '20 at 19:08
  • @LincolnFuller That's what N is for. Try N[f[2020] / 2] for machine-precision results. If you want, you can also specify higher precision with a second argument to N, such as N[f[2020] / 2, 620]. – MarcoB Feb 04 '20 at 19:08
  • Perhaps the OP wants the result to be expressed in the form of a mixed fraction? I.e., $2407804\dots 539977{1\over2}$. – user15994 Feb 04 '20 at 19:19
  • I ended up using this form to get it in a way I wanted: AccountingForm[N[f[2020]/2, IntegerLength[f[2020]] + 1]] – Lincoln Fuller Feb 04 '20 at 19:20

1 Answers1

2

Consider the following recasting of your code that is more idiomatic:

ClearAll[g]
g[0] = 0;
g[n_] := g[n] = n - 1 + (-1)^n 2 g[n - 1]

You can check that it gives the same results:

g[2020] - f[2020] == 0          (* True *)

The odd bit in the definition of g that goes g[n_] := g[n] = ... implements memoization, so you do not have to recalculate any values of g[n] that you have already calculated before.

You can get a decimal approximation of the exact value returned by your calculation using N:

N[f[2020], 610]

2.407804583855793424003934613516178128156371613570711317072089420819629366611532188445121155047633756972008791634581835530260172423011878214410554795447609061049735349960699386280044745682899065822069170950656203052162544328169506502288437957948160951627913553419433909869758478666921392724480658704335271233462865158145751239410413413238865844522131684074276839053467337737308903985355817835797264640344464534963935890679199796736117538236204229623354793580866398753756708257706478962686441967407712598866115702737621809173077141370847719774806884407210151519149700957032263625064378872882734849568892539977500*10^606

MarcoB
  • 67,153
  • 18
  • 91
  • 189