I've taken ill for the last two days, so I've been working on a couple Project Euler problems in between trips to the bathroom.
Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n = 0.
Since you start with n = 0, you know that b always has to be prime in order to satisfy n = 0.
Next, if b must be prime, and all primes greater than 2 are odd, and we don't care about expressions resulting in less than 3 consecutive primes (example expression has 40), then we know that all values of a must be odd in order to satisfy n = 1!
I also suspect that for the number of consecutive primes we need to 'win', that we only need to look at negative values of a, but I'm having a heck of a time trying to prove it.
require 'prime_generator'
# pre-calculate primes
MAX = 1_000
primer = Prime_Generator.new MAX
primes = primer.stack
product = 0
highest = 0
# a must be odd
(0..MAX).each do |i|
next if i & 1 == 0
# b must be prime
primes.each do |b|
# a can be positive or negative
[i,-i].each do |a|
n = 0
while n += 1 do
break unless primer.is_prime?(n ** 2 + a * n + b)
end
if highest < n
highest = n
product = a * b
end
end
end
end
puts product
And here's the prime generator I'm using:
class Prime_Generator
attr_reader :stack
def initialize max = 3
@stack = [1,2,3]
fill_to max
end
def fill_to max
n = 1
while true do
n += 4
return @stack if n > max
@stack << n if is_prime? n
n += 2
return @stack if n > max
@stack << n if is_prime? n
end
end
def is_prime? n
return false if n <= 0
max = Math.sqrt(n).floor
fill_to(max + 1) if max > @stack.last
@stack.each do |i|
next if i == 1
return true if i > max
return false if n % i == 0
end
true
end
end
You can find more Project Euler solutions here: https://svn2.assembla.com/svn/joe-zack-personal/projects/euler/ruby/
Tags: project euler, ruby
After spending a few dozen hours playing The War of the Roosevelts, I realized that there was a wee bit of room for improvement.
Now, there's like cards and stuff.
Enjoy.
Here's the source, but I still won't waste my time looking up the svn link.
Tags: flex, game programming
I've been tinkering around with C# and Flex a lot this year, but I haven't been posting much.
So in the interest of posting *something* I give you...
*drumroll*
You, standing in the (uneven?) shoes of Franklin D. Roosevelt, finally get a posthumous chance to stand up to your bullying older cousin Teddy in a game of WAR...well, at least the game of war as I knew it growing up. Wikipedia's got it's own ideas.
I lost interest in it by the time I got to the gui, so that aspects (aka as the part people actually see) is a bit...rough. Just a bit. ENJOY!
Here's the source, but I won't waste my time looking up the svn link.
Tags: flex, game programming
I ran into a problem the other day where I needed a way to temporarily store data between page requests. Typically I'm able to stash this sort of thing in the session scope, but these requests originated from different sources and I prefer to avoid (de)serializing when I can.
Instead I set up something akin to one of those pay-per-use lockers. You stick your items in the bin, drop in a few quarters, and take the newly unlocked key. Later you come back and use that key to retrieve your items. Your key is now 'locked' back into the starting position and the cycle begins anew.
Like one of those pay-per-use lockers you just stash your data, save they key, use the key, trash the data.
Simple as pie.
This isn't the sort of thing that comes up often, but should it arise I've got just the tool for the job!
Example Usage:
// initialize the locker
application.cacheLocker = CreateObject("component","cacheLocker").init();
// store some arbitrary data
key = application.cacheLocker.store([1,2,3,4]);
// then retrieve the data using the saved key
// throws a CacheException if the key doesn't 'fit'
arbitraryNumbers = application.cacheLocker.retrieve(key);
The data is destroyed after being retrieved; it's a one time only locker.
There are two important things to keep in mind when using this utility:
Tags: coldfusion, utility
Dummy Image!
It's a cool site that lets you pass in a width and a height to generate an image which could come in really handy for quickly mocking up web pages.
I can't believe I never thought of doing something like this!
ColdFusion is a great language for doing this sort of thing, so it was trivial to whip something up quickly.
I was originally writing a file to disk because I didn't realize you could stream an image variable to the browser, but my other co-worker Jim came to the rescue with the cfimage "writeToBrowser" action!
<cfset params = listToArray(cgi.query_string,"x") />
<cfif arrayLen(params) lt 2>
<cfthrow message="Input should be like ?[w]x[h]"/>
</cfif>
<cfset width = params[1] />
<cfset height = params[2] />
<cfset color = "gray" />
<cfif not (isNumeric(width) and isNumeric(height))/>
<cfthrow message="Width/Height should be numeric ?100x100">
</cfif>
<cfset image = ImageNew("", width, height,"rgb", color) />
<cfset ImageDrawText(image, "#width# x #height#", 0, 10)>
<cfimage source="#image#" action="writeToBrowser"/>
I'd love to link an example to you, but my hosting plan doesn't support _cf_image_
Tags: coldfusion