Sunday, December 19, 2010

Hello world

Cablegate: the game

A silly little game where one tags names, organisations, places, events and topics in the 'cables' (the diplomatic communications leaked by wikileaks). It isn't clear whether any of the tags are actually used by anyone, though. Can't hurt to browse a little, though :).


Unhosted is another interesting project I read about earlier today. Apparently, they want to distribute hosting over the web, to protect against vendor lock-ins and government shutdowns. They have a manifesto and a work-in-progress protocol. While their goal is very noble, I am unsure how they will achieve a fully distributed system aimed at something that can change on the spot (web 2.0 apps). Actually, their focus lies on the application itself, not the data. So the 'program' Google Docs or last.fm's radio and recommendation system, but not the data behind it. The idea seems to be to move all the processing to the client; the server is then only used for serving the code (that is, the program) and storing the data.

An interesting thought, but unlike many open-source projects (eg, the linux kernel, Django, Python, Firefox and so on), this project is unlikely to succeed for the very reason that this concept would take away control of the user from the company that controls the program; after all, if this becomes reality, saving you google docs document on one of Microsoft's services should be trivial!

Sunday, December 5, 2010

Wikileaks

Here in the Netherlands, the website joop.nl started a petition to call the prime minister Mark Rutten (of the VVD party) to save wikileaks. More specifically:
  • The site will be welcome in the Netherlands and protected against take-down actions
  • The arrest warrant against Assange will be ignored by our police
  • We will offer political asylum to Julian Assange
At the moment almost 5500 people signed the petition. The question is, will it end in time? According to the petition website, it wont be until the 3rd of March (2011). I just hope it isn't too late by then. Assange may be an asshole (at least, according to some sources), pushing wikileaks a little too much to the sensational corner, but the work of the website is important nonetheless.

The joop.nl article mentions one interesting point that will make this hard(er) to refuse on the part of our government: quoting the coalition agreement, "The government encourages a free and open Internet" (translation by google translate). I figure this is enough incentive, but you never know.

Saturday, November 27, 2010

Nederpopshow

Wow, the Nederpopshow start again. Why didn't anyone tell me!?

Seriously though, the second series of episodes apparently started last sunday (re-watch here, requires silver/moonlight). For those of you who aren't familiar with it; it's a TV show, quiz and live music, all around Dutch music history and current music. Last series, I discovered some awesome stuff there (among others, zZz and Daily Bread). They have plenty of variety; this particular show included the horrible de jeugd van tegenwoordig as well as someone called eboman (apparently an interesting figure in Dutch techno, with this song that could be described as sample heaven) and loads of other kinds of music. Which is what makes it so fun for me.

So yes. Go watch.

Thursday, October 21, 2010

Marnie Stern - Marnie Stern

Marnie Stern - Marnie Stern

Wow, more music from Marnie Stern. I clearly remember her debut; when I heard it the first few times, I really, really loved it. Couldn't stop listening, in fact. A while back I figured I would listen to it again (I actually wrote a 'review' at the time) but I never really got back into it.

And now there is this album. My initial feelings are again the same; this sounds like an album I'm going to thoroughly enjoy. I just hope it wont turn out like her debut in the end. I doubt it, however: Marnie took an entirely different direction. This album has much less shock value, is much less loud and has much more song. Thumbs up for Marnie!

Saturday, October 16, 2010

SINT

Guess what, I just read that the Dutch commission for commercials (Reclame Code Commissie) is going to research (in Dutch) complaints about the following ad for a horror movie:


Some background for those of you readers that don't know anything about Sinterklaas: it's a yearly event for children, roughly analogous to Santa (there are some minor differences; for example, there is lots of tasty traditional food such as pepernoten, various forms of chocolate and other candy, as well as other speculaas products). He appears on a white horse, with a large staff.

All this sillyness about a damn horror movie reminds me of that good ol' Futurama episode: A Tale of Two Santas. I'm not much of a movie guy, but perhaps this movie will be interesting :-). Meanwhile, go watch the trailer (again, in Dutch) here (or on the page of the news article). It's really hilarious.

Sunday, October 10, 2010

New Additions

After the Paramæcium post earlier today, I figured I might as well post other albums I also picked up:
Crematory - Awake
Melodic Death Metal with a LOT of keyboards and generally touches from gothic metal. Pretty fun. And no, it is not the same band as the Swedish death metal band that the cosmic hearse wrote about a month or two ago.

Vandal X - Two Man Army
I bought this mainly because I enjoyed this Belgian noise (rock) duo's 2008 album, All Lined Up Against the Wall so much.

Mahjongg - Raydoncong 2005
Last but certainly not least is this album, Mahjongg's debut album. This is some very solid dance-punk (at lack of a better genre name).

Röyksopp - Eple
With these four purchases, I received the Röyksopp single Eple for free, as well as a promo for some anti-aids compilation.

In another store, I picked up:

Beastie Boys - Licensed to Ill
I liked Paul's Boutique and given the legacy of this album.. well, 'nuff said.

Room Eleven - Six White Russians and a Pink Pussycat
Room Eleven was a fairly new Dutch jazz group with a very gentle sound. The band recently disbanded, and that's quite a shame.

I guess I will be writing something about the above albums in the near future.

New purchase: Paramæcium - Within the Ancient Forest

Paramæcium - Within the Ancient Forest


I remember when I was a sort-of-outsider kid, totally into the harder forms of electronic dance music. I just started the second part of high school (at the school I went to, the last three years were on another location), I got some friends that listened to metal. Soon, I came in contact with a lot of bands from various styles -- Vomitory, Arch Enemy, Nightwish, Slipknot, Darkthrone are those I remember clearly. Just after that, I went exploring on the web (broadband was a new thing back then). Soon, I found this style called 'doom metal', of which somehow I hadn't heard anything. And there it was - Paramaecium, with the album Exhumed of the Earth (though admittedly, there was also My Dying Bride). Seeing as this was the age of limewire, I had but a few tracks of this album. But amazing as it was.

Now, roughly six or seven years later, I stroll through a local music store and I see this cover. I couldn't read the logo on the front - I believed I found some black metal by a band called 'Within the Ancient Forest'. Only when I got home I realized- Paramaecium! And what a feast of doom, nostalgia and joy it is. Who ever said Christians can't make good music?

Friday, October 8, 2010

Thoughts: Sonic Youth - Daydream Nation

Sonic Youth - Daydream Nation
I finally took it upon myself to write my thoughts on this album down. I posted the result on RYM originally, but I guess I might as well put it here too. This is a cut-paste copy (except I've mentioned the band names for referenced albums):
Ah yes, Daydream Nation. Sonic Youth's 'landmark' album. After hearing (Sonic Youth) Confusion Is Sex / Kill Yr. Idols a few times, I picked this up. Sure, it was okay, but it never quite clicked for me. This one has been in the CD rack for a (too?) long time. Likewise, C=S/KYI has been buried deep in the depths of my digital music collection. Every once in a while, I dig around and decide I really should hear X again. So here was today's choice. Anyway, the experience is the same as previously: sure, I don't mind listening to it, and it has some areas that grab my attention, but they quickly let go again. Also, seriously, a double album? 70 minutes is just too long for most albums, including this one.

Listen to this for its historic value. Listen to this to hear some solid alternative rock. Yes, it is also noise rock, but in the 80s, noise rock had much more to say in the form of (Swans) Filth, James Chance and the Contortions or (High Rise) II. Daydream Nation isn't bad, it is just inseparable from the very, very high expectations.

Saturday, September 25, 2010

Filth

In honour of a new Swans album. Here's to it being as good as this.
Filthy motherfucking Swans, bitches.
Swans - Filth

Saturday, September 4, 2010

Bash scripting

I wrote a little last.fm related script today. Before you start bashing, I'll point out this is (one of) my first bash scripts. It takes as input an artist or band name (the target), along with an optional depth (D, defaults to 5) and width (W, defaults to 7). Starting from the target, it takes the first W similar artists, creates and edge (target, similar) for each. This is performed for each of the similar artists, until depth D is reached (where initial target = depth 0). This creates a graph of (at most): sum( (for i=0..D : W^(i)) nodes (so 1+7+7^2+7^3+7^4+7^5 for the default values). The tool dot makes it really easy to create directed graphs from edges; all I had to do was filter out duplicates.

I use the following tools (which I believe aren't strictly part of bash):
wget
sed
grep
dot (part of graphviz)

The script is here.

Wednesday, July 21, 2010

Flickr / Fear Factory - Mechanize

So yeah, remember my last post, where I said I was probably going to forget about the whole artwork thing? I totally lied about that. Here you can see the images of my copy of Fear Factory's Mechanize (see also below). Definitely one of my favorites as far as artwork goes. This version includes an extra track (Crash Test), as well as "special bonus content" (don't ask, I have no idea, it's probably some piece of shockwave or flash bloatware).

Fear Factory - Mechanize, unpacked

Anyway, the music is pretty cool too. I've known about Fear Factory for quite a while now ; I've had my period where tracks like Edgecrusher and Slave Labor were among my favorites. Around when this album was brand new, I remember downloading it, mostly out of nostalgia and slightly out of curiosity whether these guys actually made good stuff. After giving it a listen, I happened to stumble upon this version in the store. Impressed as I was, I bought it right away; and it was damn worth it. The only critique I really have is that the production is too loud, making this a little hard to listen to (no doubt this was intentional, but still). Samples on youtube: Mechanize, Powershifter. Other tracks I mentioned: Slave Labor (Archtype) and Edgecrusher (Obsolete).

New Music!

So on the trip to Norway I posted about in the previous post, I bought a bunch of CDs:

NAS - Illmatic
Nas - Illmatic

This has been one of my favorite hip hop albums for quite a while now. Sure, it may be a popular choice, but this album just gets hip hop stripped down to the essentials, focusing on them, working them out in great detail. It's a little bit of a grower, but the raw beats and (generally) interesting lyrics really make this shine. It's just bad-ass.

Dinosaur JR. - You're Living All Over Me
Dinosaur Jr. - You're Living All Over Me

Another album I enjoy quite a bit. Unlike Illmatic, which I play a lot, I don't listen to this album that often (at least, not before buying it). However, as I was going to see them live, I was so enthusiastic I picked this one up, remembering how I liked it when I first heard it. And it was definitely worth the money!

Meyvn - Splintered Skies
Meyvn - Splintered Skies

Here's one I had never heard of. However, for a mere 2.50 euro's and awesome artwork, I really couldn't leave it. I expected something symphonic metal, in the line line of Epica or After Forever, but instead I got something better; a lengthy progressive metal album with plenty of impressive guitar, synths and heavyness. A friend compared them with Dream Theater; I actually don't like them- but if he's right, maybe I should give DT another chance.

Lee Morgan - The Sidewinder
Lee Morgan - The Sidewinder

Another 'classic' that I hadn't heard in a while. The RVG edition of this album is impressive like all the RVG jazz remasters I've heard (Ornette Coleman, I'm looking at you). Though perhaps not as avant-garde as I usually like my jazz, this album still has plenty of variety to offer and is a must-hear if you're working yourself through jazz.

Anathema - We're Here Because We're Here
Anathema - We're Here Because We're Here
(the text is on a plastic sleeve)

And here is a new album. I got this one with perhaps a little too little thought; I actually thought I was buying an atmospheric metal album. Surprise soon made place for enjoyment, however; this album is extremely pleasing to the ears (Steven Wilson- you actually did a good job here). However, unlike Porcupine Tree, this album doesn't suffer much from boringness (or at least, not yet). Also, this album includes a DVD-A of the album (which I have yet to try). Pretty good, for the price of a regular new CD. Oh- did I mention the carefully crafted packaging?

And that reminds me of yet another task I will probably forget about: documenting my CDs, one post for each, with pictures and all that. If I ever do it, the Anathema album, Fear Factory - Mechanize, Sunn O))) & Boris - Altar, Sigur Rós - Takk.., Municipal Waste - Massive Aggressive, Nine Inch Nails - Ghosts I-IV, will probably be the albums that go first, since (my editions of) these all have awesome/non-standard packaging.

And again..

Hey.

So since I was in Tromsø, Norway in (more or less) the previous week, I haven't really written anything. What I've been doing there? Visiting friends, and going to the bukta festival (edit: the search at last.fm for events sucks! I finally found it here). Here's the complete program (discussion below):

DAY ONE
BIG STAGE:
17:00
Big Bang
18:35
Disciplines
20:05
The Sonics (US)
21:45
MEW (DK)

SMALL STAGE
18:00
Kråkesølv
19:25
Navigators
21:05
Hayseed Dixie (US)
NIGHT PROGRAM
DJ Snowball

DAY TWO
BIG STAGE
17:00
Danko Jones (CAN)
18:30
John Olav Nilsen & Gjengen
19:50
Jim Jones Revue (UK)
21:15
Datarock
22:45
Dinosaur JR. (US)

SMALL STAGE
18:00
The Hex
19:20
Let's Wrestle (UK)
20:40
Tellusalie
22:05
Mondo Generator (US)
NIGHT PROGRAM
01:00
The Goo Men

DAY THREE: MORNING (FREE)
BIG STAGE
12:00
Blacksheeps
12:55
Moddi
14:00
Hurra Torpedo

SMALL STAGE
12:30
Mining in Yukon
13:35
Jabba the Butt
14:40
Bits'n'Pieces

DAY THREE: NORMAL PROGRAM
BIG STAGE
18:00
Hellbillies
19:30
Clutch (US)
21:10
Juliette Lewis (US)
22:50
Sivert Høyem

SMALL STAGE
19:00
Bad County
20:30
[Ingenting] (SE)
22:10
Turdus Musicus
NIGHT PROGRAM
01:00
Senjahopen

Day One
Decent weather. A little chilly, but plenty of sun and no rain.
I missed half of Big Bang [sic], but they sounded good enough. Like all the bands on the small stage, this was listen-able. Disciplines, the third band of the day, was a different story. These guys (with one American member), playing "punk" in the vein of The Clash as well as more current-day "punk", had a pretty in-your-face sound as well as a great performance. However, I'd pick The Sonics (one of the bands I went for in the first place) as the best of the day. Old as they are, they still perform their fast garage rock with great charm. MEW, on the other hand, was not quite as good. I had heard of the band before, and I actually had pretty positive expectations from them, but it turned out to be a boring show, where everything drowned in bass and every song sounded like the last. Sure, it wasn't awful, but that's about all.

Day Two
Light rain throughout the evening. Pretty warm (for Tromsø standards; about 15-20 degrees C)
Again, I missed some of the first act, Danko Jones (who plays here every other year or so). This was some badass hard rock. Not spectacular, but the guy has a nose for entertaining the crowd. The following three bands (the hex, john olav nilsen & gjengen, let's wrestle) were all mediocre at best. Jim Jones Revue, a band I'd never heard of, proved to be quite a surprise; some pretty fast, blues-fueled rock they've got going. Great show, too. After an ignorable Tellusalie, the real feast of the festival began: Datarock, funky electronic rock, with an energetic show, followed by Mondo Generator. Mondo Generator did some badass covers of Queens of the Stone Age songs, as well as the Kyuss song Green Machine (one of my favorites) and played some hardcore-speed tracks. They probably take the prize of "best new-to-me band". Dinosaur JR. was a bit of a let-down; their music was high-quality, but not as noisy as I expected. Plus, their show was BORING.

Day Three
Warm day (~20 degrees C). Lots of sun. Great day for a festival.
Morning
I've only seen the last three bands; I heard some of Moddi in the distance, which had awful vocals. Jabba the Butt was an interesting group of two guys; drums and bass only. They played a little sludgy, heavy rock with an experimental edge. Before-last song, some random other guy popped up on stage; he did some vocals (without words). Funny, really. Hurra Torpedo was brilliant; they're basically a noise rock band that makes music with bass, guitar and kitchen equipment. Lots of percussion; covered Lady GaGa and that silly milkshake song I don't want to know. Hilarious, but slightly childish. Bits'n'Pieces was generic, boring poppy rock.
Evening
We skipped Hellbillies; Bad County was decent but not very interesting. Clutch was, well, FUCKING AMAZING. Not as heavy as Mondo Generator, perhaps, but more psychedelic. [Ingenting] was not really worth seeing; as was the headliner, Juliette Lewis. Apparently a famous actress, her music wasn't interesting at all. Turdus Musicus was okay; they played some kind of metalcore/post-hardcore. Pretty scene-y stuff, which I normally stay away from; but it was a kind relief from Lewis. The final act, Sivert Høyem, was pretty boring too. He played some songs from the band Madrugada, which were decent. We left early.

Overall, it was a great festival; however, the headlines were a little disappointing.

Friday, July 9, 2010

Signs of life...

.. through the most appropriate style of music for the occasion; death metal!

After taking a good while off playing Pharaoh (a city building game set in ancient Egypt), watching various seasons and series of Stargate (all of Atlantis and over a third of SG-1) and generally being covered in Egyptian mythology, the best start is probably...

... No, not Nile, actually. Nice try though. In fact, I'll post about Aeternam, a band that had their debut this year (though various regions of the internet seem to believe a 2009 release also exists, RYM does not). It's called Disciples of the Unseen and is in a strange region somewhere between (old) Arch Enemy, mordern melodic death and Nile. Though their melodic death is solid, the clean vocals and the non-metal influences are the prime elements that make this album impressive. I'm actually doubting whether the non-metal elements are related to Egypt, but suffice it to say the middle-east seems about accurate. Of course, there are some bands from this region (1000 Funerals, for example -- but that's for another post), but this one is not among them; they're Canadian. Either way, you can hear some here (out of these two I recommend Angel Horned).

Disciples of the Unseen:
Aeternam - Disciples of the Unseen

Next up is one of a pretty long list a friend of mine got me not too long ago; Autopsy's legendary album, Mental Funeral. Though this is probably blasphemy to death metal fans out there, I haven't heard it in the past five years I've listened to metal. Boy did I miss out. Doom-infused, raw death metal, something that has probably served as inspiration for Entombed. They've probably got some friends in Obituary, too. This definitely sparks interest for Autopsy's debut, Severed Survival.

Mental Funeral:
Autopsy - Mental Funeral
Severed Survival:
Autopsy - Severed Survival

Here's some more stuff I'll be (re)trying in the near future. If I feel I have something to say, I will post about them.
Benediction - Organized Chaos
Brutality - Screams of Anguish

Sonic Youth - Daydream Nation
Kraftwerk - Radio-Activity
A Life Once Lost - Hunter

Saturday, June 26, 2010

Hello. The following album is something you should try out if you enjoy psychedelic music, or when you want to see what southern Asia has to offer in terms of music.
Vilayat Khan - Raag Maand Bhairav
A truly fascinating three pieces played live by Ustad Vilayat Khan. In particular the last two pieces are of interest, though more for the rhythm section (tabla, played by Zakir Hussain).

Friday, June 11, 2010

Loud goodies.

Why hello there. This is a sort-of-tribute to all the awesomeness at the cosmic hearse blog. I haven't had time in a while, so I figured I'd post here some of the goodies I've saved up until now. Thank you, Aesop!

Check out this crust black metal goodness over at the cosmic hearse: Axeman - Arrive. I know it is probably intended, but it's a real shame this is on cassette and not on MP3 or CD. The production is pretty much non-existent. While it adds to the atmosphere, I'd like to hear this with decent production.

Liked that and need to be fed more punk and less black metal for a change? Grab some Japanese punk: 集団自殺 (syudan jisatsu) - Demon Priest. I think this one's a little slow-paced and maybe a bit too silly in terms of vocals, but it's still enjoyable. If you're digging the transition, you ought to go for GAI - Total Control, another fucking awesome japanese hardcore EP. Also, here is MOAR.

Syudan Jisatsu - Demon Priest

Speaking of Japan, while I was searching for something I stumbled on this Hanatarash EP:
Hanatarash - The Hanatarash and his eYe
Hanatarash is one of those bands that were on my mental check-these-guys-out list for quite a while and I'm glad I finally got to it. It isn't quite on par with Eye's work with Boredoms, but unlike my initial expectations, it isn't a wall of fucking noise everywhere. It has some really funny moments and some of the sound generation methods remind me of Tangent Precipitate (even though the influence probably goes the other way around). It also applies some breakcore-esque ideas in the background.

You want more gritty hardcore fucking punk, you say? I totally agree! Check out this wicked cover:
Wretched - In nome del loro potere tutto è stato fatto
The album is as badass as the cover looks. DO WANT.

Sunday, May 30, 2010

Master of Orion and other obsessions



So I yet again took one of my all-time favorite games out: Master of Orion, a turn based space exploration game by MicroProse. A screenshot:

It runs perfectly in dosbox, though I haven't gotten the sound working yet-- but that's a linux and lazyness issue (read: it worked fine on windows last time I tried, but this game doesn't really need sound anyway). A quick google showed me you can actually pick this game up, together with Master of Orion 2, for a mere 6 dollars (.). Or you could go to your favorite abandonia website and grab it there.

More nostalgia today:
Entombed, the first (metal) band I ever saw live. In a forgotten few sectors on my harddrive, there it was: Wolverine Blues:
Entombed - Wolverine Blues
A pretty damn awesome piece of rollin' death metal. For all those people who love AC/DC and secretly want death metal; for all those metal fans who secretly enjoy rock, I present to you a fine, fine album.

Sunday, May 23, 2010

Project Euler | Problem 6 (&7!)

Problem 6 is one of those where functional programming just makes things WAY too easy. Look at this:
squares x = [y^2| y<-[1..x]]
answer = (sum ([1..100]))^2 - sum(squares 100)

If you know how list comprehension (the [result | var<-generator;condition] part) works, and you know that [x..y] is all the natural numbers between x and y, including x and y, you can understand the above.
List comprehension works like this:
The generator generates one value. Each occurrence of var is replaced by the value, the conditions, of any, are evaluated, and if all of them apply, result is evaluated and "returned". Since functional languages like these are lazy, evaluation of a list comprehension only occurs when a value from the list is actually needed. Thus, one can define all prime numbers as follows:
allPrimes = [y |y<-[1..];isPrime( y )]
And use it as a list containing all possible primes (assuming the isPrime function exists). Just be sure not to call reverse or something on an infinite list like this. If you try to find the xth prime, all you have to do is allPrimes!x and the list will be evaluated up to the xth result.

[edit]
Holy crap, I almost answered problem 7 without even reading the question. Given allPrimes from above and isPrime from problem 3, we can do:
allPrimes ! 10001

And we're given the prime we want.
[/edit]


By the way, this album is fucking badass:
Ultramagnetic MC's - Critical Beatdown
The Cosmic Hearse, a blog I've followed for nearly a year now, posted this and I've been hooked ever since. Its sound is fairly repetitive, but Kool Keith is just really fucking solid and there are some great samples and beats here. One of the strongest rap albums out there, possibly.

Project Euler | Problem 5

Well, it's about time for the next problem! The solution isn't hard to find with pen and paper if you realize that what is asked is the least common multiple of the numbers 1 to 20. Since 20 is quite a small number, we can easily find this through the prime decompositions of all these numbers. Here's how it works for 1 to 10 (which, as given, gives 2520 as a result):
2=2
3=3
4=2*2
5=5
6=2*3
7=7
8=2*2*2
9=3*3
10=2*5

Note that any multiple n needs to be dividable by each of the above. In other words, the multiple n needs to contain the prime decomposition of each of the above numbers. Next, observe that 2 occurs at most 3 times, 3 at most 2 times, and 5 and 7 both at most once in each prime decomposition. Thus, the number we're looking for is 2*2*2*3*3*5*7=5*7*8*9=2520.
Analogously;
11=11
12=2*2*3
13=13
14=2*7
15=3*5
16=2*2*2*2
17=17
18=2*3*3
19=19
20=2*2*5

So the number is 2520*2*11*13*17*19=232792560. Surprisingly big number, isn't it?

The question remains, is this the most efficient method? Probably not, since determining the prime decomposition in the way I did in a previous post less than p/2*(n/2) modular operations and (p-1) divisions, where p is the amount of (non-distinct) primes in the decomposition and n is the value to be decomposed. The actual amount of modular operations is the multiplication of (n/p_i/2) for p_i=1,p_1,p_2..p_y, where p_1..p_y are the primes in the decomposition of n. I'm pretty sure this can be optimized to p/2*sqrt( n) by changing the condition in the while loop from x/2 to sqrt( x). Sounds efficient? I'd say so. You really don't want to use this to reverse an RSA public key, though: we know that in the above, p=2, but also that n=2^(2048) or n=2^(3072), for a reasonably secure key. that still means we need sqrt(2^(2048)) operations with this naive method, which is insanely huge. Nevertheless, it might be fun to try this method, or preferably, the more efficient method called Number Field Sieve, which is mentioned here.
To be continued?

Project Euler | Problem 4

This problem asks you to find the biggest 6-digit palindrome that is the product of two 3-digit numbers. I found this one quite boring, so I'll just put the code (in the functional language Amanda) I used to solve it here and not think about it too much.
condition x = (~.empty) [y | y <- [100..999] ; divides y x ; (x/y)<1000 ]
isPalindrome len val = (take len (itoa val))=((reverse.drop len) (itoa val))
pali6 = reverse [x| x <- [100000..999999]; isPalindrome 3 x]
answer = hd [x | x <- pali6 ; condition x]
divides y x = (x%y = 0)

Those familiar with haskell should probably be able to understand this. It takes about half a minute to evaluate, which is probably because I first calculate all the palindromes and then check them. Also, it should be possible to do the whole thing in one line, but it gets kinda hard to read that way.

Saturday, May 22, 2010

Project Euler | Problem 3

Ah, an interesting problem!
Problem 3 (clicky) asks you to find the largest prime factor of a pretty big number. Now, you could (like me) try to figure out how to do prime factorization efficiently. wikipedia has some really interesting articles on this subject (I might write a little bit about them later). However, compared to real prime decomposition problems (like in RSA), the primes are hard to find because the factors are close together and are really, really large, compared to the number in the problem.

So I decided to try the brute-force approach. First I tried to do it top-down, ie. count from the square root of x to 1 and return the first factor, find the factor of that and so on. There are two flaws in that approach: first, the decomposition of the biggest (possibly non-prime) factor does not always provide the biggest prime factor. Second, the difference between the square root and the biggest prime factor is pretty large (in this particular case, on the order of 1000 times bigger than the biggest prime factor).
In short, brute-force works better by counting from 1. Here's some python that does just that:
def find_factor(x):
if (x%2==0):
return 2
i=3
while i<(x/2):
if (x%i==0):
return i
i+=2
return x
def decompose(x):
res = []
while(x>=2):
f = find_factor(x)
res.append(f)
x=x/f
return res
print decompose(600851475143)

decompose finds the whole decomposition, while find_factor finds the smallest factor of a number, which is always a prime factor. This is easy to prove:
Assume x is the smallest factor of n, but x is not prime. That means x has some prime decomposition that contains p, p!=x. But that means p<x, and because x divides n, p divides n, and thus p is a smaller factor of n than x, which is a contradiction.
Finding this factor is pretty fast:
namnatulco@namnatulco-desktop:~/codestuff$ memtime python test.py
[71, 839, 1471, 6857L]
Exit [0]
0.02 user, 0.00 system, 0.10 elapsed -- Max VSize = 1616KB, Max RSS = 56KB

However, if we try to find the decomposition of 600851475133, it takes a lot longer:
namnatulco@namnatulco-desktop:~/codestuff$ memtime python test.py
[7, 13, 47, 140484329L]
Exit [0]
47.08 user, 0.26 system, 65.80 elapsed -- Max VSize = 7212KB, Max RSS = 3364KB
Moving on to the C code:
#include  //for printf
long long find_factor(long long x){
if (x%2==0){
return 2;
}
long long i=3; //we can start at 3, since 2 is the only even prime
while (i<(x/2)){
if((x%i)==0){
return i;
}
//even factors are caught by the initial if
i+=2;
}
return x;
}
int main(){
long long target = 600851475143LL;
long long f = 0L;
int count = 0;
printf("Decomposing: %lld\n",target);
while (target>=2){
f = find_factor(target);
printf("Factor%d:%lld\n",count,f);
target = (target/f);
count+=1;
}
return 0;
}

This is, as expected, really fast:
namnatulco@namnatulco-desktop:~/codestuff$ memtime ./a.out
Decomposing: 600851475143
Factor0:71
Factor1:839
Factor2:1471
Factor3:6857
Exit [0]
0.00 user, 0.00 system, 0.10 elapsed -- Max VSize = 1616KB, Max RSS = 60KB
And the other number:
namnatulco@namnatulco-desktop:~/codestuff$ memtime ./a.out
Decomposing: 600851475133
Factor0:7
Factor1:13
Factor2:47
Factor3:140484329
Exit [0]
1.26 user, 0.01 system, 1.70 elapsed -- Max VSize = 1616KB, Max RSS = 356KB
So the C code is about 35 times faster than python.

I've also tried to do this in Amanda, but it doesn't support long longs (I think it uses the C type long int underwater).

Thursday, May 20, 2010

Project Euler | Problem 2

Okay, so here's something about PE, problem 2 (clicky). Again, the problem is not very hard, it's just a matter of toying with writing efficient code (which is a pretty new idea to me, usually I only do this for more complex algorithms, rarely during actual coding). So, it's C time! Also, there will be some python, since Fibonacci is a nice toy example for generators.
Usually, I check for evenness with %2. However, C allows you to do bitwise operators while actually making sense (I've been told this is more of a pain in Java, but I haven't tried it in that language myself). So you can do this:
#include <stdio.h> //for printf
int main(){
long long sum = 0;
int curr = 2;
int prev = 1;
int tmp = 0;
unsigned long long limit = 4000000000;
printf("Up to: %lld\n",limit);
//loop through all fibonaci numbers, starting with 1 and 2
while (curr < limit){
if(!(curr&1)){ //if even, add curr
sum+=curr;
}
tmp=curr;
curr+=prev;
prev=tmp;
}
printf("Result:%lld\n",sum);
return 0;
}

Note the brackets, which are needed since ! takes precedence.
This is pretty fast:
namnatulco@namnatulco-desktop:~/codestuff$ memtime ./a.out
4613732
Exit [0]
0.00 user, 0.00 system, 0.10 elapsed -- Max VSize = 1616KB, Max RSS = 56KB


Now, on to python. This shows why generators are cool: they're like functions, except they yield a value, instead of return, and when they yield, the context is saved and on the next call, they continue. More on generators: in the python docs and on wikipedia.
def fib(first, second):
prev=first
curr=second
yield prev
yield curr
while True:
tmp=curr
curr+=prev
prev=tmp
yield (prev+curr)

STOP = 4000000

result = 0
for i in fib(1,2):
if i%2==0:
result+=i
if i > STOP:
break
print result

By the way, what wikipedia says about generator expressions ("Python has a syntax modelled on that of list comprehensions") makes sense if you're into functional programming (or mathematical set notation). So I figured I could do some Amanda here too, just for fun:
fib a b = a:(fib b (a+b))
f = fib 1 2

efib = [x | x<- f ; (x%2=0)]

res (x:y:xs) = x +y+ res xs, if y<4000000
= x , otherwise

main = res efib

Note that Amanda has lazy evaluation, ie. it only evaluates something when it needs that value. main= (...) simply executes the command when the code is loaded. fib is a function that generates all the fibonacci numbers, while efib is a function that picks out all the even ones with list comprehension. Fun fact: if we run efib by itself, it will generate an infinite list. If we interrupt the program and scroll back, we can see that: [2, 8, 34, 144, 610, 2584, 10946, 46368, 196418, 832040, 3524578, 14930352, 63245986, 267914296, 1134903170, 512559680, are all the even integers in the fibonacci series below whatever maximum amanda defines for numbers (beyond this, the number just wraps to negative).

Project Euler | Problem 1

Okay, so I'm going to try and learn some more languages, and improve my knowledge of those I already know. I'm going to keep a log of the code I write on this blog. I'll try and write the solution in as many languages as possible. Plus, maybe the fact that everyone can read my ugly code means I'll try and write things in a nicer way. This post-series is mostly just personal; I don't expect people to read this.

Anyway, lets get started with Problem 1. Of course, the basic solution is simple: walk over the numbers between 0 and 1000, check whether 3 or 5 divides it, add them to the sum. I figured I could play with some efficiency, but it turned out (clicky) that there is simply an x86 operator that does the modular operation, so there's not a whole lot to speed up here. Here's what I did in C:
#include 
int main(){
int i = 0;
int sum=0;
while (i<1000){
if ((i%3==0) | (i%5==0)){
sum+=i;
}
i++;
}
printf("%d\n",sum);
}

I'm kind of bothered by the fact that I need the include. But anyway, you can also do this without a division if you really want it. it's easiest to demonstrate with python:
rresult=0
for i in range(999,0,-3):
result+=i

for i in range(995,0,-5):
result+=i

for i in range(15,1000,15):
result-=i
print result

Of course, these numbers are pretty boring. I tried to scale things up a bit, so I modified the C code to use longs for the sum and had both go up to 1 000 000. Using memtime (a little tool to record time and used memory for whatever you feed it), I recorded the time each took. As expected, python is way slower:
namnatulco@namnatulco-desktop:~/codestuff$ memtime ./a.out
233333166668
Exit [0]
0.01 user, 0.00 system, 0.10 elapsed -- Max VSize = 1616KB, Max RSS = 56KB
namnatulco@namnatulco-desktop:~/codestuff$ memtime python test.py
233333166668
Exit [0]
0.27 user, 0.00 system, 0.40 elapsed -- Max VSize = 12472KB, Max RSS = 8544KB

However, if I change the python code to match what the C code does, this happens:
namnatulco@namnatulco-desktop:~/codestuff$ memtime python test.py
233333166668
Exit [0]
0.74 user, 0.00 system, 0.80 elapsed -- Max VSize = 7212KB, Max RSS = 3312KB

Surprising? Perhaps at first, yes. But if you know a little bit about python, you'll know it has a lot of C in the background. Looks like even though the range() function takes a large amount of memory, it's way faster than manual computation.

So that's it. Enough toying with code; go listen to this magnificent post-hardcore album:
Lungfish - The Unanimous Hour
There's a sample track (Vulgar Theories) for listening on the dischord website (take note: flash).

Tuesday, May 18, 2010

Python!

So someone asked me a little while ago how to get some set of files to be sorted on creation date and then have a number in the filename to indicate the sorting. I figured it'd be easy with bash, but being the bash newbie I am, I switched to do some python. Using some quick googling, I wrote the code down there.
It does things pretty simple: it uses os.stat to query the ctime, puts it in a tuple together with the filename, adds it all to a list, sorts that list and then copies the files to their new names.

import os, glob, sys, shutil
from stat import ST_CTIME
path = '.'

lijstje = []
for f in glob.glob( os.path.join(path, '*') ):
lijstje.append((os.stat(f)[ST_CTIME],f))

counter = 0
lijstje = sorted(lijstje)
for x in lijstje:
date, name = x
if counter <10:
dest = "00%d%s"%(counter,name[2:])
elif counter <100:
dest = "0%d%s"%(counter,name[2:])
else:
dest = "%d%s"%(counter,name[2:])
#print dest
shutil.copy(name, dest)
counter +=1


Here's what it does:


namnatulco@namnatulco-desktop:~/test/subdir$ ls -Al
total 4
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:44 a
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:44 b
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:44 f
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:44 hello_thar
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:44 test
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:45 testfile
-rw-r--r-- 1 namnatulco namnatulco 444 2010-05-18 21:45 test.py
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:44 x


I like to see what happens, so I commented the print line in the code out. Here's what it outputs:

namnatulco@namnatulco-desktop:~/test/subdir$ python test.py
000a
001x
002b
003f
004hello_thar
005test
006testfile
007test.py

And the resulting ls -Al:

namnatulco@namnatulco-desktop:~/test/subdir$ ls -Al
total 8
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:46 000a
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:46 001x
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:46 002b
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:46 003f
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:46 004hello_thar
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:46 005test
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:46 006testfile
-rw-r--r-- 1 namnatulco namnatulco 444 2010-05-18 21:46 007test.py
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:44 a
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:44 b
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:44 f
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:44 hello_thar
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:44 test
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:45 testfile
-rw-r--r-- 1 namnatulco namnatulco 444 2010-05-18 21:45 test.py
-rw-r--r-- 1 namnatulco namnatulco 0 2010-05-18 21:44 x


edit: yes, I know this is probably a lot easier in just plain bash, but I just like to play around in python. Besides, this'll probably work on windows :)