473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Is there any problem with this snippet?

1
Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * 
  3.  * @param row The ith row of the Pascal Triangle
  4.  * @return ArrayList<Integer> A List containing all the binomial coefficients
  5.  *         for the given row
  6.  */   
  7. public ArrayList<Integer> pascalRow(int row){
  8.     ArrayList<Integer> values = new ArrayList<Integer>();
  9.  
  10.     for(int i = 0; i <= row; i++){
  11.         values.add(choose(row, i));
  12.     }
  13.  
  14.     return values;
  15.  
  16. }
  17.  
  18. /**
  19.  * 
  20.  * @param n The number of total elements
  21.  * @param r The number to choose from n
  22.  * @return The binomial coefficient n!/(r! * (n-r)!)
  23.  */
  24. public int choose(int n, int r){
  25.     return factorial(n)/(factorial(r) * factorial(n-r));
  26. }
  27.  
  28. /**
  29.  * 
  30.  * @param n The number of which to take the factorial
  31.  * @return int n!
  32.  */
  33. public int factorial(int n){
  34.     int x = 1;
  35.  
  36.     for(int i = 2; i <= n; i++){
  37.         x *= i;
  38.     }
  39.  
  40.     return x;
  41. }
  42.  
Source https://txeditor.com/jhn9g440ob5
Feb 22 '18 #1
0 1021

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Brian Blazer | last post by:
Hello, I am playing around with pyGoogle and encountered an error that I have never seen, and I am unsure how to correct for it. Here is a code snippet: for r in data.results: print 'Title:...
1
by: wtnt | last post by:
Hello. I've searched all over and haven't seen another thread with this problem. Please bear with me as I try to explain. thanks. :) I have some programs that need to be cross-platform...
9
by: Eric Lilja | last post by:
Hello, I have two code snippets I want you to look at. My program compiles without warnings (warning level set to max, gcc 3.4.3) with either snippet but the latter one causes a segfault at...
17
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions...
15
by: Gan Quan | last post by:
I'm writing a c++ program that has many (100+) threads read/write files simultaneously. It works well if not considering the efficiency. The file i/o seems to be the bottleneck. This is my code...
2
by: daokfella | last post by:
Hi all, I've created a code snippet in VS 2005 and saved it in the proper snippet folder. When I right-click and select Insert Snippet, I navigation to My Code Snippets and it shows my snippet....
0
by: frankatle | last post by:
Hi. I am writing a opensource code snippet manager in python (pyqt4). The idea is that users can upload their snippets to remote subversion server, (if they choose) and add comments to existing...
13
by: Kamui Shirow | last post by:
Hello! I will write a snippet to illustrate my doubt: snippet 1: void foo( int l ) { char* tmp; tmp = (char*) malloc( l * sizeof( char ) );
2
by: steve.falzon | last post by:
Hi I've been searching high and low for this info but have been unsuccessful so far. I wonder if anybody knows where I can get a specification for the directives contained within the code...
4
by: tshad | last post by:
I was watching a video that used a code snippet to create a property and when you type "prop" tab tab, it would create the private variable as well as the property definitions with the private...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.