Project Euler 5 by Exhaustion in Python

30 June, 2009

Here is my solution to the 5th problem of Project Euler, solved via exhaustion in Python. Please note that I am aware that this problem is more preferably solved by factoring, but programatic exhaustion can be “fun.” With a few added print statements, a file around 3 GB large could be outputted from this 4 kilobyte program.

This program is Python 3-compatible. To make it compatible with earlier versions, remove the parentheses from the final print statement. It is released under the CC BY-SA (Creative Commons Attribution-ShareAlike license).

5. What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?

divisors = range(1, 21)
i = 1

while i:
\t for x in divisors:
\t\t if i % x != 0:
\t\t\t break

\t if i % x == 0 and x == 20:
\t\t answer = i
\t\t i = -1

\t i = i + 1

print(answer)


Opera 10 Beta Review

12 June, 2009

I bet Opera has recently been getting a lot of attention after this article was put on Digg, and while I myself have already tried out Opera, I decided to take another look at it and to see all the good things Opera 10 Beta has to offer. There are a lot cool new features, such graphical tabs (including a Windows Vista-like preview where hovering over a tab let’s you see a preview of the page), 100% on the Acid3 test (although I still find it doesn’t render some pages as well as Safari or Firefox), and better speed, but I’m going to take a look at some of the sidebar features: what I would consider to be separate applications built into the browser.

Before starting, though, I’d just like to mention what’s one of very few cons with this (pre-)release: a few preferences are scattered around other menus when they really should be in Opera > Preferences, namely Appearance and Mail and Chat Accounts.

I’ve never really liked built-in support for e-mail and chat, but Opera implemented it well. When I open up mail in the sidebar, the current page being viewed is still intact. From this sidebar, I can check if I have any new mail. If I do, I can open it in another tab. Once I’m done, I just close the tab and am back to browsing. No more switching between applications. Now, while I’m still a big Colloquy fan, I find that IRC chatting has been implemented quite well. Your favorite (or rather, recently-connected) rooms are listed in the sidebar and all you need is one click to connect to them.

Screenshot of Opera 10 Beta

For jotting down links you want to go back to later but don’t exactly want to bookmark, the notes tab on the side allows you to jot those down, as well as other notes or reminders. There’s also a built-in address book, which makes keeping contacts less cumbersome than it tends to be on Windows and Linux distros (I still prefer OS X’s Address Book.app, though).

Overall, Opera 10 Beta a very nice and stable release whose final build I would certainly recommend, especially to Windows and Linux users. Don’t get me wrong; it runs very well on OS X, and is about sixty megabytes smaller than Safari, but I just find that user interface doesn’t fit in very well with the rest of the system. Other than that, it’s great and the features are wonderful.


“My” Snow Leopard Feature

30 May, 2009

While I doubt that I was the only one to recommend this, one of my submissions to Apple (see “Spotlight and the Finder“) seems to have been heard.

If I’m in my Documents folder and I start typing in the Spotlight toolbar item, I’ll want Spotlight to start searching within my Documents folder, not throughout my whole Mac. If I want to do that, I’ll either go into the root directory or search from the menu bar.

The following screenshot is taken from a now-removed YouTube video depicting Snow Leopard’s Finder preferences:

0

Thanks for the new feature, Apple!


Reflections on GNOME’s Sociological Research

20 May, 2009

I took a look at GNOME’s recent sociological research, which set out to organize information about worldwide GNOME users and to get some ideas about who uses GNOME, what they use it for, and so on.

To start off, I found the relations between countries and languages to be extremely interesting. From a developer’s point a view, this is great for knowing which language localizations should be more of a priority than others, what languages the project development or support team should have a basic hang of, and of the general future of the GNOME-using community. The number of Chinese users is almost the same as the number of American and European users combined.

It’s also easy to see that the GNOME community is being led by students and learners worldwide that want to know more, distribute more information, and have access to more information. The vast majority of users are currently attending a university or college, wish to learn more, and use GNOME not only as a work environment, but also for pleasure. This shows that it’s nice that GNOME is succeeding at reaching out to both sides of the spectrum.

Most of the users are connected to the internet, mostly with high-speed DSL or better. A lot use Windows as well and, by a small margin, tend to prefer desktop computers to laptops for working with GNOME and Linux. Many however, answered they’d like GNOME to follow in some of the steps of OS X: most notably, they want to be able to search for menu items within each program.

The GNOME team itself summarized a few important points, such as (just to name a few): more “professional” applications need to be written for the Linux desktop, GNOME should head for the cloud (integrating internet activities more prominently into the daily use of the desktop), the panels should be redesigned, and finding things (be it files, applications, etc) needs to be easier.

I hope GNOME 3.0 turns out to be an awesome release when it comes by, and that all the information gathered can be put into good use.


Learning Lisp

8 May, 2009

I’m about to venture off into the world of I consider to be less explored languages, and ones that I find have a great potential to be famous programming languages. I plan on learning Prolog, LISP, OCaml, and perhaps Eiffel.

I became interested in these languages when I read about a few of them on a post entitled “A Brief, Incomplete, and Mostly Wrong History of Programming Languages” that was on the Reddit front page today. I took my first step with SICP, which gave me a broader idea of what Scheme/LISP looked like, and now I’m planning on following Practical Common Lisp (link, as well as to other useful online programming books, can also be found on my sidebar).

Wish me luck on my new adventure!


Top New Features in gedit 2.26

25 April, 2009

The three main new features in gedit 2.26, which came along with Ubuntu’s new Jaunty Jackalope release are a fullscreen mode (which I personally find slightly lacking – only the text view is shown) and better use of the status bar in terms of being able to select the syntax highlighting language and tab width very quickly. All of these are really nice additions to the program, and I’m glad to see that gedit’s becoming more and more full-featured.

A screenshot of the new gedit version, 2.26.

A screenshot of the new gedit version, 2.26.


Working with User Information in Linux

21 April, 2009

Introduction
At certain times, it can be of advantage to have access to a user’s information, be it their home directory, their full name, their password, their system hardware, etc. This sort of information can be used to automatically customize programs according to the name of the user that started them or certain makeshift security methods.

Available User Information
If we take a look at /usr/include/pwd.h, we’ll see the following structure (with genuine comments):

/* The passwd structure.  */
struct passwd
{
  char *pw_name;        /* Username.  */
  char *pw_passwd;        /* Password.  */
  __uid_t pw_uid;        /* User ID.  */
  __gid_t pw_gid;        /* Group ID.  */
  char *pw_gecos;        /* Real name.  */
  char *pw_dir;            /* Home directory.  */
  char *pw_shell;        /* Shell program.  */
};

In Linux and most Unix systems, pw_gecos is the user’s real name, although you may encounter pw_comment every now and then.

Saying Hello
Let’s write a program that says “hello” to the user and tells them what their home directory is. We’ll need to include the following headers:

#include <sys/types.h>    // defines special types
#include <pwd.h>    // defines the passwd structure
#include <stdio.h>    // standard I/O

Now, let’s write the main function and define some basic variables:

int main ()
{
    uid_t uid = getuid();
    struct passwd *user_info;
    user_info = getpwuid(uid);

The uid_t type was defined in <sys/types.h> and getuid() is defined in unistd.h. This function is declared as uid_t getuid(void); and it returns the user identity (UID) of the user that started the process.geteuid() can be use to return the user’s effective identity (EUID – the user’s second UID that defines what resources processes begun by the user has permission to access. [1 http://unixpapa.com/incnote/setuid.html%5D). We then create a pointer to the passwd structure to be able to access the members denoted above.

The getpwuid() method is defined in <pwd.h> and gets the password file entry given a certain UID – in this case, that of the user that started the process. A password entry can also be returned based on a user’s login name; in this case, getpwnam(const char *name) is used.

Now, do the following to get the user’s real name and their home directory:

    printf("Hello, %s!\n", user_info->pw_gecos);
    printf("Your home directory is %s.\n", user_info->pw_dir);

Don’t forget the -> notation. It is very common in certain frameworks, most notably Qt. Remember that a->b is equivalent to (*a).b .

We can now wrap up the program and compile it.

    return 0;
}

Here are two different results:

$ ./a.out
Hello, Patrick Braga!
Your home directory is /home/patrick.

# ./a.out
Hello, root!
Your home directory is /root.

Getting System Information

Sometimes, especially in applications that include networking capabilities, it is useful to know the name of the OS a user is running, the release, and the version. One can also find out the name of the host and the type of hardware it’s running on (i386, i686, etc). With this information, an application can be optimized to run more properly on the specified OS or on the specified hardware type.

#include <unistd.h>
#include <sys/utsname.h>

The <unistd.h> file includes two important functions: int gethostname (char *name, size_t namelen) and long gethostid(void). <sys/utsname.h> includes the uname function and the returned utsname structure.

Here are the most important members of the utsname struct. with original comments intact:

/* Structure describing the system and machine.  */
struct utsname
  {
    /* Name of the implementation of the operating system.  */
    char sysname[_UTSNAME_SYSNAME_LENGTH];

    /* Name of this node on the network.  */
    char nodename[_UTSNAME_NODENAME_LENGTH];

    /* Current release level of this implementation.  */
    char release[_UTSNAME_RELEASE_LENGTH];

    /* Current version level of this release.  */
    char version[_UTSNAME_VERSION_LENGTH];

    /* Name of the hardware type the system is running on.  */
    char machine[_UTSNAME_MACHINE_LENGTH];

    // ...
  };

Let’s expand the above program to include the following statements (don’t forget to include the necessary headers):

    struct utsname uts;
    uname(&uts); // this is the function that returns the uts struct

    printf("Your computer is running %s on %s hardware.\n",
        uts.sysname, uts.machine);
    printf("You are currently running kernel v%s.\n", uts.release);

Here are two different results:

$ ./a.out
Hello, Patrick Braga!
Your home directory is /home/patrick.
Your computer is running Linux on i686 hardware.
You are currently running kernel v2.6.27.21-170.2.56.fc10.i686.

# ./a.out
Hello, root!
Your home directory is /root.
Your computer is running Linux on i686 hardware.
You are currently running kernel v2.6.27.21-170.2.56.fc10.i686.

Note that the system and hardware information remain intact throughout the users – the user-specific information remains the same.

Using Finger

It’s great to be able to access this information in a C program, but what about in a shell script, remotely logged into a shell, or whenever you need it quickly from the command line? The finger command allows us to do that. There are four options for finger:

  1. -s displays the user’s login name, real name, idl and login times, office location, and office phone numbers.
  2. -l (default) displays everything shown in -s and any information in certain hidden files in the user’s home directory
  3. -p displays -l information without information from said certain hidden files in ~/
  4. -m prevents matching user names to their login names

The mentioned hidden files are (in the home directory ~/) .nofinger (denies the user’s existence to requests outside the local host), .project (a one-line file of whatever project the user is working on), .plan (a multi-line file of the user’s plan of things to do), and .pgkey. Remember, in Unix systems, it’s perfectly all right that files carry no extension.

Conclusion
Needless to say, the possibilities for using this sort of information are vast, from making a user’s experience more enjoyable to fine-tuning an application’s services to fit the user’s hardware.