Java Help
November 23, 2011 11:58 AM

Programmingfilter: I created a program to output Pascals Triangle in Java (in Arrays). How can i change the shape of the output from a Rightsided triangle into a (typical for a Pascals Triangle) Isoceles shape?

Do i have to change something inside the System.out.print() command or do i need to do something beforehand?


-Also bonus question, how do i go about sorting out all of the prime numbers or all of the non-prime numbers out of an array?

Thanks!
posted by freddymetz to Computers & Internet (9 answers total) 1 user marked this as a favorite
Is this for a homework assignment? If so you may want to try to figure these sorts of things out on your own since struggling through these sorts of problems with trial and error is part of what helps you become good at programming.
posted by burnmp3s at 12:14 PM on November 23, 2011


I remember people doing this problem (including the pretty-printing) 35 years ago. We joked that it was "Pascal's Sandpile," since that's what it looks like when the numbers get big.

I think you may need to calculate twice: once to figure out how wide the bottom row will be, and again to print everything out at the right width. (Or you could store the calculation results, but it's so cheap to recalculate, I'd fall on the "save memory, burn CPU" side of the fence.)

I don't do much in Java, but java.util.Formatter appears to be what you need...
posted by spacewrench at 12:19 PM on November 23, 2011


I've been attempting it for a while. If you don't like giving straight up answers, how about some pointers or tips?
posted by freddymetz at 12:20 PM on November 23, 2011


thanks space wrench! will try that!
posted by freddymetz at 12:21 PM on November 23, 2011


Copy and paste the output of your program into a text editor. How would you change the shape? By adding spaces at the beginning of each line, right? So modify your program to compute the right number of spaces for each row of the triangle. You'll almost certainly want to first pad each number out with spaces so they're all the same width.

As for your second question: the slow and simple way is trial division. For a somewhat faster approach, look up the sieve of Eratosthenes.
posted by teraflop at 12:28 PM on November 23, 2011


Sieve of Eratosthenes is the way to go. It's one of those canonical programming homework assignments... Fast and easy to program. Use a "while" loop. That's my hint.
posted by telegraph at 12:40 PM on November 23, 2011


java.util.formatter would make for an interesting approach if you're comfortable with programmatically generating the format string and want to impress your teacher. Boring old string concatenation is probably the better way to go, though.
posted by marakesh at 12:44 PM on November 23, 2011


I recently did this in clojure. Best I can remember, I first spit out (number of rows - current row) spaces. That worked because all my numbers were single-digit. You'd have to multiply by the maximum number of digits per number, and pad out each number with its own spaces.
posted by mad bomber what bombs at midnight at 2:47 PM on November 23, 2011


Not to drive traffic away from Meta, but you might try asking the question on Stackoverflow.com. Tag the question as homework, and you'll likely get lots of people jumping in and nudging you in the right direction.
posted by Ickster at 6:13 PM on November 23, 2011


« Older Any Advice on Buying a Cool Mom-Mobile?   |   how much to charge? Newer »
This thread is closed to new comments.