local cuboids = []; for (local i = 0; i < 2000; ++i) { cuboids.append(0); } function gcd(a, b) { if (b == 0) { return a; } else { return gcd(b, a%b); } } function coprime(a, b) { return gcd(a,b) == 1; } function EuclidLeg1(m, n) { return square(m) - square(n); } function EuclidLeg2(m, n) { return 2*m*n; } function odd(x) { return x % 2 == 1; } function square(x) { return x * x; } function min(x, y) { if (x < y) { return x; } else { return y; } } function max(x, y) { return -min(-x, -y); } function countCuboids(l1k, l2k) { local pydiag = square(l1k) + square(l2k); if (l1k < 2000) { for (local i = (l2k/2); i > 0; --i) { if (l2k-i >= 2000) { return; } else { local d1 = square(l2k-i+l1k) + square(i); local d2 = square(l1k+i) + square(l2k-i); if (pydiag <= min(d1, d2)) { cuboids[max(l2k-i, l1k)] += 1; } } } } } for (local m = 1; m < 60; ++m) { for (local n = 1; n < m; ++n) { if (odd(m-n) && coprime(m, n)) { local leg1 = EuclidLeg1(m, n); local leg2 = EuclidLeg2(m, n); for (local k=1; k * min(leg1, leg2) < 2000; ++k) { countCuboids(leg1 * k, leg2 * k); countCuboids(leg2 * k, leg1 * k); } } } } local count = 0; for (local i = 0; i < 2000; ++i) { count += cuboids[i]; if (count > 1000000) { print(i + "\n"); break } }
Thursday, May 22, 2014
Problem 86 - Squirrel (and done with finals)
I finished my last final earlier this evening, so I will be free for a while to possibly solve Project Euler problems. I may find other things to pass my time, but nonetheless I expect I will get a good number done in the next couple of weeks. Anyway, as promised in my last post, I freed up Squirrel, so I used Squirrel on problem 86. Squirrel really is an easy language to script in. It's basically like python with curly braces. No wonder I keep coming back to it. Now that I am climbing in numbers, the code I write is starting to follow less and less directly from the problem. As the goal of this blog is to share my experiences using a bunch of programming languages, using Project Euler as the medium of the challenge, not to talk about Project Euler problems, I will not go into too much depth about why the code below produces a correct solution, though the reader can feel free to investigate themselves or read about it elsewhere. I will give a short summary of what's going on though. The problem asks about shortest paths on cuboids, but these shortest paths are along the hypotenuse of a right triangle if you unfold the cuboid. Anyway, with this, Squirrel is now my fourth most used language (behind only Python and J, which each have 5 problems solved, Squirrel is up to 4), and here is my solution, which runs in about 9.7s on my machine:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment