pascals-triangle

Pascal Triangle Positioning

*爱你&永不变心* 提交于 2019-12-24 14:27:51
问题 I made a Java program that prints out a pascal triangle, however I can't figure out how to correctly position it. Program 1 public class Triangle { public static void main() { System.out.println("\nTriangle: "); int row = 11; long[][] triangle = new long[row][row]; triangle[1][1] = 1; System.out.print(triangle[1][1] + "\n"); for (int i = 2; i<row; i++) { for (int n = 1; n<row; n++) { triangle[i][n] = triangle[i-1][n-1] + triangle[i-1][n]; if (triangle[i][n]>0) { System.out.print(triangle[i][n

Memoization pascals triangle

孤街醉人 提交于 2019-12-20 05:00:12
问题 I'm not interested in the actual solution or other methods solving the problem, it's the memoization i need help with :) I need help doing a pascals triangle problem with memoization. I want to get the middle number in the base of the triangle. (Project Euler 15) The first example is not memoized (although the name suggests so) "20 20" not solvable Second attempt is an attempt on doing something similar to: http://www.haskell.org/haskellwiki/Memoization Third is hlints suggestion on no2 if

Pascal's Triangle Row Sequence

好久不见. 提交于 2019-12-12 17:31:44
问题 I'm currently working on finding the row sequences of Pascal's triangle. I wanted to input the row number and output the sequence of numbers in a list up until that row. For example, (Pascal 4) would give the result (1 1 1 1 2 1 1 3 3 1) . I am trying to use an algorithm that I found. Here is the algorithm itself: V c = V c-1 * ((r - c)/c) r and c are supposed to be row and column, and V 0 =1. The algorithm can be specifically found on the wikipedia page in the section titled "Calculating and

Pascals triangle with recursion

情到浓时终转凉″ 提交于 2019-12-12 10:56:42
问题 Okay can someone tell me if my current code is even possible. I have to create pascals triangle with an input WITHOUT using any loops. I am bound to recursion. Ive spent 3 days on this, and this is the best output that I can come up with. its driving me INSANE def pascal(curlvl,newlvl,tri): if curlvl == newlvl: return "" else: tri.append(tri[curlvl]) print(tri) return pascal(curlvl+1,newlvl,tri) def triLvl(): msg = "Please enter the number of levels to generate:" triheight = int(input(msg))

Printing Pascals Triangle (recursive) (JAVA)

爱⌒轻易说出口 提交于 2019-12-11 06:24:52
问题 So far I have this, but I am not quite sure how printPTriangle can print the triangle using the code. If someone could help me out with this, I would be grateful. public static int factorial(int n) { if (n == 1) { return 1; } return n * (factorial(n - 1)); } public static int pascalsNumber(int x, int y) { return factorial(x)/(factorial(y) * factorial((x - y))); //Using combinations formula } public static void printPTriangle(int z) { } 回答1: Try this, public class PascalTriangle { public

Pascal's Triangle using mainly functions in C++

倖福魔咒の 提交于 2019-12-11 06:08:54
问题 I was trying to write a code that would display pascals triangle. Instead of displaying the result as : my result is displayed as 1 1 1 1 2 1 1 3 3 1 Please help me figure out how to modify it to be able to get the actual triangle. I cant use arrays and pointers since those aren't covered in my class yet. Here's my code: #include "stdafx.h" #include <iostream> using namespace std; void PascalsTriangle(int); int main() { int n; cout << "Enter the number of rows you would like to print for

Formatting Pascal's triangle

无人久伴 提交于 2019-12-11 02:55:14
问题 I am currently working on a homework assignment to generate what is known as Pascal's triangle in Python. So far, this is what I have: def mytri(myrange): trianglevar = [[1]] for i in range(1, myrange): tempvar = [1] for n in range(0, i-1): tempvar.append(trianglevar[i-1][n]+trianglevar[i-1][n+1]) tempvar.append(1) trianglevar.append(tempvar) return trianglevar def mymenu(): for i in mytri(int(raw_input("Please enter the height of the triangle: "))): print i print '\n' choicevar = raw_input(

Attempting to generate Pascal's Triangle as a tuple using recursion

怎甘沉沦 提交于 2019-12-08 11:17:22
问题 I need to write a function to generate Pascal's Triangle as a tuple using recursion. e.g pascal(5) ((1,), (1, 1), (1, 2, 1), (1, 3, 3, 1), (1, 4, 6, 4, 1)) I can generate it as a list with the following function: def triangle(n): if n == 0: return [] elif n == 1: return [[1]] else: new_row = [1] result = triangle(n-1) last_row = result[-1] for i in range(len(last_row)-1): new_row.append(last_row[i] + last_row[i+1]) new_row += [1] result.append(new_row) return result And I've tried changing

How do you find which row and column a number belongs to in Floyd Triangle

时光毁灭记忆、已成空白 提交于 2019-12-08 10:44:00
问题 How do you find which row and column does a number belongs to in Floyd Triangle? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 For example, 33 is in the 8th row and 5th column (input 33 → output 8th row, 5th column) 46 is in the 10th row and 1st column 27 is in the 7th row and 6th column Thank you so much in advance! 回答1: Note that n-th row ends with value n*(n+1)/2 . So you can make

How can I calculate the number at a given row and column in Pascal's Triangle?

那年仲夏 提交于 2019-12-05 08:19:11
问题 I'm trying to create a function that, given a row and column, will calculate the value at that position in Pascal's Triangle. Example: val = GetPasVal(3, 2); // returns 2 So here I'm specifying row 3, column 2, which as you can see: 1 1 1 1 2 1 ...should be a 2. 回答1: The Pascal's triangle contains the Binomial Coefficients C(n,k); There is a very convenient recursive formula C(n, k) = C(n-1, k-1) + C(n-1, k) You can use this formula to calculate the Binomial coefficients. 回答2: Using Armen's