Showing posts with label euler. Show all posts
Showing posts with label euler. Show all posts

Friday, December 10, 2010

Euler (more, again)

Today I solved Euler 25 and 48. Neither were particularly hard although I did have an integer offset fail incident for each of them.



I'm going in order of difficulty, not number order. Which is why my numbers are out of order.

Euler is a good way to distract myself from stuff sometimes.

Saturday, December 4, 2010

Euler 13

Today I learned about Ruby inject, otherwise known as a left fold. http://en.wikipedia.org/wiki/Fold_(higher-order_function)

nums.inject &:'+'
&: takes the ... and calls to proc which is a callable object. with the provided arguments... the result is that you can take a method name on one of the array objects (like +) and ...do it.
1.send '+', 2

Assuming that you've assigned nums[] to hold the data you're dealing with, then puts nums.inject(0) {|a, b| a + b} will get you the answer 9and then you just take the first ten digits)

A much less elegant way to do this is
sum = 0
for i in 0..nums.size-1
sum = sum + nums[i]
end
puts sum

Once you understand what the notation does, it's an elegant way of doing it. But the uneducated will be extremely confused. There is all sorts of evil to it.

Friday, November 12, 2010

Euler project 11..n

So, Euler problems 1-10 have increased my knowledge of ruby, python, and pain. It's been fun, and took longer than I wish it had. Obviously it's time to start on #11.

11. http://pastie.org/1306770 This could use some refactoring, but it works. It took me a bit to figure out, but I learned a lot, and didn't steal anyone's code. Although rancor did tell me that string.split existed.

16. http://pastie.org/1342335 This one was disturbingly easy and I don't know why. Maybe I'm getting better at Ruby. Or their ranking system is off. Something. Yes, I skipped 12-15 so I could do this one quickly and feel happy.

20. http://pastie.org/1342458 This one was very similar to 16. Doing one makes the other easy.

13. http://pastie.org/1348523 my brute-force solution.

14. http://pastie.org/1348526 my brute-force solution. http://pastebin.com/JVDVBL0b rancorbeast's elegantish solution.

Thursday, November 4, 2010

Euler Project

I'm doing problems from the Euler Project just now. My code is hideously embarrassing, but it's all my own and I'm kinda proud that I can write the stuff at all, considering that less than a month ago I might've looked at the problems and despaired, or at least made headway much more slowly.

My profile: http://projecteuler.net/profile/geeklinda.png I wish it said which specific problems I've solved...
  1. http://pastie.org/1273530 An alternate and very elegant solution from rancorbeast ((1..(1000 - 1)).reject { |i| i % 5 != 0 && i % 3 != 0 }).inject(0) {|a, b| a + b}
  2. http://pastie.org/1273527
  3. http://pastie.org/1273740
  4. http://pastie.org/1285750
  5. http://pastie.org/1290420 Code blatantly stolen from 17 Sep 2005 03:20 am post by Olath at http://projecteuler.net/index.php?section=forum&id=5 (can't access if you haven't already solved the problem) ... yes there is a discrepancy here. :)
  6. http://pastie.org/1285746
  7. http://pastie.org/1293957 (stolen from forums after getting answer)
  8. http://pastie.org/1285739
  9. http://pastie.org/1293953 (All mine. I'm proud of it. It runs pretty quickly. finding an answer took me several tries for some reason.)
    rancorbeast beat me by miles with the oneliner below.
    (1..500).each {|i| (i..500).each {|j| if i + j + Math.sqrt(i ** 2 + j ** 2) == 1000; print i * j * Math.sqrt(i ** 2 + j ** 2); end } }
  10. solved in python.
Euler 11..n will be in a new post.