# What is Mathematica doing?

**URL:** <https://community.wolfram.com/t/what-is-mathematica-doing/2930>\
**Category:** Questions\
**Tags:** mathematica, tuning-and-debugging\
**Created:** [August 11, 2014, 7:58pm UTC](https://community.wolfram.com/t/what-is-mathematica-doing/2930 "2014-08-11T19:58:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![generalrelativity](https://avatars.discourse-cdn.com/v4/letter/g/e9bcb4/32.png) [@generalrelativity](https://community.wolfram.com/u/generalrelativity)\
**Post date:** [August 11, 2014, 7:58pm UTC](https://community.wolfram.com/t/what-is-mathematica-doing/2930/1 "2014-08-11T19:58:59Z")

</div>

I am still in the process of learning the Wolfram language. I have written the following two implementations of a function that finds all perfect numbers between the given range. The first implementation is how you may typically write such a function in a language such as c. The second version is my attempt at a more Mathematica version of the function.

```
PerfectSearch[from_Integer, to_Integer] := Module[
  {id = to},
  Flatten[Rest[Reap[While[id >= from,
      sum = 0;
      n = id - 1;
      While[n > 0,
       sum = sum + If[Mod[id, n] == 0, n, 0];
       n = n - 1];
      If[sum == id, Sow[id]];
      id = id - 1;
      ]
     ]
    ]
   ]
  ]

```

And

```
PerfectSearch2[from_Integer, to_Integer] := 
 Module[{testNumbers = Range[from, to]},
  MapThread[(If[#1 == #2, #1, (##) &[]]) &,
   {
    Map[
     Function[testNum,
      Total[
       Select[
        Map[
         Function[divisor, If[Mod[testNum, divisor] == 0, divisor, 0]],
         Range[testNum - 1]
         ],
        (# > 0) &
        ]
       ]
      ], 
     testNumbers
     ]
    ,
    testNumbers
    }
   ]
  ]

```

I generated timing information for these two implementations as follows

```
p1Timing = DeleteCases[
   Flatten[
    Table[
     Timing[
      PerfectSearch[1, n];
      ],
     {n, 2, 200}
     ]
    ]
   ,
   Null];

```

And

```
p2Timing = 
  DeleteCases[
   Flatten[Table[Timing[PerfectSearch2[1, n];], {n, 2, 200}]], Null];

```

This timing information is shown below

 ![Timings.png](https://global.discourse-cdn.com/wolfram/original/2X/7/72737f28962bc3219042d576d4bb8bae114c7365.png)

Can anyone explain to me what Mathematica is doing in the second version of the function. It appears that the two functions perform the same up until testing numbers of 100 and above. At this point the second version of the function takes significantly less time to calculate.

Feel free to point out any improvements in the code; As I say, I am still getting to grips with Mathematica.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![dreiss](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/dreiss/32/29094_2.png) [@dreiss](https://community.wolfram.com/u/dreiss)\
**Post date:** [August 11, 2014, 9:14pm UTC](https://community.wolfram.com/t/what-is-mathematica-doing/2930/2 "2014-08-11T21:14:48Z")

</div>

It is likely that at that value the internal automatic compilation of some of the code is kicking in:

```
In[16]:= SystemOptions[CompileOptions]

Out[16]= {"CompileOptions" -> {"ApplyCompileLength" -> \[Infinity], 
   "ArrayCompileLength" -> 250, "AutoCompileAllowCoercion" -> False, 
   "AutoCompileProtectValues" -> False, "AutomaticCompile" -> False, 
   "BinaryTensorArithmetic" -> False, "CompileAllowCoercion" -> True, 
   "CompileConfirmInitializedVariables" -> True, 
   "CompiledFunctionArgumentCoercionTolerance" -> 2.10721, 
   "CompiledFunctionMaxFailures" -> 3, 
   "CompileDynamicScoping" -> False, 
   "CompileEvaluateConstants" -> True, 
   "CompileOptimizeRegisters" -> False, 
   "CompileParallelizationThreshold" -> 10, 
   "CompileReportCoercion" -> False, "CompileReportExternal" -> False,
    "CompileReportFailure" -> False, "CompileValuesLast" -> True, 
   "FoldCompileLength" -> 100, "InternalCompileMessages" -> False, 
   "ListableFunctionCompileLength" -> 250, "MapCompileLength" -> 100, 
   "NestCompileLength" -> 100, "NumericalAllowExternal" -> False, 
   "ProductCompileLength" -> 250, "ReuseTensorRegisters" -> True, 
   "SumCompileLength" -> 250, "SystemCompileOptimizations" -> All, 
   "TableCompileLength" -> 250}}

```

If you play with some of these using

```
SetSystemOptions[
 "CompileOptions" -> {"ApplyCompileLength" -> \[Infinity], 
   "ArrayCompileLength" -> 250, "AutoCompileAllowCoercion" -> False, 
   "AutoCompileProtectValues" -> False, "AutomaticCompile" -> False, 
   "BinaryTensorArithmetic" -> False, "CompileAllowCoercion" -> True, 
   "CompileConfirmInitializedVariables" -> True, 
   "CompiledFunctionArgumentCoercionTolerance" -> 2.1072099696478683`,
    "CompiledFunctionMaxFailures" -> 3, 
   "CompileDynamicScoping" -> False, 
   "CompileEvaluateConstants" -> True, 
   "CompileOptimizeRegisters" -> False, 
   "CompileParallelizationThreshold" -> 10, 
   "CompileReportCoercion" -> False, "CompileReportExternal" -> False,
    "CompileReportFailure" -> False, "CompileValuesLast" -> True, 
   "FoldCompileLength" -> 100, "InternalCompileMessages" -> False, 
   "ListableFunctionCompileLength" -> 250, "MapCompileLength" -> 100, 
   "NestCompileLength" -> 100, "NumericalAllowExternal" -> False, 
   "ProductCompileLength" -> 250, "ReuseTensorRegisters" -> True, 
   "SumCompileLength" -> 250, "SystemCompileOptimizations" -> All, 
   "TableCompileLength" -> 250}]

```

You will be able to see if the threshold where the timing makes a jump moves according to the change in the threshold for the system options setting.

---

<div class="post-metadata">

**Author:** ![dckubler](https://avatars.discourse-cdn.com/v4/letter/d/e480ec/32.png) [@dckubler](https://community.wolfram.com/u/dckubler)\
**Post date:** [August 12, 2014, 2:54am UTC](https://community.wolfram.com/t/what-is-mathematica-doing/2930/3 "2014-08-12T02:54:36Z")

</div>

One of the things to learn about Mathematica (Wolfram) is to search the documentation for functions that may simplify your work.  
Try _guide/NumberTheoreticFunctions_ in the documentaion. There you’ll find `Divisors[]` and `DivisorSum[].`  
A number is perfect if `Total[Divisors[n]]/2==n` or `DivisorSum[n, Identity]/2 == n`.

---

<div class="post-metadata">

**Author:** ![generalrelativity](https://avatars.discourse-cdn.com/v4/letter/g/e9bcb4/32.png) [@generalrelativity](https://community.wolfram.com/u/generalrelativity)\
**Post date:** [August 12, 2014, 7:42pm UTC](https://community.wolfram.com/t/what-is-mathematica-doing/2930/4 "2014-08-12T19:42:46Z")

</div>

The “speed-up” does indeed seem to be due to the internal compilation that is occurring from the use of Map. For anyone wanting a nice review of the compilation features and limits see the link  
[How to compile effectively](http://mathematica.stackexchange.com/questions/1803/how-to-compile-effectively)
