My work is the finger work.
samskivert

June 1, 2008

Euler 042

Problem 042:

object Euler42 extends EulerApp {
  def wvalue (word :Seq[Char]) = word.foldLeft(0)((s, l) => (s + (l - ‘A’ + 1)))
  def tri (n :Int) = (n*(n+1))/2
  println(readwords(”words.txt”).map(wvalue).filter(v => (v == tri(Math.sqrt(2*v)))).length);
}

This one is kind of cheating. I discovered (using the terminology tri = (n * (n+1))/2) that floor(sqrt(2*tri)) == n for at least the first 10,000 values of n. This isn’t hugely surprising because 2*tri = (n*n + n), so the square root of that is always going to be a bit bigger than n, and floor of that could well take us right to the integer we need. Who knew?

It may be true for larger values of n but BigInt doesn’t have sqrt() so I didn’t feel like going through the pain of checking. Certainly the biggest n we encounter in words.txt is way smaller than 10,000 (it is in fact 19).

Posted to euler by mdb at 10:59 am | Comments (0)       

Euler 041

Problem 041:

object Euler41 extends EulerApp {
  def perms (d :List[Char], n :List[Char]) :List[Int] = d match {
    case Nil => n.mkString.toInt :: Nil;
    case _ => d.flatMap(digit => perms(d.filter(digit.!=), digit :: n))
  }
  println(perms(”1234567″.toList, Nil).filter(isprime).foldLeft(0)(Math.max));
}

We save ourselves many CPU cycles by noticing that a pandigital number with 1 through 9 will always have digits that sum to 45, and similarly one with 1 through 8 will always have digits that sum to 36. Both of those numbers are divisible by three which means that no 8 or 9 digit pandigitals are prime.

Armed with that handy shortcut, we then generate all possible permutations of the digits 1 through 7, filter them for primeness and find the largest one. Voila!

This is sort of a gratuitous use of a case method, but it brings back fond memories of SML, so I didn’t fight the feeling.

Posted to euler by mdb at 10:09 am | Comments (0)       

Euler 040

Problem 040:

object Euler40 extends Application {
  val digits = List.range(1, 200000).flatMap(n => n.toString.toList).map(_-'0');
  println(List(0, 9, 99, 999, 9999, 99999, 999999).foldRight(1)((idx, b) => b * digits(idx)));
}

Another straightforward solution that’s nicely accommodated by our basic tools. Generate a list of the first 200,000 integers, split those into lists of each individual integer’s digits and flatmap them into one contiguous list. Then extract the digits at our desired indices and multiply them. It would have been more efficient to only do the character to integer conversion for the values we are multiplying, but then things didn’t fit so nicely onto two lines.

Had I had more indices to extract, I might have written:

  println(List.range(0, 6).foldRight(1)((idx, b) => b * digits(Math.pow(10, idx).intValue-1)));

but the whole Math.pow and intValue business felt like too much ugly Java intrusion.

Posted to euler by mdb at 9:40 am | Comments (0)       

Euler 039

Problem 039:

object Euler39 extends Application {
  def sols (perim :Int) = perim + 1000 * (for {
    a <- List.range(1, perim/2);
    b <- List.range(1, (perim-a)/2+1);
    c <- List(perim-a-b);
    if (a*a + b*b == c*c)
  } yield 1).length;
  println(List.range(3, 1000).map(sols).foldLeft(0)(Math.max) % 1000);
}

Nothing fancy here, just a moderately brute force search.

Posted to euler by mdb at 9:31 am | Comments (0)       

April 20, 2008

Lars and the Real Girl (3)

Charmingly bizarre. Likable but dysfunctional man mail orders a sex doll and earnestly tries to pass her off as his new girlfriend. Having the whole town attempt to just play along is a recipe for comedy. If your friends can’t put up with your aluminum and silicon imaginary girlfriend, then what kind of friends are they? The writer apparently saw an ad for the Real Doll and came up with this crazy premise. I have to admit that’s not what popped into my mind when I first saw the Real Doll, but perhaps I am at a gender disadvantage.

Posted to films by mdb at 11:33 pm | Comments (0)       

April 1, 2008

The God Delusion (3)

Dawkins was most interesting in this book when he was talking about biology and speculating about whether there are evolutionary reasons for humans to tend toward religious belief. Most of the rest was a bit long-winded. I am sympathetic to his message that society should acknowledge religion as a powerful tool for getting people to unthinkingly accept beliefs of any sort and its intrinsic danger. Perhaps there should be an international body akin to the IAEA to monitor the use of religion as a dangerous technology (and if that idea sounds absurd to you, I wonder if that is the religion meme protecting itself).

Posted to books by mdb at 6:27 pm | Comments (0)       

March 28, 2008

Scenes from a Marriage (3)

This was the first Ingman Bergman film I’ve ever seen and he pulls no emotional punches. This is a complicated movie about complicated lives. At times I loved the characters, at times I hated them, at no point did I forget that they were living in the 70s and their wardrobe was awesome.

Posted to ramble by mdb at 10:22 pm | Comments (0)       

March 27, 2008

Dersu Usala (3)

Another Kurosawa masterpiece, in this case starring a citizen of my favorite ex-nation Tuva. I have great respect for a filmmaker that is crazy enough to take a huge crew and make a vast, sweeping film in a enormous country, spanning multiple seasons, and still manage to capture the tenderness of the relationship between Dersu and the Captain. Still manage to capture the delicate longing for the untamed wild and the sorrow for its loss shared by those two men. My heart broke when the Captain turned on the snowy hillside to call out “Dersuuu” and was met with the reply “Capitaaan!”

Posted to films by mdb at 11:22 pm | Comments (0)       

Media Update

Bits
Camera Wrestling
Archives:

Powered by WordPress

samskivert.com ©2001 - 2005 Michael Bayne <mdb@samskivert.com>