Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Sunday, December 30, 2018

Magic squares generator in C

2021-11-10: NOTE: I rewrote the program to make it faster in magic-square.

A magic square is a table with equal number of rows and columns that is filled with all the numbers from 1 to NxN (being N the number of rows) in a way that verifies that the sum of the numbers in any row, in any column and in any of the two diagonals gives the same result in all cases, which is called the magic constant. If you choose a different initial number than 1 for filling the square (even zero or negative) and a different increment than 1 to get the following numbers, you will also get the same number of magic squares although the magic constant will change.

Because by rotation or reflection of a magic square you can get other 8 magic squares, only one of those variations (or trivial solutions) is counted. One magic square of size 1x1 exists, zero magic squares of size 2x2 exist and one magic square of size 3x3 exist (with 8 rotations and/or reflections) whose lines sum 15. If you choose the magic square of size 3x3 with the minimum corners, you get this one (printed by my magic square generator):

 2 | 9 | 4 
---+---+---
 7 | 5 | 3 
---+---+---
 6 | 1 | 8 

Exist 880 magic squares of size 4x4 (7040 if you count the trivial solutions) with sum 34, and the magic squares of size 5x5 with sum 65 are many many more, so I created this magic square generator to count them and verify the number of magic squares that others found. Because that number is so big and the program cannot generate all magic squares in a short time, you can can restart the counting from any point by choosing the numbers of the corners with the -c option, although it only accepts ordered numbers for the initial corners. The minimum corners that I have found that generate solutions is this:

$ ./magic-square -c1,2,5,22 5
  1 | 18 | 20 | 24 |  2 
----+----+----+----+----
 23 |  8 |  6 | 12 | 16 
----+----+----+----+----
 19 |  3 | 25 |  7 | 11 
----+----+----+----+----
 17 | 21 |  4 |  9 | 14 
----+----+----+----+----
  5 | 15 | 10 | 13 | 22 
...

To stop the program you can hit Control+C or wait until it reaches the last corners 22,23,24,25. The available options are shown by executing the program without arguments. The -q option counts the solutions without printing them. Happy searching!

Sunday, June 15, 2014

CSV calendar generator

csv-cal.c is a command-line calendar program written in C to generate files in format CSV (Comma-Separated Values). The output of the program can be redirected to a .csv file ready to be opened in a spreadsheet application like LibreOffice Calc in order to format and print the calendar as you like.
The program allows to choose the first month to show, how many months and how many columns to show. The first day of the week can be set to Sunday or Monday and the names of the months and the days of the week can be changed. For example, to generate a calendar for the year 2014 starting the weeks on Monday and using Spanish names, you can type:
csv-cal -m 2014 "do,lu,ma,mi,ju,vi,sá" "Enero,Febrero,Marzo,Abril,Mayo,Junio,Julio,Agosto,Septiembre,Octubre,Noviembre,Diciembre" >2014.csv
To run it you need first a C compiler to generate the executable file, and then you need to know how to execute it from the command-line console. Executing the program without parameters will print a help for the different options that it supports. Enjoy!

Sunday, January 13, 2013

Modern 10 PRINT ASCII-Art labyrinth generator

After reading a Slashdot post about the 10 PRINT one-liner maze generator, I spent many days thinking about how a simple random sequence of / and \ could make those labyrinths.
Anyone who tried to generate labyrinths from a program knows that it isn't an easy task, so I immediately wrote a program to test if that algorithm could be so good, and I got this output:
/\////\\//\/\//\\\\\/\//\\
\////\\\///\/\////\/\\/\/\
/\\/\\\///\/\/\///\/\/\/\\
/\/\\\\\//\/\\\\/\\\\//\\\
///\/\\\/\\///\/\/////////
/\//\/\//\\/\///\\\/\/\/\/
\////\\\/\/\\\\\/\/////\\/
/\//\/\/////\\//\/\\///\\\
\\\\///\\/\/\/\/\\//\\/\\\
///\\/\\//\\\\/\///\\/\\\\
Well, that wasn't exactly what I expected, and I quickly realized that there was a problem with the fonts, so I made some tests trying to change many parameters of the text in a word processor, without success. Modern "\" and "/" characters are not designed for such effect! And the separation of lines was also a problem.

Anyway, I was not satisfied yet, so I studied how to design a program to represent, generate and print such maze using horizontal and vertical characters, learning in the way a lot about this thing. Today I would like to publish my little program 10print.c for anyone interested. This was the result:
   |  ___  |   |   |   |  _______  |   |  ___  |  __
 | | |  ___|___|___|___|___  |  ___| | | |  ___|___ 
 |___| |   |   |   |  _______|___  | | | | |  ___  |
___  | | | | | | | |___  |  _______| | |___| |  ___|
   | |___|___| | |_______|___  |   | | |  ___| |  __
 | |___  |   | | |  ___  |  ___| | |___| |  ___|___ 
 | |  ___| | |___|_______|_______| |   | |___  |  __
___|_______| |  _______________  | | | |_______|___ 
   |   |   | |___  |  ___________| | |_______  |   |
 | |___| | |___  | | |   |   |   | | |   |   | | | |
 | |  ___| |   | |___| | |___| | | | |___|___| | |__
 | | |   | |___|_______| |  ___| | |___  |  ___| |  
 |___| | | |   |   |  ___|_______| |  ___| |  ___|__
_______|___| | |___| |   |   |   | | |  ___|___  |  
   |  ___  | | |  ___| | |___| | | | | |   |   | |__
___| |   | |___| |  ___|___  | | | | | |___|___|___ 
  ___|___|_______| |   |   | |___|___| |  ___  |  __
 |  ___  |  _______| | | | |_______  | |_______|___ 
 | |   | | |  _______| | |_______  | | |  __________
___| | |___| |   |   | |_______  | |___|___________ 
Much better! I haven't read the 10 PRINT book, but I enjoyed a lot doing this. Thank you for give me such an interesting problem!