Project Euler Problem #13
After I submitted my solution, I Googled around and found a fair deal of “lol, this one is easy!”. And, yeah, it is.
Add 100 numbers together and output the first ten digits of the sum.
So. Easy.
C# is persnickety about numbers and handling. Out of the box, C# does not like working with any number above either 232 (Int32) or 264 (Int64), and trying to shoehorn these massive numbers into .NET was a colossal (no pun intended) pain in the arse. My first C# program was up past 30 lines and two methods-and then even I had trouble handling the remainder; my approach with C# was to dump the numbers into an array and add one column at a time.
My second C# program implemented BigInteger. While it worked (it returned the correct figure), I had no way to implement the library outside of a Visual Studio 2010 project. Having to even launch VS is excessive overkill, so I closed out of that projected, and wrote a five-liner in Bash:
#!/bin/bash
A=13.txt
for i in {1..100}
do
B=`expr $B + $(sed -n "${i}{p}" $A)`
done
echo -n $B | cut -c 1-10
Categorised as: programming
Your code is short but cryptic to me.
My Problem 13 solution is in C# using BigInteger and a bit longer than your Bash code but very easy to read.
I love Visual Studio because I have a single solution for ALL PE problems I have done (63 so far) and all I will ever do. I have 16 folders and 25 problem classes in each folder.
I use ReSharper and StyleCop to keep my code free of code issues and ensuring that the style is maintained perfectly thru all problems.
I also use reflection and custom attributes. This allows me to change one location and when the problem runs to display the problem number and the description as it appears on the PE website followed by the answer and the time in milliseconds it took to run.
For a few problems bash is fine but for hundreds you want a different approach if only to be able to find and reuse code you’ve written before.
Just my opinions but I really wanted to respond to your comments.
Thanks,
–Mike
–Mike
Hey Mike,
I was a Linux power user/hobbyist admin for almost a decade before I went back to school to learn programming. I am fairly adept at using the Bash scripting language, even if I am occasionally rusty. It helps me keep fresh to jump back into the shell on occasion and bash something out there. Also, Project Euler doesn’t encourage the use of any one language over the other – and the sheer plethora of submitted code on the forum impressively demonstrates this. :]
For those two reasons, I take a “Whatever works!” philosophy toward the problems, so long as I’m coding my own solution and not just calling on an existing program to output the answer (Problem #19). A Bash solution for Problem #13 immediately popped into my head, so I wrote that instead.
To answer the other half of your question: I simply do not enjoy having to use Visual Studio. While it has all of the tools a .NET programmer needs…it has all of the tools. It’s a 200 kilogram gorilla with a crappy text editor. I use Sublime Text 2 to write my programs, and csc via a Cygwin shell to build it. I still receive all the feedback about errors, and I get to write code in a pretty, light, and distraction-free environment.
I organize my PE submissions by keeping the *.cs files in a Dropbox folder, and redundantly back them up by posting them here. :) I am also trying to avoid referencing older code where I can avoid it, because I recognize that I am learning at a very rapid pace. I might have a better method than the one I used last week, or I might want to cement some technique in my mine. I do reuse code where time is short.
Hope this helps!
Mark