Problem 5, from Project Euler:
{ 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? }
So this one gave me a hard time... At first I thought about using hashes, but then I figured that good 'ol arrays would do me just as well.. with less memory used? I'm still not 100 percent sure on this issue, being a young and ignorant programmer. Probably, for programs that preform these simple tasks the amount of allocated ram is insignificant, but I got one eye out for that home boy.
Anyways, at first I thought recursion would do me well. But with a little more thought I realized that there's a more simple solution. Here's what I came up with:
@primes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47]
@lcm_primes = []
def lcm_of?(n) #n must be array
prime_is = false
product = 1
temp = n
@lcm_primes.clear
while temp != []
@primes.each do |prime|
n.each_with_index do |e, i|
if e % prime == 0
temp[i] = e / prime
prime_is = true if !prime_is
end
end
@lcm_primes.push(prime) if prime_is
prime_is = false
temp.delete(1)
end
end
@lcm_primes.each{|e| product = product * e}
return product, @lcm_primes.sort
end
prime_is = false
product = 1
temp = n
@lcm_primes.clear
while temp != []
@primes.each do |prime|
n.each_with_index do |e, i|
if e % prime == 0
temp[i] = e / prime
prime_is = true if !prime_is
end
end
@lcm_primes.push(prime) if prime_is
prime_is = false
temp.delete(1)
end
end
@lcm_primes.each{|e| product = product * e}
return product, @lcm_primes.sort
end
I realize there are a few limitations here. For example, the program is limited by @primes. For larger number ranges, there will be larger primes, which will cause the program to hang indefinitely as if e % prime == 0 searches to divide a prime by something other than itself. Otherwise, it’s apparently efficient.
Ruby impresses me. Probably for its conventions mostly, which really don’t matter (as you can see from the code above). I guess I break a lot of the rules, but what I like most about the language is its readability. Once your feet start getting wet, preliminary coding starts rolling off the fingertips. However, I sometimes wonder if ruby takes a “programming for dummies” – literally, idiots – form… or if this solution just makes me mad because it is so much more to the point:
require 'rational'
num = (1..20).inject(1) { |result, n| result.lcm n }
puts "Smallest evenly divisible number is #{ num }.”
OK, so my solution does more than what’s required. But really? Is this in the spirit of puzzle solving?
I’m just bitter.
_____
In the spirit of gripes, however, have you been keeping up to date with your crime drama? In light of this whole Wikileaks ordeal (whatever the fuck a “wiki leak” is), I’ve been noticing how frequently authorities attempt (and succeed most of the time) to keep information from the press, and this alarms me. Which is, of course, old news… and practical: as the profilers on Criminal Minds often reveal to us, sometimes publishing the serial killer’s misdeeds will only result in more havoc.
For a moment, though, consider the fact that pulp television is simply a reflection of what the perceived viewers of any particular show already know and understand. You may argue, “but these shows, especially crime drama, are based on factual information.” And you would be correct, but it’s only based on the facts, and that’s why searching a database for any supposed criminal takes mere seconds on the screen. It just goes to show that real-life makes for bad cinema. Who wants to watch some actor-scientists sitting around eating Chinese food waiting for a computer to produce results that may not exist?
What I find interesting about all of this is that the facts only exist as generalizations of what is real, are only perceived facts with absolutely no actual basis in reality. When Eliot Spencer interrogates a suspect, we are entertained by this drama and it is only drama because that’s what ‘might’ happen if we were being interrogated. Actually, this works both ways, as in the role of the interrogator as well. But it is this catharsis that defines any interrogation scene as dramatic, otherwise it would be comedy, a lampoon of interrogation. Which happens quite often when watching CSI Miami, a show that should for all intents and purposes be considered crime-comedy.
The frequency of the dramatic authorities hiding any news from the press (read: the public), be it fact, pseudo-fact, or fiction resembles some already pre-conceived notion of government: to protect the masses, some information must be obfuscated until the problem is solved. Notice that the viewer base need not actually believe this to be true. Keeping information from the press only exists as an element of the story as a whole, which typically must be somewhat believable, or relatable, to be dramatic. Criminal Minds has prepared us for Wikileaks, or in other words: we have prepared ourselves for reality by relating to a rather baseless generalization of reality.

No comments:
Post a Comment