Saturday, May 3, 2014

Problem 3 - Cat

So, notably 3 is a pretty small number, far smaller than the numbers I am dealing with right now in the 80s. However, problem 3 is kind of annoying, as it is a problem that involves factoring a large number, where large can be simply defined as just something that is too big to store in the 32-bit integers many languages use. Before today, my solution to problem 3 was in Fortran. While Fortran may not be my favorite language to work with, it looks like it does have pretty good support for rectangular arrays, which is what I need right now to work on the path-sum problems I have come upon. I solved this problem in a language which gave me some difficulty, but possibly that is only because I am a bit rusty. Cat is, as its name sort of implies, a concatenative language, or in more simple terms a stack-based language. After the haze in my mind cleared and I was able to think in this paradigm again, my main issues came from two facts: The "normal" Cat interpreter does not support large >32-bit integers, but the online interpreter does...the online interpreter, however is a bit hard to work with, being a little iffy at times, and indeed I don't entirely understand where the last major issue I had came from. Anyway, here is my solution to problem 3 in Cat, it runs in a pretty short amount of time, despite that amount of time being hard to measure due to its online-ness. Now this means reusing Fortran soon. Yay.
define dup2 { [dup] dip dup [swap] dip }
define func1 { dup2 dup * lt [pop writeln 0] 
[dup2 mod 0 eq [dup2 swap pop [div] dip] [inc] if] if }
600851475143 2 [func1] [dup 0 gt] while

No comments:

Post a Comment