Pascal砖三角形

huangapple go评论138阅读模式
英文:

Pascal brick triangle

问题

以下是您要翻译的内容:

there is a pyramid, each brick weighs 1, the function takes the position of the brick, gives out how much pressure is on it from above

it's something like Pascal's triangle but I couldn't solve it

i tried to print the pyramid at least but failed

n_blocks = 0

for i in range (1, 11):
print(n_blocks, f"{n_blocks/i} "*i)

n_blocks +=i

英文:

there is a pyramid, each brick weighs 1, the function takes the position of the brick, gives out how much pressure is on it from above

it's something like Pascal's triangle but I couldn't solve it

i tried to print the pyramid at least but failed

n_blocks = 0

for i in range (1, 11):
  print(n_blocks, f" _{n_blocks/i}_ "*i)
  
  n_blocks +=i

Pascal砖三角形

答案1

得分: 1

这类似于帕斯卡三角形,但不完全相同。砖块上的重量等于(它上面两块砖的重量加上这些砖自身的重量)的一半。

换句话说,W(n, k) = (W(n-1, k-1) + W(n-1, k) + 2) / 2

除了k == 0的情况,我们有W(n, 0) = (W(n-1, 0) + 1) / 2
以及对于k == n,我们有W(n, n) = (W(n-1, n-1) + 1) / 2

所以让我们实现这个:

def brick_triangle(n_layers):
    layers = [[0]]
    if n_layers == 1: return layers

    for n in range(1, n_layers):
        # 初始化这一层的第一块砖的重量
        this_layer = [(layers[-1][0] + 1) / 2]
        # 添加层中其他砖块的重量,除了最后一块
        for k in range(1, n):
            w = (layers[-1][k-1] + layers[-1][k] + 2) / 2
            this_layer.append(w)

        # 添加最后一块砖的重量
        this_layer.append((layers[-1][-1] + 1) / 2)
        
        # 将这一层添加到所有层中
        layers.append(this_layer)

    return layers

打印结果如下:

for layer in brick_triangle(5):
    print(*layer, sep="\t")
0
0.5     0.5
0.75    1.5     0.75
0.875   2.125   2.125   0.875
0.9375  2.5     3.125   2.5     0.9375

如果您有任何进一步的问题,欢迎提出。

英文:

This is similar to Pascal's triangle, but not quite.
The weight on a brick is equal to half of (the weight on the two bricks above it plus the weight of those bricks themselves).

In other words, W(n, k) = (W(n-1, k-1) + W(n-1, k) + 2) / 2,

except for k == 0, we have W(n, 0) = (W(n-1, 0) + 1) / 2
and for k == n, we have W(n, n) = (W(n-1, n-1) + 1) / 2

So let's implement this:

def brick_triangle(n_layers):
    layers = [[0]]
    if n_layers == 1: return layers

    for n in range(1, n_layers):
        # Initialize this_layer with weight on first brick
        this_layer = [(layers[-1][0] + 1) / 2]
        # Append weight on other bricks in layer except the last one
        for k in range(1, n):
            w = (layers[-1][k-1] + layers[-1][k] + 2) / 2
            this_layer.append(w)

        # Append the weight on the last brick
        this_layer.append((layers[-1][-1] + 1) / 2)
        
        # Add this layer to all layers
        layers.append(this_layer)

    return layers

Printing this gives:

for layer in brick_triangle(5):
    print(*layer, sep="\t")
0
0.5     0.5
0.75    1.5     0.75
0.875   2.125   2.125   0.875
0.9375  2.5     3.125   2.5     0.9375

<sup>Note: I'm leaving the previous incorrect version of my answer here so you can compare the differences in the output.</sup>

You've correctly recognized that this is related to Pascal's triangle. The numbers in each layer of Pascal's triangle give you the proportion of weight that is carried by the bricks in that layer. The total weight carried by a layer is equal to the number of bricks above it, which for the nth layer is simply sum(range(1, n)) (where n is one-based)

Given this, and understanding that the element in the nth row and kth column of Pascal's triangle is easily calculated as nCk = n!/(k! * (n - k)!), we have:

import math

def brick_triangle(n_layers):
    layers = []
    for n in range(n_layers):
        weight_above = n * (n + 1) / 2 # sum(range(1, n))
        brick_proportions = [math.comb(n, k) for k in range(n+1)]
        s = sum(brick_proportions)
        brick_weights = [weight_above * b / s for b in brick_proportions]
        layers.append(brick_weights)

    return layers

Note: I used math.comb to calculate nCk, but that's only available in Python 3.8+. If you have Python <3.8, see https://stackoverflow.com/questions/2096573/counting-combinations-and-permutations-efficiently

Now, we can call this function for n layers and print the results:

for layer in brick_triangle(5):
    print(*layer, sep=&quot;\t&quot;)

which gives the result:

0.0
0.5     0.5
0.75    1.5     0.75
0.75    2.25    2.25    0.75
0.625   2.5     3.75    2.5     0.625

huangapple
  • 本文由 发表于 2023年3月10日 01:07:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定