473,320 Members | 1,841 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,320 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 1017

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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.