Projects, portfolio, and personal work of Joe Pelz

Programming Languages

Parlez-vous Deutsch amigo?

During spring break of 2015 at BCIT, I went and redid one of my assignments a few times in a few different languages: C++, Haskell, JavaScript, PowerShell, and Python. C++ was the course I was studying and I had done a fair bit of python before. JavaScript I’d touched a couple times, but Haskell and PowerShell were completely unfamiliar to me.

The assignment I chose was a small lab assignment to calculate which set of 7 numbers had the most permutations that were prime numbers.

Considering 3-digit numbers, as a simpler example, gives a maximum of 4 permutations. There are 3 different solutions tied with 4 permutations.

3-digit numbers are easy to work with, but when you get up to 7-digit numbers it becomes more important how you choose to solve it. I really enjoyed solving this one. So much so that I resolved it in a few different languages. The code is the purpose of this article so I’m about to give away how to solve it. Stop now if you want to do it yourself.

Solutions

The general solution I used is

  1. Generate all the prime numbers in the range you need: e.g. [1000000:9999999]
  2. Write a function that will take a number in that same range, and turn it into a key based on the digits it contains, so that 1045, 1405, 5041, and 4501 all give the same key
  3. Group all the prime numbers that have the same key, or just keep the largest group if you don’t care about ties.
  4. Report the largest group(s).

One other note: Do not take the results below as a benchmark for a language speed comparison. I ran different code in each of the languages, some of which I had no knowledge of prior to starting this project and are not necessarily optimal. Haskell was run through a virtual machine, C++ through Cygwin, JavaScript through chromium.

C++

Haskell

JavaScript

PowerShell

Python