samskivert
samskivert » 2008 » June

June 30, 2008

Stuff of Thought

Pinker continues to amaze and fascinate me. The mechanics of language and thought outlined in this book are the most compelling ideas about the mid-level organization of the brain that I’ve read anywhere. If I were a cognitive science researcher, I would be running off to model all of this in software.

Posted to books by mdb at 11:39 am | Comments (0)       

June 15, 2008

Uncertainty: Einstein, Heisenberg, Bohr, and the Struggle for the Soul of Science

The title will hardly win any awards for brevity, but it doesn’t misrepresent the book’s contents. These days, I love a little history and biography with my physics and Lindley didn’t fail to deliver.

Posted to books by mdb at 11:25 am | Comments (0)       

June 10, 2008

Why Beauty is Truth: The History of Symmetry

I loved Stewart’s Mathematical Recreations column in Scientific American, and this short history of the mathematics of symmetry (algebra and groups) was no less thought provoking and clearly communicated than the ideas in those columns. I’m fondly reminded of a math professor teaching Discrete and Combinatorial Algebra who actively suppressed any talk about group theory until we’d gotten through two semesters of preparatory material. He treated it like sacred ground that could only be trod after performing the proper ablutions.

Posted to books by mdb at 11:06 am | Comments (0)       

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)       

MDB

Bits
Ludography
Reviewlets
Camera Wrestling
Archives:

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