Friday, August 8, 2014

Problem 99 - Mathematica. One more to the big 100

Mathematica is a language that many have been telling me to use for a long time. My aversion to it has mostly been because I was not sure whether it really counts as a programming language, or is it something else entirely? Well, I decided that I might as well succumb to common belief and say that it is a language, and problem 99 gave me a problem to solve in this language that was pretty appropriate for Mathematica. Processing relatively large data sets and doing somewhat high precision floating point arithmetic with logarithms? Sounds like a good place to use Mathematica to me.

I have used Mathematica very, very little in the past (twice for a physics class), but I had few difficulties with this problem. My only real difficulties were unfamiliarity with the user interface of Mathematica, and my inability to find any relative path for the data file (note for anyone deriving information about my filesystem from the absolute path, this problem was solved on a very infrequently used Windows partition of my laptop)

Hopefully tomorrow (or, oh, look at how late it is, later today), I will be ale to reach 100 with a yet to be determined language.

I didn't feel like figuring out how to do timing in Mathematica, but the below code runs pretty quickly on my machine.

ans = 0
maximum = 0
list = ReadList["C:\\Users\\Sam\\Desktop\\Euler\\base_exp.txt", Number]
For[i = 1, i <= Length[list], i = i + 2,
 next = Log[Part[list, i] * 1.0] * Part[list, i + 1] ;
 ans = If[next > maximum, (i + 1) / 2, ans];
 maximum = Max[next, maximum] ]
Print[ans]

No comments:

Post a Comment