Interestingly, the Perl 5 and Perl 6 solutions to Challenge 24 task 1 in the Perl Weekly Challenge are identical.
Author: Joelle Maslak
Perl Weekly Challenge 12 – Euclid Numbers
This week’s Perl Weekly Challenge, problem 1, reads:
The numbers formed by adding one to the products of the smallest primes are called the Euclid Numbers (see wiki). Write a script that finds the smallest Euclid Number that is not prime. This challenge was proposed by Laurent Rosenfeld.
The first Euclid numbers are 3, 7, and 31. These are computed as follows:
We’ll take the first three prime numbers – 2, 3, and 5.
The corresponding Euclid number is the prime number multiplied by all the smaller prime numbers, with 1 added to it.
So the first one, 3, is just 2 (nothing to multiply it against) plus 1.
The second one is 3 * 2 plus 1, or 7.
The third one is 5 * 3 *2 + 1, which is 31.
How do you do this in code? Continue reading
Converting Decimal to Roman Numbers in Perl 6
This week’s (week 10) Perl Request Challenge, challenge 1, was:
Write a script to encode/decode Roman numerals. For example, given Roman numeral CCXLVI, it should return 246. Similarly, for decimal number 39, it should return XXXIX. Checkout wikipedia page for more informaiton.
For this blog, I’m going to talk about converting a decimal to a Roman numeral.
To start with, I read the Wikipedia page referenced in the challenge, and realized there were several different systems for writing Roman numerals – it wasn’t as standardized as I thought! That said, I stuck with the style used in the description of the challenge, specifically “subtractive” notation. Essentially, the symbols are written from the largest value to the smallest value, left to right, with no more than 3 of any symbol used. When four of a symbol would normally be used (for instance, IIII to mean 4), instead it would be written as IV, meaning one less than 4 (you see this because the smaller number is before the bigger number).
So that’s what I’ll talk about below – the part of the code that converts an integer to a Roman number.
Solving the Sparkpost Challenge
The Perl Weekly Challenge for week 9 includes an optional third challenge – essentially, use the Sparkpost service’s API to send an email. Sparkpost is a service that allows sending emails via an HTTP interface, just by posting a JSON form response.
I’ve noticed people have not known how to solve these API-usage challenges, so I will share my method of solving them, using Perl 6.
Anagrams – in O(N)
If you understand “big O” notation, skip on down to the “Solving Week 5 Problem 2” heading.
I’m going to discuss this using Perl 6, but I think an intermediate Perl 5 user will be able to figure out what the code does and how it would be able to be implemented in Perl 5.
“Big O” Notation
Most students of computer science have been exposed to “big O” notation. Essentially, algorithms can be classified by performance. N represents the size of the data being processed, while O() is shorthand for “execution time on the order of …”
Open Up Your Open Source Module – Part 4: Contributor Credit
This is Part 4 of a 5 part series. Links to other parts (as they become available) are below.
- Part 1: CONTRIBUTING File
- Part 2: TODO File
- Part 3: CODE_OF_CONDUCT File
- Part 4 (Below): Contributor Credit
- Part 5 (Future): BONUS TIPS!
We’ve previously talked about adding the CONTRIBUTING, TODO and CODE_OF_CONDUCT files. But how about giving contributors credit? This is a short, and easy, tip.
Open Up Your Open Source Module – Part 3: CODE_OF_CONDUCT File
This is Part 3 of a 5 part series. Links to other parts (as they become available) are below.
- Part 1: CONTRIBUTING File
- Part 2: TODO File
- Part 3 (Below): CODE_OF_CONDUCT File
- Part 4: Contributor Credit
- Part 5 (Future): BONUS TIPS!
We’ve previously talked about adding the CONTRIBUTING and TODO files, so now we’ll get to the post I’ve been dreading: the CODE_OF_CONDUCT file. I believe strongly in the value of codes of conduct, and think every project should have one.
So, why would I dread it?
Open Up Your Open Source Module – Part 2: TODO File
This is Part 2 of a 5 part series. Links to other parts (as they become available) are below.
- Part 1: CONTRIBUTING File
- Part 2 (Below): TODO File
- Part 3: CODE_OF_CONDUCT File
- Part 4: Contributor Credit
- Part 5 (Future): BONUS TIPS!
So, you’ve added a CONTRIBUTING file to your module (in part 1). That’s great! Now people know the ground rules for contributing to your module. But there is another thing you can do to encourage the contributions you want: Simply tell the potential developer what changes you might appreciate.
Open Up Your Open Source Module – Part 1: CONTRIBUTING File
You’ve written something awesome! You want to release it as open source software into the world. And, you hope other people will want to polish the rough edges, oil the squeaking hinge, and translate the help text into Klingon. Assuming that your module is interesting to people, how do you help them help you? I’ll give examples of how to do this in the context of Perl 5 modules, but they’ll work in other languages, too.
I talked about this in a lightning talk @ The Perl Conference 2018
But I also thought I could expand and organize my thoughts a bit better, so for the textually inclined, read on!
This is part one of a five part series. I’ll link them all here when they are up.
- Part 1 (Below): CONTRIBUTING File
- Part 2: TODO File
- Part 3: CODE_OF_CONDUCT File
- Part 4: Contributor Credit
- Part 5 (Future): BONUS TIPS!
A Minimal Raspberry Pi 3 GPS Time Server
A Raspberry Pi can make an outstanding NTP time server for your network. Below, I document the bare minimum needed to get a Raspberry Pi 3 Model B with the Adafruit Ultimate GPS Hat working as an NTP server. You can do this, even buying all the parts new, for under $100 USD.
What makes this project interesting is that you end up with potentially a very compact time server – a Pi with a nicely stacked board on top. Just bring network and power to the box, put the box near a window, and it’s otherwise self-contained. For an even simpler deployment, in a corporate environment with PoE switches, use a PoE to USB cable like this one (disclaimer: this is an Amazon affiliate link, so I get a tiny amount of money if you buy something here): PoE to USB Splitter – then all you need is a network cable (no wall wart is required) which supplies connectivity and power. Note that the PoE (Power over Ethernet) adapter requires that you have ethernet switches that support PoE. Your business might have these, but you probably don’t have them at home (they cost a lot more money).
The only thing I’m missing is a good case – if anyone knows of a good Raspberry Pi 3 case that fits well with the Adafruit hat installed, I’m very interested — please comment!
Why do you want to do this? There are several reasons:
- You have a need for “correct” time on your servers (to do log correlation or for regulatory requirements)
- You have an asymmetric internet connection. There is no way for internet time servers to be more accurate than 10ms or so if you have this type of connection – virtually any DSL or cable internet connection. On these connections, you have a faster download than upload. That means that that it takes several ms (milliseconds) less time to travel from the internet to your machines than it takes to travel from your machines to the internet. Because NTP measures round trip delay and divides it by two, assuming equal delay on both paths, your time will be off in such cases. Whether it’s off enough to bother you is a different question!
- Related to the above, it is very unusual for internet backbone paths to be the same for both directions of your connection. In fact, this is more common than not. This causes issues with NTP.
- During network congestion (whether it’s a peering point, a congested NTP server, or your internet connection), you will see additional delay added, typically more in one direction than another. Right now, NIST’s Maryland servers (time-a.nist.gov, time-b.nist.gov, time-c.nist.gov) have about 70ms of extra delay due to congestion at their end – this makes the time about 35ms off (70 divided by 2) for anyone synchronizing to their servers.
- You need accurate time on a network not connected to the internet and/or during internet outages
- You don’t want to be dependent upon volunteer server operators for the correct time
- You might just like knowing your time is more correct than your friend’s!
There are lots of tutorials on the building NTP servers from Pis, but this one is a bit different because:
- It’s based on the Pi 3 Model B instead of the 2 or older. There are some serial oddities on the Pi 3, so you do need to do minor modifications.
- Most tutorials connect to gpsd via shared memory to fetch the time of day while using the kernel PPS module to get the seconds to start at the right instant. This means you can have time that is several hundred milliseconds off reality upon startup of NTP. This can be mitigated slightly by fudging the stratum of the GPS and having internet time services also configured, but it can’t be eliminated completely.
- I don’t generally manage computers from the console, I use network-based access to do so. Most tutorials assume it’s convenient to haul a monitor out to the Pi. This tutorial never requires you to connect a monitor (but you can if you want).
- This tutorial uses versions of ntpd and gpsd I compiled from more recent sources than come with the stable branch of Raspbian. The ntp code also has a patch I’ve made to allow it to see both the GPS and PPS devices from gpsd easily.
So, here’s the procedure: Continue reading