Narcissus prime in Python

I’ve been looking back on some of my blog posts that included Mathematica code to see whether I could rewrite them using Python. For example, I rewrote my code for finding sonnet primes in Python a few days ago. Next I wanted to try testing the Narcissus prime.

Futility closet describes the Narcissus prime as follows:

Repeat the string 1808010808 1560 times, and tack on a 1 the end. The resulting 15601-digit number is prime, and because it’s a palindrome made up of the digits 1, 8, and 0, it remains prime when read backward, upside down, or in a mirror.

My Mathematica code for verifying this claim is posted here. Here’s Python code to do the same thing:

    from sympy.ntheory import isprime
    isprime(int("1808010808"*1560 + "1"))

This does indeed return True. However, the Mathematica code ran for about 2 minutes and the SymPy code took 17.5 hours, about 500 times longer.

Update (December 29, 2019): Aaron Meurer reports in the comments that the latest version of SymPy is much faster at solving this problem.

My next endeavor

After over 12 years at MD Anderson Cancer Center, I turned in my resignation last week. I’m leaving my steady job for self-employment.

I have done occasional consulting for 20 years, but I haven’t done it as my only source of income before, other than a salaried position I once had with a consulting company. I enjoy consulting, and I’ve wanted for some time now to do it for a living. Recently, my wife and I agreed that this was the time to make the leap.

I plan to offer consulting in applied math, statistics, and software development. At a high level, this means helping businesses formulate and solve problems, as well as helping them to interpret and apply the solutions. At a lower level, this means using Bayesian inference, number crunching, or whatever else it takes to get the job done. I am also available for speaking and training.

The hard part of consulting is finding enough work. I would appreciate your help in this. Please contact me if you would like to work together, or if you have suggestions for people I should talk to.

Management principle from Dune

The other day Atamert Ölçgen quoted a passage from Dune in response to something I’d posted on Google+. I haven’t read the Dune books, but the passage puts generalists in a favorable light, and since I’m something of a generalist, I’m inclined to like such quotes. Here’s the part I particularly liked. It says that the mentat must be able to say

There’s no real mystery about this at the moment. This is what we want now. It may prove wrong later, but we’ll correct that when we come to it.

Too often we act as if every decision is weighty and permanent. Of course some decisions are weighty and permanent, but most are not. Wisdom is knowing which decisions are and which are not. Sometimes it pays to not try to look too far ahead, conceding that if you’re wrong “we’ll correct that when we come to it.”

Related posts

Elementary statistics book recommendation

I’ve thought about making a personal FAQ page. If I do, one of the questions would be what elementary statistics book I recommend. Unfortunately, I don’t have an answer for that one. I haven’t seen such a book I’d recommend enthusiastically.

When asked for book recommendations, people will often recommend the textbook used in a course they had. But I never had an elementary statistics course. I had a PhD in math before I became interested in statistics, so I learned statistics from more advanced books. I’ve looked at a number of elementary books, but I haven’t found one I’m excited about.

Elementary statistics books may do more harm than good. They often brush difficulties under the rug. They avoid mathematical and philosophical details. They don’t define terms carefully, and even say things that are false. And they imply that statistical analysis is a matter of applying a set of rules by rote. (And it is, for many statisticians. But that’s a topic for another time.) If a statistics book doesn’t have fairly steep prerequisites, it will be hard for it not to be misleading.

This leads to another frequently asked question: Do I intend to write my own elementary statistics book? No. I don’t know whether I could write such a book that I’d be proud of. And if I could, it would take more time than I could afford to devote to it at this point in my life.

(I’ll write soon about what “this point in my life” is. If you don’t want to wait, here’s the news in a nutshell.)

Rifle-shaped wine bottle

Ever since I posted a photo of an odd wine bottle four years ago, I’ve periodically had people leave comments or send me email about odd wine bottles!

Here’s the latest, an Italian wine bottle shaped like a riffle:

Here’s a close-up of the cap.

If you know anything about this bottle of wine or what it might be worth, let me know and I’ll pass the info on to the fellow who sent me the photos.

Not complex enough

One time a professor asked me about a problem and I suggested a simple solution. He shot down my idea because it wasn’t complex enough. He said my idea would work, but it wasn’t something he could write a paper about in a prestigious journal.

I imagine that sort of thing happens in the real world, though I can’t recall an example. On the contrary, I can think of examples where people were thrilled by trivial solutions such as a two-line Perl script or a pencil-and-paper calculation that eliminated the need for a program.

The difference is whether the goal is to solve a problem or to produce an impressive solution.

Five years to overnight success

The first post here was January 9, 2008, five years ago today. Since then I’ve written over 1900 posts, about one a day.

Not many people read anything here until May 2009 when the post Plain Python got a few thousand visitors. Then the site mostly returned to obscurity, aside from a few random spikes. It wasn’t until sometime in 2010 that readership started growing steadily.

Thanks to all of you who have contributed to the blog by reading, commenting, sharing links, suggesting topics, etc.

If you stop by regularly rather than subscribing, you may be interested in 3.5 ways to subscribe.

You can also find me on Twitter and Google+.

Sonnet primes in Python

A while back I wrote about sonnet primes, primes of the form ababcdcdefefgg where the letters a through g represent digits and a is not zero. The name comes from the rhyme scheme of an English (Shakespearean) sonnet.

In the original post I gave Mathematica code to find all sonnet primes. This post shows how to do it in Python.

from sympy.ntheory import isprime
from itertools import permutations

def number(t):
    # turn a tuple into a number
    return 10100000000000*t[0] + 1010000000000*t[1] 
           +   1010000000*t[2] +     101000000*t[3] 
           +       101000*t[4] +         10100*t[5] 
           +           11*t[6]

sonnet_numbers = (number(t) for t in 
    permutations(range(10), 7) if t[0] != 0)

sonnet_primes = filter(isprime, sonnet_numbers)

Disciplina praesidium civitatis

The title of this post is the motto of The University of Texas. I’m pretty familiar with that motto, having received degrees and paychecks from UT. I only recently learned the motto’s context, a statement by Mirabeau Lamar, second president of the Republic of Texas. The motto is the Latin form of the opening phrase.

A cultivated mind is the guardian genius of democracy and, while guided and controlled by virtue, the noblest attribute of man. It is the only dictator that freemen acknowledge and the only security that freemen desire.

I found Lamar’s statement in the essay Against Great Books (subscription required) in which the author comments on the “contradiction between the seals and mottos of older institutions and their more recent mission statements.” The marketing coming out of UT now emphasizes cultivating research to advance the economy more than cultivating virtuous minds to serve as guardians of democracy.

Efficiency could land you in jail

A German postman recently faced criminal charges for coming up with using more efficient routes to deliver the mail. His supervisor had informally tolerated his initiative, but could not officially sanction it since his violated procedure. He got into trouble when his suspicious peers reported him. Fortunately he was not fired, only reprimanded for not following rules.

The source I saw (thanks Tim) doesn’t give much more detail. Maybe the charges against him were not as ridiculous as they seem. Maybe he violated reasonable safety regulations, for example. But I find it quite plausible that he simply got into trouble for using his brain. Even if the incident were completely made up, it would make a good story. It’s symbolic of bureaucratic punishment of efficiency. It’s easy to find analogous examples.

If this mailman were working for a small courier company, the company might reward him and ask him for recommendations for improving other routes. Of course a small company might also fire him. But large organizations, public and private, are more likely to punish initiative. And I understand why: large organizations have to maintain consistency. The clever postman must be reprimanded for the good of the system, but it’s maddening when you’re the postman.

Related posts