Problem 5 ver. 1: brute-force

March 6th, 2009 by Ivan Lakhturov | 0 | Category: Programming | Tags: |

What is the smallest number divisible by each of the numbers 1 to 20?

This is also quite known problem --- finding a least common multiple (LCM).

As usual, let's start with the brute-force solution.

  1.  

This is the most inefficient way we can invent for the task of finding LCM. The complexity in the worst case is something like O(p^n), when n divisors out there are prime numbers, each is p at the average (but different, of course, then their LCM is just their product). More strictly, the number of divisions is up to n*p^n in the worst case (would be that if not shorcutted logical operations in (all-divide?) function).

Don't install Linux onto /dev/sdc

March 5th, 2009 by Ivan Lakhturov | 0 | Category: Linux | Tags: |

I had three HDD drives, and I made a mistake installing Linux on the third one. I think, "the third" means that it hangs on SATA-channel 3. But GRUB sees a (first on that drive) boot partition at it as (hd0,0), if I choose the drive as the first to boot from in BIOS.

So, I removed one drive from a system, and my /dev/sdc became /dev/sdb. Of course, /etc/fstab still contains /dev/sdc* references and I get a kernel panic at boot. Actually, my GRUB also contains reference to the root partition at /dev/sdc, but this can be changed in interactive GRUB mode.

The issue can be solved changing /etc/fstab with a sytem rescue cd/usb, but I prefer to have no limitations on changing drives. I took a clean HDD, put it on SATA-channel 1 (it appears as /dev/sda), partitioned properly and copied everything from an old drive. Then edited /etc/fstab, grub-install --no-floppy /dev/sda with a proper grub.conf and it just works. Now I have two identical Linuxes on different drives, and I will probably keep the old one for a while (just in case).

By the way, at first I made a mistake copying with bare cp -r command. This made user accounts (except root) not usable, as access rights were changed. Copy with cp -a in this situation.

Problem 4 ver. 1: brute-force

March 5th, 2009 by Ivan Lakhturov | 0 | Category: Programming | Tags: |

Find the largest palindrome made from the product of two 3-digit numbers.

An additional function that filters out empty lists from a list of lists:

  1.  

The function that represents a number in arbitrary number system and the function that returns 10-base digits for a number (starting from the rightmost digit):

  1.  

(digits) function could use standard (number->string), but this generalized solution I like more. Now a solution to the problem looks as easy as:

  1.  

Notice, for comparing lists we use (equal?), not (eq?) and not (eqv?) - they would return just #f. However, we could use (eq?) instead of (null?) in (filter-empty-lists) to compare a list to the empty one. As regards number of operations, we have n^2 / 2 multiplications, as many (palindrome-number?) calls that create digits lists (up to 6 divmod per each) and compare them (up to 6 comparisons per each). So, the complexity is O(n^2), and memory use is proportional to the palindrome numbers density in the n by n matrix (half of which we actually iterate over to find an answer).

Problem 3 ver. 1 and 2: brute-force and optimization

March 4th, 2009 by Ivan Lakhturov | 0 | Category: Programming | Tags: |

Problem 3

Find the largest prime factor of a composite number.

is a very well known problem called factorization. The fundamental theorem of arithmetics comes in handy.

Brute-force in this case is just iterating from 2 upwards, searching for factors:

  1.  

If a factor is found, we divide the number by it, and continue with searching factors of a quotient. We don't need to start from 2 again (see the theorem).

The complexity for finding one factor is O(n) in the worst case. Let us optimize at once, stopping the iteration at sqrt(n). Indeed, a number cannot have a factor greater than its square root. This improves the worst case complexity drastically to O(\sqrt(n)) and this is the classical factorization algo.

  1.  

Now, having written additional function (max-list) for finding maximums through lists, we can solve the third problem:

  1.  

Problem 2 ver. 1: brute-force solution

March 4th, 2009 by Ivan Lakhturov | 0 | Category: Programming | Tags: |

I've written the generic procedure to generate lists iteratively:

  1.  

I've refactored (create-filtered-numbers-list from to filter) function into (numbers-filters) and added also (numbers) for future use:

  1.  

Now the brute-force solution for the problem 2

Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million

looks like this:

  1.  

The complexity is O(n). I decided not to generate a filtered list, but rather first generate, then filter, (fibonacci) function goes to the library for future use (for future functions I will not mention placing into my library). This is an optimal algorithm for generating a Fibonacci sequence, unlike computing the n-th member of it, which can be done more efficiently. However, for computing a sum of members, perhaps, there is a better solution, I will think about it later.

Code colorizer installed

March 3rd, 2009 by Ivan Lakhturov | 0 | Category: Miscellaneous | Tags: |

I found the Source Code syntax highlighting plugin for WordPress, which makes use of GeSHi highliter and installed it. Now to get proper colorizing (the default code-tag of WordPress looks pale) for Scheme code I embed it into <pre lang="scheme">...<pre> tags.

Actually, I dont like default colors and already created dups of templates for Scheme R6RS and Nemerle. But will elaborate those later.

There is also JavaScript-based highligher named SHJS, which can simulate different IDEs, i.e. has many templates for each language, but the list of supported languages looks somewhat narrower and doesn't contain Lisp / Scheme. Which is not a problem of course, but anyway, I don't want client-side colorizing.

Hacked the WordPress theme again

March 3rd, 2009 by Ivan Lakhturov | 0 | Category: Miscellaneous | Tags: |

Now you can see tags nearby postings. This is simply by adding the clause

<?php the_tags('<strong>Tags:</strong> ', ' | ', ''); ?>

to the <div class="date_author_comments"> in files single.php, index.php, search.php and archive.php of the WhitePress theme (version 1.1.7).

Backing up and upgrading WordPress

March 3rd, 2009 by Ivan Lakhturov | 0 | Category: Miscellaneous | Tags: |

Overlooked the manual for backing up. It seems, the easiest way of backing up a WordPress database, listed here is to use the appropriate plugin. Installation didn't bring troubles. Backed up the DB also smoothly, however, had to choose "save to server" option, cause "download to my computer" didn't work (in Opera browser). There are just 10 tables in WordPress engine. Upgraded the engine using the manual without a problem as well.

Problem 1 ver. 2: brute-force solution generalized to a list of divisors

March 3rd, 2009 by Ivan Lakhturov | 0 | Category: Programming | Tags: |

I refactored the previous solution and extracted such library functions, they could be useful in future:

  1.  

I think for these algoritms, where we have to iterate through large lists of numbers, the laziness (e.g. in Haskell) would give slightly more elegant code at certain places.

The first problem solution now becomes:

  1.  

For a list of divisors with length k the number of operations will be k*n. Actually, less than that because of shortcut (exists). Assuming k fixed and small, the complexity is still O(n). We could make use of the not yet written (create-numbers-list) function and filter that afterwards instead of the (create-filtered-numbers-list) function, but let us take less space in memory at once.

How to write and connect an own library in R6RS Scheme (PLT Windows solution)

March 2nd, 2009 by Ivan Lakhturov | 0 | Category: Programming | Tags:

It took me quite a while to understand. Today I'm using a laptop with Windows, so here is solution for the windows-version of the PLT Scheme implementation. Under Linux it will not differ much. By the way, to use R6RS in PLT DrScheme you need to choose Module language (Ctrl+L) and write #!r6rs in the beginning of each program.

First, write your own library. I separated the function (divides?) from the previous program:

  1.  

Second, save it under the path, for example, C:\Projects\projecteulersolutions\project-euler-lib.scm and, as it is explained in "C:\Program Files\PLT\doc\r6rs\Installing_Libraries.html", compile and install the library into collections folder by running the command (the first way explained there seems not to be working):

"C:\Program Files\PLT\plt-r6rs.exe" --install C:\Projects\projecteulersolutions\project-euler-lib.scm

Some files drop into the directory "C:\Users\User\AppData\Roaming\PLT Scheme\4.1.4\collects\project-euler-lib"

Third, you connect it to the program by replacing (import (rnrs)) clause with (import (rnrs) (project-euler-lib)). And, of course, delete (divides?) function form the top-level program body.

Fourth, before recompiling a library, don't forget to clean the folder:

rmdir /s /q "C:\Users\User\AppData\Roaming\PLT Scheme\4.1.4\collects\project-euler-lib"