Java.writeMyCode() – Vol. I – BigInteger

Programming contests are very popular in Texas and most areas of the state host at least 1 each month.  They are great vehicles through which to increase enrollment and to get students excited about “cranking code”.

It is very important to know what tools are available in your language before attending a contest.  If the language being used provides a tool to help solve a problem, then teams should use that tool as part of the solution instead of writing every single line of code from scratch.  I stress to my students to never re-invent the wheel.  If the language already has a perfectly good wheel, put that sucker on your car and hit the road.

Java has lots of cool stuff built-in.  BigInteger is a class that has many methods in it that can make solving certain math related contest problems much easier.  BigInteger contains an isProbablePrime() method.  This is quite useful at contests.

Using BigInteger in Java

Using BigInteger in Java

It is equally important that student understand the algorithm being used to determine if a number is prime and how to create such an algorithm.  Learning the fundamental algorithmic concepts is critical to being a well rounded CS student, but for contest purposes, it is all about speed and accuracy so using built-in methods where possible helps save time and prevent errors.