
For a list of BASHing data 2 blog posts see the index page.
Square root days, prime years and maximum-factor years
Square Root Day happens nine times a century. It's the day on which the square root of the last two digits in the year (2025) is both the month number and the day number, and this year it was 5 May (2025-05-05):
2001-01-01
2004-02-02
2009-03-03
2016-04-04
2025-05-05
2036-06-06
2049-07-07
2064-08-08
2081-09-09
It's easy to generate that list in your head, but I wondered whether there might be other numerical date questions best answered with a bit of coding, like Which of the year numbers are prime?
The GNU "coreutils" utility factor helps with that one. It decomposes a number into its prime factors, and a prime number will show up as a one-factor result ("1" as a factor is ignored):

So in the first 10 years of this century, only 2003 is prime. I can get all the 21st century prime years by filtering the factor output with AWK, since the default field separator for AWK is one or more spaces, and prime years in the factor will have only two fields:

Fourteen prime years in the current century, but how does that compare with past centuries?
One way to answer that question is with nested for loops, but eval is needed to make sure that shell variables within a brace expansion get expanded first. (For more on this point, see this BASHing data post from 2020.)
for cent in {0..20}; do for year in {"$cent"01.."$cent"99}; do eval factor "$year"; done | awk -v hundred="$cent" 'NF==2 {++primes} END {print "century "(hundred+1)": "primes" primes"}'; done

Another date question is Which of the year numbers has the largest number of prime factors?
It's possible to use factor again for this job, but it's fairly slow:
for year in {1..2100}; do factor "$year"; done | awk 'NR==1 {max=NF; a[NF]=$0; next} {if (NF > max) {max=NF; a[max]=$0}} END {print a[max]}'

I might have guessed this answer by assuming that the prime factors would have to be the smallest ones, i.e. 2, and 2 to the 11th power is 2048. In an increasing series of positive integers, will the integer with the largest number of prime factors always be a power of 2?
Next post:
2025-08-22 Converting lat/lon from DMS to DD without screaming
Last update: 2025-08-15
The blog posts on this website are licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License