## Thursday, February 4, 2010

### A Scala quine

A Scala quine (credit doesn't go to me):

object Q extends Application{val s="object Q extends Application{val s=%c%s%1\$c;printf(s,34,s)}";printf(s,34,s)}

## Wednesday, February 3, 2010

### Project Euler #27: Quadratic primes

Here is problem #27.

Again, judicious application of elementary number theory can reduce the search space.

First, notice that the restriction that $$f(0)$$ is a prime immediately constrains $$b$$ to be a prime.

Furthermore, $$f(b)$$ is definitely NOT a prime, as:

$$f(b) = b^2 + ab + b$$
$$f(b) = b(b + a + 1)$$

When considering a polynomial, then, $$0 <= n <= b$$, $$-1000 < a < 1000$$, and $$b$$ must be prime. We are given that for values of $$b \approx 1601$$, only 80 consecutive primes are produced, so the search space is already thinning with this very naive analysis.

Further analysis could probably tighten the bounds on $$a$$, but with just these bounds, a solution can be found in under 10 seconds.

Using a precomputed list of primes, this Ruby program finds the solution in approximately 6 seconds:

def f(n, a, b)
n*n + a*n + b
end

def count(ps, a, b)
(0..b).take_while { |n| t = ps.include? f(n, a, b) }.count
end

def primes
# Grab a file of the first few primes to make calculation
# easier.
file = File.new("primes.txt")
set = Set.new
while (p = file.gets)
set.add(p.to_i)
end
file.close
set
end

b_max   = 1000
ps      = primes
bs      = ps.take_while { |b| b <= b_max }
bs      = bs + bs.map { |b| -b }
a, b, c = 0, 0, 0

bs.each do |b_i|
(-b_max..b_max).each do |a_i|
c_i = count(ps, a_i, b_i)
if c_i > c
a, b, c = a_i, b_i, c_i
end
end
end

puts a*b

## Monday, February 1, 2010

### Windows: Is it oem or retail?

I have a bunch of Windows installs, and I can't tell if they are OEM or Retail installs. Usually they are OEM, but sometimes I'm surprised. There is a quick and easy way to tell (within Windows) if it's OEM or retail:

1. Go to My Computer -> Properties -> General
2. Check under the "Registered to:" field and look at the product ID, which is separated into 4 groups of characters
3. If the 2nd group is all numbers, it is Retail, otherwise it will say OEM

### Project Euler #67: Maximum path sum II

Here is the question:

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.
3
7 5
2 4 6
8 5 9 3
That is, 3 + 7 + 4 + 9 = 23.

Find the maximum total from top to bottom in triangle.txt (right click and 'Save Link/Target As...'), a 15K text file containing a triangle with one-hundred rows.

NOTE: This is a much more difficult version of Problem 18. It is not possible to try every route to solve this problem, as there are 2^(99) altogether! If you could check one trillion (10^(12)) routes every second it would take over twenty billion years to check them all. There is an efficient algorithm to solve it. ;o)

A clever algorithm gives the answer in $$O(log(n))$$ time.

Consider this smaller problem:

1
2 3
4 5 6

Once you arrive at element 2, you will only ever want to move to number 5, and never 4. Thus, the triangle can be reduced to this:

1
7 3
5 6

Similarly, when you arrive at 3, you will only ever want to move to 6, so this is the new reduction:

1
7 9

So lastly, when you reach element 1, you will only ever want to move to 9, giving max sum to be 10.

This is the strategy employed to solve this problem, and here is a solution in Scala:

import scala.io.Source
import scala.Math._

val data = Source.fromFile("problem-00067.txt")
.getLines
.toList
.map(_.trim.split(" ").map(_.toLong))
.toArray

for (i <- data.length - 2 to 0 by -1;
j <- 0 until data(i).length) {
val left = data(i + 1)(j)
val right = data(i + 1)(j + 1)
data(i)(j) += left max right
}

val result = data(0)(0)
println(result)

Credit goes to Jessica Rhee for this solution.