Wednesday, December 11, 2024

HTML table year calendar

I wrote a simple new program in Perl, yeartable.pl, another one like this other old calendar program, but now I try to visualize all the days of a given year in one page, suitable to be copied to your favourite word processor and then printed, to get a calendar like this:

Calendar table view in spanish

To run it you need to provide the year, the names of the months and the names of the week days, for example:

$ perl yeartable.pl
Use: perl yeartable.pl YEAR "Jan,Feb,..." "Sun,*Mon,..."
(put * in the week-day to be the first row in the table)
$ perl yeartable.pl 2025 "Jan,Feb,Mar,Apr,May,Jun,Jul,Ago,Sep,Oct,Nov,Dic" "S,*M,T,W,T,F,S" > 2025.html

And then you can open the generated file with an internet browser, copy the table directly to a word processor and adjust the format. Enjoy!

Using jugs of known capacities to get certain quantity

Suppose you only have a few containers of known capacities for measuring quantities, for example two jugs of 5 and 7 litres of capacity respectively, and you want to use them to obtain exactly 1 litre, for example. Can you do it?

jars 7 5 litres

To solve this kind of problems, I wrote a simple Perl program, jugs.pl, that explores the solutions using a brute-force backtracking algorithm limiting its recursivity level for finding the solutions with minimum steps.

There are two versions of these problems, one having a limited quantity of the substance to be moved from one jug to another, and other having an unlimited quantity available to fill any jug and discard it. My program only supports the first version, since the second one can be simulated by creating one jug filled with a large quantity that represents the source and sink of the substance, for example:

jars 20 7 5 litres

This way you can use the program to print a solution for the problem:

$ perl jugs.pl 20,7,5 20,0,0 1
(12) (7) (1)    1->3 3->2 1->3 3->2 2->1 3->2 1->3 3->2
$ perl jugs.pl
Use: perl jugs.pl M1,M2,...,MX I1,I2,...,IX N [NUMSTEPS]
Minimum steps to obtain N units in one of X jugs
given the Maximum and Initial quantity for each jug.

This way you can explore different configurations to learn more from the problem, even setting the number of steps of the solutions. Enjoy!

Sunday, August 4, 2024

Simulating a dice of any number of sides by rolling a different dice

Suppose for example that you have a dice of 6 sides and you want to obtain a random result between 1 and 8, how could you do it?

Well, one way is running my Perl script anydice.pl, rolling the dice the indicated number of times and then checking what is the result indicated by the tables printed by the program.

The method used by the program is explained in the paper "Simulating a dice with a dice" by B. Kloeckner.

$ perl anydice.pl
Use: anydice.pl [-d N] [-w] MAX
Print tables to get a random number from 1 to MAX by rolling a dice.
  -d N, -dN    Use a dice of N sides, 6 by default
  -w           Print the tables in HTML instead of ASCII
Important: When rolling a dice many times, if you get the result ?
then you must repeat ALL the rolls to get a correct probability.
$ perl anydice.pl -d6 8
A\B| 1 2 3 4 5 6
---+------------
 1 | 1 1 1 1 2 2
 2 | 2 2 3 3 3 3
 3 | 4 4 4 4 5 5
 4 | 5 5 6 6 6 6
 5 | 7 7 7 7 8 8
 6 | 8 8 ? ? ? ?

In this case, you must roll the D6 two times, A and B, and then look at the table to see what is the result. Note that if you get the result "?" when rolling the dice many times, then you must repeat all the rolls from the beginning, otherwise you will get certain results with a probability not equal to the others.

Monday, July 15, 2024

Cistercian numerals in ASCII Art

So you want to convert any number to the Cistercian numerals in your terminal? Well, in that case please download my cisternum.pl Perl script to do it and enjoy how the monks from the XIII century used to write numbers!

$ perl cisternum.pl
Use: cisternum.pl [-h|-v] NUMBER ...
Converts numbers to Cistercian numerals represented in ASCII.

 NUMBER Number encoded using Arabic numerals from 0 to 9999
 -h     Uses the horizontal representation, the default
 -v     Uses the vertical representation instead of horizontal

$ perl cisternum.pl 2024 07 16
           _         _      
 _\____   |______    ______ 
  |  |              |       

$ perl cisternum.pl -v 2024 07 16
          _    _   
 _|/     | |    | |
  |      |      |  
 _|      |      |  
  |      |      |  

Monday, December 11, 2023

Exploring the paths between two points of a grid

Many results in combinatorics can be generated counting all the different ways to make something in the real world. As an example, consider the lines connecting points in a grid or lattice:

o-------o   .   .
        |
.   .   o---o   .
            |
.   .   .   |   .
            |
.   .   .   o---o
                |
.   .   .   .   o

This figure can be associated to many objects in the real world, like a walk through the streets of a city or multiple movements of a chess rook. The number of different paths like this in a grid of NxN points using only two directions to move and not crossing the main diagonal of the grid is equal to the Catalan number N, and these paths are also known as Dyck paths. Interestingly, including the moves crossing the main diagonal generates exactly the numbers of the famous Pascal's triangle, that is, the Binomial coeficients.

OK, but, what happens if we use a chess queen instead?

Thinking in that question I wrote a program to print these paths, and I generalized it to optionally include moves surpassing the main diagonal of the grid and to optionally include diagonal paths. I published the result in LatticePath and it prints the paths in ASCII:

> java -jar LatticePath.jar --pass --diag 5
...
o---o---o
        |
        o---o
            |
            o
            |
            o
            |
            o---o

o---o---o
        |
        o
         '.
           'o---o
                |
                o
                |
                o

o---o---o
        |
        o
         '.
           'o
             '.
               'o
                |
                o
...
> java -jar LatticePath.jar --pass --diag --count 5
1	1	1	1	1
1	3	5	7	9
1	5	13	25	41
1	7	25	63	129
1	9	41	129	321

After writing the first version of the program I realized that including diagonal moves generates different kinds of numbers: The number of paths not surpassing the main diagonal of the grid is called Schröder number, and the number of paths surpassing the main diagonal of the grid is called Delannoy number. How many connections from only a few simple rules! Thanks also to the OEIS site for being so useful for knowing the name of any sequence of numbers.

I hope you enjoy the program LatticePath and the minimalistic style in which it is programmed!

Saturday, July 9, 2022

Why the area of a square of side L is LxL?

Recently I watched a video talking about the difference of the volume of the sphere of radius 1 and the cube of side 1 in N-dimensions, and I asked myself why the cube volume is always 1 in any dimension, or, which is an equivalent question:

Why the area of a square of side L is LxL?

Because the unit to measure areas is the square of side 1.

Why the unit to measure areas is the square of side 1?

Because it's convenient, since we usually measure areas by dividing them in rectangles and then measuring the sides of those rectangles. But it's not mandatory!

What happens if we change the unit to measure areas?

Well, you can continue doing maths, but then, the area formulas will be all wrong. Yes, they only work if you first state that the area of a square of side 1 is 1. Anyway, the resulting formulas will only be different by a multiplying constant.

It's an interesting exercise to derivate the main area formulas by choosing, for example, the equilateral triangle of side 1 as the area unit if you like square roots and Pythagoras, or the circle of radius 1 as the area unit if you like π and how to measure circles with triangles and vice versa.

As with the common use of our arbitrary base 10, it's important to note that much of our mathematical knowledge, as the formula of the area of the rectangle, is just conventional. Think about it before talking to any aliens, or perhaps you will not be able to understand each other...

Thursday, May 12, 2022

Add or subtract seconds to a subtitles .SRT file

Do you have a subtitles .SRT file not synchronized with the video?

Recently I recovered an old program that I wrote and I made it better. Now it supports milliseconds and negative numbers: add_seconds_to_srt.pl

> perl add_seconds_to_srt.pl
usage: add_seconds_to_srt.pl SECONDS[,MILLISEC] <INPUTFILE >OUTPUTFILE
> perl add_seconds_to_srt.pl -17,995
00:00:18,994 --> 00:00:24,594
00:00:00,999 --> 00:00:06,599
>

Enjoy it!