• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pascal's Triangle, Binomial Coefficients and Factorial process

 
Ranch Hand
Posts: 91
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have written a Java Program where it will ask for an input number of lines and it will print the coefficients using Binomial theorem and Factorial and Pascal's Triangle Method.

I know any Java student can write code. But I believe many things influence a code. So I want to know if it can be improved or not.

Here's the code:

 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest you start by getting rid of _ in method and variable names. You might use _ in C, but not in Java®.
Since your methods behave as pure functions, it is correct to make them static. In which case, why aren't the methods in lines 24 and 34 marked static, too.
Don't double‑space code.
Why are you using a long rather than an int? Is there any chance of your getting enough lines to reequire a long before you run out of memory or stack space.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Mark,

a good factorial is always handy, and what you do seems fine to me.

But if it is only about Pascals Triangle, you don't need factorials. Every coefficient (apart from the first and last one of each row) is the sum of the two above coefficients. Since the first row is {1, 1}, filling the triangle is then easy.

A minor detail: in your output I would have row 1 as 1 1, since row one has the coefficients of (a + b) ^ 1. The 2nd term of row N should be N.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:. . . a good factorial is always handy, and what you do seems fine to me. . . .

Yes, it is
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic