Desperately seeking Pythagoras

Also known as Project Euler problem 39:

If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.

{20,48,52}, {24,45,51}, {30,40,50}

For which value of p ≤ 1000, is the number of solutions maximised?

My first thought here was to generate an iterable collection of right angled triangles and accumulate a count of triangles by perimeter size. Don’t try this – it is far too slow an approach.

So my second thought was to try and iterate over the perimeter sizes and calculate the number of right angle triangles for each. It’s a long time since I had to think about Pythagoras but, for a triangle with sides a, b and c and a perimeter of p, I can make the following two statements:

a + b + c = p
a^2 + b^2 = c^2

Based on the second statement, above, I can also assert:
c = sqrt(a^2 + b^2)

Also:
c = p - a - b

Which means:
a^2 + b^2 = (p - a - b)^2

This can be expanded and simplified to state:
0 = pp - 2pb - 2ap + 2ab

or:

b = p(p - 2a) / 2(p - a)

Using this formula, we can say that for any values of p and a where p > a, if b is an integer then we have a right angle triangle.

One further optimisation: Intuitively, a can’t be greater than p/3.

Implementing this can then be done with two very simple for loops. The outer one iterates over all values of p and the inner one iterates over the range (2, int(p / 3)) and counts the number of integer values of b.

flattr this!

Use SQL To Update A Sequence Number

Oddly enough, I have encountered a couple of situations in the past where I have needed to regenerate sequence numbers in a table. The approach I took at the time involved populating the field in question with a value generated from the Relative Record Number.

This approach looks a lot neater, which is why I’m noting it here for future reference.

flattr this!

Enable NetworkManager in Frugalware

When I installed Frugalware, way back in August, I mentioned that the network Manager wasn’t starting up automatically. Starting it manually is easy enough, but it’s something that really should be starting up automatically.

Finally, after spending way too much time reading up on systemd, I have gotten around to doing this. Inevitably, the command to achieve automatic starting of the network manager is ridiculously simple:

# systemctl enable NetworkManager.service

… but I’m noting this here so I have something to refer back to for next time.

flattr this!

Neat trick of the day: Editing database files as if they were stream files

The background

Today I found and interface that works1 by copying a stream file into a database table and then attempting to parse the resulting mess. It’s been running for almost ten years and today it broke because some of the numbers in the stream file had decimal points in them2.

The interface in question is due to be retired shortly, so the program isn’t going to be fixed and the solution is to fix the data and then chase the folks that provided the stream file.

Have you ever tried to query or update a table that not only contains unstructured data but also has Carriage Return and Line Feed characters scattered throughout?

The solution

Work with Object Links is your friend.

This is the command you need:

WRKLNK '/QSYS.LIB/library.LIB/file.FILE/member.MBR'

The editing functionality is a bit basic, but it’s a lot easier than trying to database functionality to edit non-database data.

It’s also worth noting that the Work with Object Links actually knows what to do with Carriage Return and Line Feed characters.

Footnotes

1 In the loosest sense of the word.
2 Yes, really.

flattr this!

Killing queries with SQL

Back in the 20th Century, Query was a handy tool for… well, writing ad-hoc queries. The form-filling interface is remarkably simple – it really can be used by anyone – and rather inflexible. Now, of course, we have SQL – which is a lot more powerful – and a collection of third-party tools to help with the building your query.

But Queries exist. Some of them are on menus. They must be destroyed!

Here’s an approach:

RTVQMQRY QMQRY(querylib/queryname) SRCFILE(sqllib/QSQLSRC) ALWQRYDFN(*ONLY)

The Retrieve Query Management Query (RTVQMQRY) command allows you to retrieve Structured Query Language (SQL) source from a query management query (QMQRY) object. The source records are placed into an editable source file member.

But you don’t have to have a QMQRY object to use this command. ALWQRYDFN allows you to specify whether query information is taken from a query definition (QRYDFN) object when a query management (QMQRY) object cannot be found. There are three possibilities:

  • *NO: Never use the QRYDFN. If the QMQRY object doesn’t exist, the command will fail
  • *YES: If the QMQRY object doesn’t exist, use the QRYDFN
  • *ONLY: Ignore the QMQRY object and just use the QRYDFN

Once the command is executed, you will have a member (called queryname) in library/member sqllib/QSQLSRC, based on query querylib/queryname.

And now your options are endless.

flattr this!

5342 posts in 149 months

If you have been following this blog over the past year, you may well be aware that I have spent much of the year equivocating over whether or not to pull the plug on Pulpmovies.com.

The equivocation has come to an end and now the deed is done.

The main cause of my hesitating over this is sheer inertia. I launched the site in June 2000 (after some playing around with the free homepage providers of the time – remember Angelfire?) for a couple of reasons. One of these was that I wanted an excuse to teach myself some HTML. Another was that I was single, living in Amsterdam, and watching a lot of cinematic oddities which I really was far too keen to talk about. The result was an erratically maintained site that grew, started to receive review submissions, developed and then lost a small community, and finally started to become something of a chore.

Time marches on, circumstances change, priorities change and interests change and over the past couple of years I have found myself increasingly disinclined to spend my evenings sitting through yet another zombie film. Exploitation films can be fun but, like fast food, are best enjoyed infrequently and, quite honestly, I need a break.

I was intending to just put the site on hiatus but after some minor credit card shenanigans I was left with the choice of using PayPal or closing down the site. So I have spent a bit of time extracting my original content (I don’t really want to hang on to an archive of press releases) and downloading the image files, and now the plug has been pulled.

All that remains is for me to remove some links from my About page (and reword any references to Pulpmovies into the past tense) and then go delete some no longer maintained social media accounts.

But first, I have some bedtime stories to read.

flattr this!

Desperately seeking pandigitals

Also known as Project Euler Problem 38:

Take the number 192 and multiply it by each of 1, 2, and 3:

192 × 1 = 192
192 × 2 = 384
192 × 3 = 576

By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and (1,2,3)

The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5, giving the pandigital, 918273645, which is the concatenated product of 9 and (1,2,3,4,5).

What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product of an integer with (1,2, … , n) where n > 1?

This is a problem for which finding a practical approach took a lot longer than the implementation of said approach. It’s also a problem that forced me to think about the actual maths of the problem. I already have an is_pandigital function, which I wrote to help solve problem 32, so the main challenge here is to constrain the search enough that I can avoid endless looping.

So, a bit of analysis…

For n = 2,
9 * (1, 2) = 918
98 * (1, 2) = 98196
987 * (1, 2) = 9871974
9876 * (1, 2) = 987619752 <- That's 9 digits

Since the problem mentions the pandigital, 918273645, I plugged this into the calculator as well,
9182 * (1, 2) = 918218365 <- That's 9 digits again

For n = 3,
9 * (1, 2, 3) = 91827
98 * (1, 2, 3) = 98196294 <- 8 digits is not enough
987 * (1, 2, 3) = 98719742961 <- 11 digits is too many

So I now know that the number I am looking for is n * (1, 2) where n is between 9876 and 9182.

The implementation, therefore, is a very simple loop that checks is_pandigital(int(str(n) + str(n * 2))) for values of n from 9876 to 9182.

The result is almost instantaneous.

flattr this!

Note to self: Xorg and Frugalware

Just so that I don’t forget about this when Frugalware 1.8 is released:

xorg merge

Xorg upgrades have been merged to current. There is some manual intervention required due to strict version depends. You will need to remove some packages by force before you can complete your next system upgrade. This command should assist you in this:

pacman-g2 -Rd ffmpeg-compiletime ffmpeg-0.6 ffmpeg-0.6-compiletime

flattr this!

The joy of codecs

While I can be a bit snap-happy, I don’t tend to take a lot of videos which is my excuse for not realising for over a year that my camera records its videos in .mts format. (It’s also why it’s taken me six months to find the 27 minute recording of the inside of my coat pocket – but that’s another story.)

So the few family videos I have are big, beautifully clear, big and locked into a proprietary format. This becomes a problem as soon as I want to share any of these with the extended family, which brings me to the point of this post.

Public Videos is a (seemingly no longer updated) Posterous Space with a cornucopia of useful information on converting media file formats with free tools.

According to How to convert AVCHD MTS files to Dirac/Ogg, Theora/Ogg, H.264/MP4 and JPEGs using free tools, you can use ffmpeg2theora to convert a given video to theora ogv. The suggested command is:

ffmpeg2theora --videobitrate 1200 --keyint 30 --audiobitrate 128 --samplerate 44100 --optimize --two-pass -o $TARGET $SOURCE

I tried this and then tinkered around with the options to try to improve the resulting file. The main thing I learned was that I breathe really loudly when filming stuff. Ffmpeg2theora includes a lot of options and I need to find the time to go understand them properly but the point of this post is to ensure that I don’t lose the link.

flattr this!