Pure Functions

In this lesson, we’ll look deeper into pure or anonymous functions. We’ll start with a simple example. Suppose that we want to create a function f which takes as input a positive integer n  and returns the sum 1 + 2 + 3 + ... + n. This can be done in Mathematica in a variety of ways. One way is to use a function defined in terms of a formula.

f[n_]:= Sum[i, {i, 1, n}]

We can now evaluate f or even map it across a list of numbers. Both of these tasks can also be accomplished using shortcuts.

f[5] (* this is normal evaluation*)

f@5 (* this is shortcut evaluation *)

Map[f, {1, 5, 100}] (* this is normal evaluation using Map *)

f /@ {1, 5, 100} (* this is shortcut evaluation using Map *)

In the example above, notice that f was an input to the function Map which required us to have previously defined the function f. Pure functions provide us a way to define a function on the fly without having to provide an explicit name for the function. (See this page for more information on pure functions.) In the following exercises, you will see that the ability to use a function without naming it can be very handy. Here is a pure function equivalent to f defined previously. This can also be done using shortcuts.

Function[n, Sum[i, {i, 1, n}]] (* normal definition - requires an input n *)

Sum[i, {i, 1, #}]& (* shortcut definition - requires an input # *)

Again, we are free to evaluate these pure functions or map them to lists. Here’s how we can use the pure functions to exactly what we did previously with the named function f.

Function[n, Sum[i, {i, 1, n}]][5]

Sum[i, {i, 1, #}]&@5

The following computations do exactly the same thing!

Map[Function[n, Sum[i, {i, 1, n}]], {1, 15, 100}]

Function[n, Sum[i, {i, 1, n}]] /@ {1, 15, 100}

Exercise 1: Use a pure function to compute 1 + 2 + ... + 2015.

Exercise 2: Use a pure function to create a list of length 50 whose i-th entry is 1 + 2 + 3 + ... + i.

In Hazrat, anonymous functions are demonstrated using shortcuts. Below shows how the book might suggest mapping out function onto a list.

Sum[i,{i, 1, #}]&/@ {1, 15, 100}

This works and provides the same output as the version that explicitly uses the Function command, but there are advantages to the Function construction as well. In particular, using Function will allow us to name input variables, so that we are able to use pure functions within other pure functions without ambiguity. For example, if we wanted to test whether each of the elements of the following lost are divisible by some integer, we could introduce a pure function or a variable # that performs this task.

Map[Function[n, Divisible[Sum[i, {i, 1, n}], #]],{1, 5, 100}]&

Notice that inside this pure function, the Divisible function is used. Both variables, n and #, appear inside of this use of Divisible. As we have seen with shortcuts, the variable is always named #. Because of this fact, the shortcut method for our pure function which sums the first n integers would not work well, because Divisible wouldn’t know how to distinguish between n and # (as they both would named #). Using Function allows us to label the variable n which disambiguates the inputs. We use this new pure function to test whether elements in the list are divisible by 3 and then by 5.

Map[Function[n, Divisible[Sum[i, {i, 1, n}], #]],{1, 5, 100}]&@3

Map[Function[n, Divisible[Sum[i, {i, 1, n}], #]],{1, 5, 100}]&@5

Exercise 3: Use nested pure functions (like above) and answer the following questions.
	1. For how many integers n between 1 and 100 does 7 divide 1 + 2 + ... + n?
	
	2. Create a list of length 100 whose i-th entry is the number of integers between 1 and 100 such that i 
	     divides 1 + 2 + ... + n.

It is also possible to create a pure function of several variables. For example, suppose we wish to create a pure function which takes two positive integers n and k as inputs and returns the sum (1^k)+(2^k)+ ... +(n^k).  Here is a pure function that does the job, along with the shortcut after. (Note that when using shortcuts, the n-th variable is named #n). Use this function to answer exercises 4 through 7.

Function[{n, k}, Sum[i^k, {i, 1, n}]] (* use for Ex 4, 5, 6, 7 *)

Sum[i^#2, {i, 1, #2}]&

The following computation calculates the sum of the first five squares.

Function[{n, k}, Sum[i^k, {i, 1, n}]][5, 2]

Exercise 4: Compute the sum of the first 100 squares.

Exercise 5: Compute the sum of the first 50 cubes.

Exercise 6: For each proposition, write a pure function which returns the truth value of the equation for a specific input n and then use it to determine truth values for 1 ≤ n ≤100. Which propositions are false, and which do you suspect are true? (Remember, there is no such thing as proof by example!)

Exercise 7:  Let (Subscript[a, n])=(∑_(i=1)^n)(i^(2 n)). Use Inner and the anonymous function marked above to compute the least common multiple of (Subsuperscript[((Subscript[a, n])), n=1, 10]). (Hint: Yes, the answer is huge!)

Exercise 8: 
	1. Decipher what the following code does:

Outer[Function[{n, k}, Sum[i^k, {i, 1, n}]], Range[5], Range[5]]

(Hint: To see a list of lists more clearly, check out the command TableForm.)

2. Using a pure function of two variables and Outer, answer the following question. For how many pairs of integers (n, k), with 1 ≤n, k ≤ 100, does k divide (1^k)+(2^k)+... + (n^k)?

Exercise 9:

A positive integer n is called a Liese number if there exists an integer k, with 1 ≤ k ≤ n, such that the sum of the k-th powers of the proper divisions of n is prime. Use pure functions only. For historical context, the Liese numbers were invented by Dr. Liese for this homework assignment and have no mathematical significance whatsoever.
	1.  Without using the computer, explain why no prime number is a Liese number.
	
	2. Is 6 a Liese number?
	
	3. What is the smallest Liese number?
	
	4. How many of the first 500 integers are Liese numbers?