Friday, April 17, 2015

Some findings on basic recursion and data type limits.

Going off of CodingBat's first recursion problem, factorial (which can be found right here), I implemented the recursive method into Eclipse and made a working program out of it.

You can view/download the program by clicking here.

While making this program, I realized some interesting things about doing factorials in Java. The main problem is that, if you only use the int data type, the highest number the program can successfully compute the factorial of is 12. Anything past that is computed either negative, incorrect, or just plain old 0

When I changed everything to long, it became possible to calculate factorials up through 20. Anything past that continues to be calculated incorrectly. For example, inputting 21 causes this to happen:




Just goes to show how huge factorial numbers are!

EDIT: Just added a bit of code that splits numbers into groups of three, like this:


All it took was a little imported function.

No comments:

Post a Comment