Pascal's triangle, calculating number at given row and column Java -


i have small assignment have produce pascal triangle , find number given user inputted row , column. have come with, not know how proceed calculating number @ given row , column.

import java.util.scanner;  public class pascaltriangle {     public static void print(int n) {        (int = 0; < n; i++) {            (int j = 0; j <= i; j++) {                system.out.print(pascal(i, j) + " ");            }            system.out.println();        }    }     public static int pascal(int i, int j) {        if (j == 0) {            return 1;        } else if (j == i) {            return 1;        } else {            return pascal(i - 1, j - 1) + pascal(i - 1, j);        }     }     public static void main(string[] args) {        scanner scanner = new scanner(system.in);        system.out.print("enter row number");        int row = scanner.nextint();        print(row);    } } 

first, code slow. i'm not going assignment, can compute next line of pascal's triangle knowing previous one, in constant time each cell, while using huge time re-computing same values.

second, actual question, can check out formula of cell in pascal triangle. not right way go, can still starting point documenting on pascal triangle.

using formula, can compute number @ given position computing only row.

edit

your recent comment seems point not compute value @ position, compute position of value. beforehand, there absolutely no unicity of such position: values appear @ least twice in triangle, symmetrical.

but if want return e.g. first in lexicographical order of positions, compute rows 1 one (using same scheme display triangle) , stop wanted value.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -