Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I manually evaluate this cell to set Prime[0]

Unprotect[Prime]
Prime[0] = 1
Protect[Prime]   

I manually evaluate this cell to reset Prime[0]

Unprotect[Prime]
Prime[0] =.
Protect[Prime]  

In case I forget to do it, is there a way to automatically evaluate the second cell when I close the nb?

Motivation

I'm working with bags of primes and want to use $p_0$ as the bag for all prime greater than the square-root of some $n$.

Displayed as: $\left\{p_0^1,p_1^5,p_2^2,p_3^1,p_4^1\right\}$

With counts as: $\left\{1,5,2,1,1\right\}$

share|improve this question
1  
Have you seen NotebookEventActions[]? –  J. M. Sep 3 '12 at 7:03
5  
You also could just define something like: prime[x_Integer /; x >= 0] := If[x == 0, 1, Prime[x]] as another approach. –  Vitaliy Kaurov Sep 3 '12 at 7:07
    
@J.M.-- Looks interesting. I determined that if I exit mma, the change gets reset automatically. It's only a problem when I open or create nbs from the current session. Thanks. –  Fred Kline Sep 3 '12 at 7:54
2  
I was hoping you'd answer your question yourself, since you figured out how to use it... :) –  J. M. Sep 3 '12 at 12:25
add comment

1 Answer 1

up vote 3 down vote accepted

Per J.M., I did this and it works. I put it in the same cell as the setup.

SetOptions[SelectedNotebook[],NotebookEventActions ->
Quiet[{Unprotect[Prime]; Prime[0] =.; Protect[Prime];}]}];
{Unprotect[Prime]; Prime[0] = 1; Protect[Prime]};    

Edited to add the Quiet to the actions.

share|improve this answer
    
Nice work. :) ${}$ –  J. M. Sep 3 '12 at 22:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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