Reputation: 77
basically if there are 3 blocks the program should say 2 layers - 2 blocks on the bottom and then 1 on the top (total 3 blocks used)
if there are 6 bricks available then the program should say 3 layers - 3 blocks on the bottom, 2 above it, and then 1 on the top
if there are 7 bricks available then the program should also be 3 layers - 3 blocks on the bottom, 2 above it, and then 1 on the top, 1 left over which is not enough for a new layer
i've got it working however for some reason it acts strange when I put in 20 bricks, where it gives me a negative number and increases the layer,
I fixed this with an if statement, but I do not understand why this is happening, could use some advice. Thanks
blocks = int(input("Enter the number of blocks: "))
layer = 0
layer_count = 0
while blocks >= layer:
layer = layer + 1
blocks = blocks - layer
layer_count = layer_count + 1
print('blocks needed for layer is', layer)
print('blocks remaining are', blocks)
print('number of layers completed is', layer_count)
print()
if blocks < 0:
layer_count = layer_count - 1
print('Final count is', layer_count)
Upvotes: 1
Views: 1521
Reputation: 135
Without using Math trick
To understand this problem, we should first understand how we get the total number of blocks from the number of layers. If we have 3 blocks, then Total = 1+2+3 = 6. Now, let's check the other way round.
Let say we now know the total number of bricks is 6, and we want to know the number of layers. We will keep decreasing the total until it is not negative.
CASE I.: Full layers
Total = 6
counter = 1
Total-1 = 6-1 = 5; counter = 2
Total-2 = 5-2 = 3; counter = 3
Total-3 = 3-3 = 0; counter = 4
STOP INCREMENTING counter when Total-counter < 0
Number_Layers = counter - 1 = 4-1 = 3
Case II. : Not every layers full
Total = 7
counter = 1
Total-1 = 7-1 = 6; counter = 2
Total-2 = 6-2 = 4; counter = 3
Total-3 = 4-3 = 1; counter = 4
Total-4 = 1-4 = -3< 0; counter = 5 STOP
STOP INCREMENTING counter when Total-counter < 0
Number_Layers = counter - 1 = 5-1 = 4
total = 12
c = 1
while total>0:
if (total-c) < 0:
break
# print(total)
total = total - c
c +=1
print(c-1)
Using Math trick
given a function f(n), if f(n+1)-f(n) is a constant value r, then, the sum of
f(i) + f(i+1) + ... + f(k) = (k-i+1)*(f(k)+f(i))/2.
Particular Case: f(n) = n
==> f(n+1) = n+1
==> f(n+1) - f(n) = n+1-n = 1
==> f(1) + f(2) + ... + f(n) = 1 + 2 + ...+ n = n(n+1)/2
The sum S = n(n+1)/2 ==> n(n+1) = 2S
==> n^2 + n -2S = 0. This is a quadratic equation that can be easily solved in a close form
n1 = (-1-sqrt(1+8s))/2 n2 = (-1+sqrt(1+8s))/2
n1 <0 Not possible, hence, the solution is n2
def height(S):
Delta = 1+8*S
n1 = (-1-Delta**0.5)/2
n2 = (-1+Delta**0.5)/2
return(int(n2))
height(1000)
Upvotes: 0
Reputation: 42139
The number of blocks in a pyramid with N layers is N*(N+1)/2. So if you know the number of blocks B you simply have to resolve B = N*(N+1)/2 for N.
This will give you N = ( √(8B+1) - 1 ) /2
You will want the floor of this value because you don't want fractions of a layer.
so you can write the following function:
def pyramidLayers(B): return int((8*B+1)**0.5-1)//2
and use it in your program.
example outputs:
for blocks in range(1,10):
layers = pyramidLayers(blocks)
blocksUsed = layers*(layers+1)//2
print(blocks,"blocks -->", layers,"layers using",blocksUsed,"blocks")
1 blocks --> 1 layers using 1 blocks
2 blocks --> 1 layers using 1 blocks
3 blocks --> 2 layers using 3 blocks
4 blocks --> 2 layers using 3 blocks
5 blocks --> 2 layers using 3 blocks
6 blocks --> 3 layers using 6 blocks
7 blocks --> 3 layers using 6 blocks
8 blocks --> 3 layers using 6 blocks
9 blocks --> 3 layers using 6 blocks
Upvotes: 1