Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Perl, please do my math homeworkby whakka (Hermit) |
on Oct 18, 2009 at 05:17 UTC ( [id://801833]=CUFP: print w/replies, xml ) | Need Help?? |
In the course of my math class in the engineering program I'm currently in it's often convenient to write up little programs that perform some calculation based on a function or algorithm. However, oftentimes a reasonable program makes calculations in a different way than humans might, and it becomes a challenge to parse the print statements I insert into the code to "show my work."
Well I finally decided to get around this problem after reading about inlining eval's in regular expressions while reading the Camel in a compromising position recently and faced with a particularly laborious exercise in my latest assignment: Find the number of partitions of 7 and 8 using the formula for the number of partitions of a number (all possible combinations of adding smaller positive integers): My first attempt was straightforward:
The output to this program though is actually harder to put together than doing the problem manually! The recursion is depth-first and so the program goes straight to the bottom of each branch, and yet it's more natural to evaluate one level at a time and combine. Fortunately, Perl rocks my world:
And the output looks like: Beautiful! Copied straight to the notebook. Update: For those interested in efficient solutions to this and other integer partition problems, Limbic~Region has this post.
Back to
Cool Uses for Perl
|
|