How to adress the # Map variable via a function.

Consider the following function:



sf3[4,# &]


{{1, 2, 3, 4}, {1, 2}, {1}, {1}}

as expected.


sf3[4, Floor[4/#] &]


{{4, 2, 1, 1}, {4, 2}, {4}, {4}}

as expected.

I want to know how to address the inner map variable such that I can create:

{Floor[4/1], Floor[4/2], Floor[4/3], Floor[4/4]},
{Floor[2/1], Floor[2/2]},

So, the

Floor[4/#] in sf3[4, Floor[4/#] &] 

should be something like



  • 3
    $\begingroup$ I do not understand what you want to accomplish. What is your ultimate goal here, and what is the expected output from applying your envisioned function to the list of lists of Floor expressions you gave? $\endgroup$ – MarcoB Apr 4 at 15:06
  • 1
    $\begingroup$ Why write Map[fun[#]&, ...] instead of Map[fun, ...]? $\endgroup$ – Jason B. Apr 4 at 15:48
  • $\begingroup$ Because it works, does not help b.t.w. $\endgroup$ – nilo de roock Apr 4 at 16:06
  • $\begingroup$ @MarcoB It is one ( there are at least 3 ) way of expressing the Sum from 1 to n, and for eacht of those the sum from a function applied to the divisors of these numbers. - See Tom Apostol - Introduction to Analytic Number Theory Chapter 3 (par.10 / par. 13) Theorem 3.11 or other books on the field. $\endgroup$ – nilo de roock Apr 4 at 16:09

Unlikely this is what you're looking for but it might push things on a bit.

sf3[n_, fun_] := Map[Map[fun[Length[r]/ToString[#]] &,
    r = Range[Floor[n/#]]] &, Range[n]]

sf3[4, floor[#] &]
{{floor[4/1], floor[4/2], floor[4/3], floor[4/4]},
 {floor[2/1], floor[2/2]},
| improve this answer | |
  • $\begingroup$ I will study this later today ( very early on Sunday at CET right now ), thank you. I have updated the question, perhaps I was not clear enough. $\endgroup$ – nilo de roock Apr 5 at 6:09

Wrap # with Defer or HoldForm in the second argument:

sf3[4, Floor[4/Defer[#]] &]
{{Floor[4/1], Floor[4/2], Floor[4/3], Floor[4/4]}, 
  {Floor[4/1], Floor[4/2]}, 
| improve this answer | |
  • $\begingroup$ This is what I get at the moment, but not what I am looking for. Pls. look again. ( Upvoted? ) $\endgroup$ – nilo de roock Apr 5 at 6:05

I'm not sure I understand the question in its entirety so I'm only going to produce a few notes that will hopefully provide some insight into the problem the way I see it.

Starting from the title of the Q.-if I get it right-this is an issue I struggle with all the time and the solution I usually use is With.

Consider the following code

Map[f[#]&, Range[5]]

This code snippet-I think-is very similar to the original code used in the definition of function sf3 if we allow for f = Map[g[#]&, Range[h[#]]]& and set h = RandomInteger[{1, #}]& for the moment; the only difference is that the respective functions are-in a sense-really simple and straightforward. For the sake of completeness, evaluating the code above in BlockRandom[<code above>, RandomSeeding -> 123456798] produces

{{g[1]}, {g[1]}, {g[1]}, {g[1], g[2]}, {g[1], g[2], g[3], g[4], g[5]}}

Now, consider what is happening in the first argument of Map in Map[f[#]&, Range[5]]: f receives as input sequentially the elements of {1, 2, 3, 4, 5}; each integer is then used as an argument inside f eg for i=1 f[1] evaluates to Map[g[#]&, Range[h[1]]] which in turn we'll assume evaluates further to Map[g[#]&, {1}]& (remember, h returns a random integer in the range {1, 1} this time and the output cannot be anything else but 1 and Range[1] is {1}). Next, the inner Map applies g-its first argument-on the elements of {1} sequentially which-this time-obviously evaluates simply to {g[1]}.

Ok, the point of this narration of the evaluation steps is to set the focus on what is available when g is going to evaluate; we know the input to g in the example above is 1 but there is also another value available: that is the argument of f from the outer Map; that value in the example above is also 1.

The problem in the code above is that when g evaluates, it cannot see that initial 1. g can only see the elements of the second argument of the inner Map ie the elements of list {1}.

So, if that's the problem with the code in the Q. my proposed solution is quite simple: define f as in f = With[{in = #}, Map[g[#, in]&, Range[h[in]]]]& and optionally allow g to receive an extra argument.

With that definition for f, the Map[f[#]&, Range[5]] inside BlockRandom[<code above>, RandomSeeding -> 123456798] evaluates to

{{g[1, 1]}, {g[1, 2]}, {g[1, 3]}, {g[1, 4], g[2, 4]}, {g[1, 5], g[2, 5], g[3, 5], g[4, 5], g[5, 5]}}

Therefore, my answer to the question in the title is to use With. Alternatively, one could define the analog to my f as a function with a local scope using Module and use local variables that will eventually be visible in deeper nested scopes.

End notes

I used the following function to replicate the various output in the Q. and I was able to replicate {{Floor[4/1], Floor[4/2], Floor[4/3], Floor[4/4]},{Floor[2/1], Floor[2/2]},{Floor[1/1]},{Floor[1/1]}}

sf3Do[f_, n_] := Reap[
          Sow[f[j, i]], {j, 1, Floor[n/i]}]] // Rest], {i, 1, n}]
 ] // Rest /* (Flatten[#, 4] &)

Evaluating sf3Do[# &, 4] produces {{1, 2, 3, 4}, {1, 2}, {1}, {1}} and sf3Do[Floor[4/#] &, 4] produces {{4, 2, 1, 1}, {4, 2}, {4}, {4}} as expected.

Also, evaluating sf3Do[Floor[Floor[4/ #1]/#2] &, 4] produces the desired output of {{4, 2, 1, 1}, {2, 1}, {1}, {1}}.

Hope that helps.

| improve this answer | |
sf3[4, Floor[HoldForm[4/#]] &] // TableForm


Use ReleaseHold to evaluate as usual in Mathematica.

{#, #} & /@ sf3[4, Floor[4/#] &][[1]]

{{4, 4}, {2, 2}, {1, 1}, {1, 1}}

sf3hp[{x_, y_}] := sf3[y, Floor[x/HoldForm[#]] &]

Map[sf3hp[#][[1, 1]] &, {#, #} & /@ 
   sf3[4, Floor[4/#] &][[1]]] // TableForm

gives together the output:

output as demanded

| improve this answer | |
  • $\begingroup$ It is essential that it goes through SF3. So second and third options are not relevant. $\endgroup$ – nilo de roock Apr 5 at 6:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.