Outputting height of a pyramid

2024/10/7 10:21:45

So for this coding exercise I have to input a number of imaginary blocks and it will tell me how many complete rows high the pyramid is.

So for example if I input 6 blocks...I want it to tell me that the height of the pyramid is 3. (3 blocks on the bottom, 2 above that, and 1 above that).

In my head I feel this would work similar to a Fibonacci pyramid so I based my code off of that.

blocks = int(input("Enter number of blocks: "))for i in range(blocks + 1):for j in range(blocks + 1):height = j / 2
if height % 2 == 0:height = height / 2print(f"The height of the pyramid: {height}")

This is what I have so far... If I do the number 6 or like 20 it works, but obviously if I do something like 1000 it isn't going to give me the result I want. I feel I'm pretty far off with my code.

Answer

A pyramid of height N has 1 + 2 + … + N blocks in it. This reduces to N * (N + 1) / 2. So you need to find the largest integer of the form (N^2 + N) / 2 that is less than or equal to your chosen number blocks. The quadratic is fairly simple: N^2 + N - 2 * blocks = 0, with roots at N = floor((-1 +/- sqrt(1 + 8 * blocks)) / 2). Since blocks is a positive integer, the negative root will never apply in your case. You can use int for floor and **0.5 for sqrt to get:

blocks = int(input("Enter number of blocks: "))
print(f'You can build a pyramid {int(0.5 * ((8 * blocks + 1)**0.5 - 1))} blocks high')
https://en.xdnf.cn/q/70259.html

Related Q&A

PySide SVG image formats not found?

I am using PyDev plugin for Eclipse with Qt integration. I have PySide installed and I am having trouble with SVG image formats. I know when I run my application the formats located in C:\Python27\Lib\…

convert ascii character to signed 8-bit integer python

This feels like it should be very simple, but I havent been able to find an answer..In a python script I am reading in data from a USB device (x and y movements of a USB mouse). it arrives in single AS…

What is the equivalent way of doing this type of pythonic vectorized assignment in MATLAB?

Im trying to translate this line of code from Python to MATLAB:new_img[M[0, :] - corners[0][0], M[1, :] - corners[1][0], :] = img[T[0, :], T[1, :], :]So, naturally, I wrote something like this:new_img(…

How do I connect mitmproxy to another proxy outside of my control?

The process would be that the browser send a request to MITMproxy and then generate a request that gets sent to target proxy server which isnt controlled by us. The proxy server would send a response t…

How does conda-env list / conda info --envs find environments?

Ive been experimenting with anaconda/miniconda because my users use structural biology programs installed with miniconda and none of the authors A) take into account that there might be other miniconda…

Updating a large number of entities in a datastore on Google App Engine

I would like to perform a small operation on all entities of a specific kind and rewrite them to the datastore. I currently have 20,000 entities of this kind but would like a solution that would scale …

Is there a neater alternative to `except: pass`?

I had a function that returned a random member of several groups in order of preference. It went something like this:def get_random_foo_or_bar():"Id rather have a foo than a bar."if there_are…

Get a permutation as a function of a unique given index in O(n)

I would like to have a function get_permutation that, given a list l and an index i, returns a permutation of l such that the permutations are unique for all i bigger than 0 and lower than n! (where n …

How to generate Bitcoin keys/addresses from a seed in Python?

I am trying to create a set of public/private keys from a mnemonic based on BIP0039. I am working in Python.Here is the code I have so far:from mnemonic import Mnemonic mnemon = Mnemonic(english) words…

NumPy - Set values in structured array based on other values in structured array

I have a structured NumPy array:a = numpy.zeros((10, 10), dtype=[("x", int),("y", str)])I want to set values in a["y"] to either "hello" if the corresponding val…