Updated 11052024-140437
https://davidblue.wtf/squares21CBB034-D929-4748-A18B-FA1A795386AEhttps://dub.sh/squareshttps://itty.bitty.site/#/pythonista3://?exec=%23!/usr/bin/env%20python3%0Aimport%20sys%0Aimport%20argparse%0Aimport%20math%0A%0A%0Adef%20closest_squares%28n%29:%0A%09sqrt_n%20%3D%20int%28math.sqrt%28n%29%29%0A%0A%09%23%20Check%20if%20n%20itself%20is%20a%20perfect%20square%0A%09if%20sqrt_n%20*%20sqrt_n%20%3D%3D%20n:%0A%09%09lower_square%20%3D%20%28sqrt_n%20-%201%29**2%0A%09%09upper_square%20%3D%20%28sqrt_n%20%2B%201%29**2%0A%09else:%0A%09%09lower_square%20%3D%20sqrt_n**2%0A%09%09upper_square%20%3D%20%28sqrt_n%20%2B%201%29**2%0A%0A%09return%20lower_square,%20upper_square%0A%0A%0Adef%20main%28%29:%0A%09parser%20%3D%20argparse.ArgumentParser%28%0A%09%20description%3D%22Find%20the%20closest%20square%20numbers%20to%20a%20given%20input%20number.%22%29%0A%09parser.add_argument%28%27number%27,%0A%09%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20type%3Dint,%0A%09%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nargs%3D%27%3F%27,%0A%09%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20help%3D%22The%20input%20number%20to%20find%20closest%20squares%20for.%22%29%0A%09parser.add_argument%28%0A%09%20%27--below%27,%0A%09%20action%3D%27store_true%27,%0A%09%20help%3D%22Output%20only%20the%20closest%20square%20below%20the%20input%20number.%22%29%0A%09parser.add_argument%28%0A%09%20%27--above%27,%0A%09%20action%3D%27store_true%27,%0A%09%20help%3D%22Output%20only%20the%20closest%20square%20above%20the%20input%20number.%22%29%0A%09parser.add_argument%28%27--both%27,%0A%09%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20action%3D%27store_true%27,%0A%09%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20help%3D%22Output%20both%20closest%20squares%20%28default%29.%22%29%0A%0A%09args%20%3D%20parser.parse_args%28%29%0A%0A%09%23%20Check%20if%20a%20number%20was%20provided%20either%20as%20an%20argument%20or%20via%20stdin%0A%09if%20args.number%20is%20not%20None:%0A%09%09n%20%3D%20args.number%0A%09else:%0A%09%09try:%0A%09%09%09n%20%3D%20int%28input%28%22Enter%20a%20number:%20%22%29%29%0A%09%09except%20ValueError:%0A%09%09%09print%28%22Please%20provide%20a%20valid%20integer.%22%29%0A%09%09%09sys.exit%281%29%0A%0A%09%23%20Get%20the%20closest%20squares%0A%09lower_square,%20upper_square%20%3D%20closest_squares%28n%29%0A%0A%09%23%20Determine%20output%20based%20on%20flags%0A%09if%20args.below:%0A%09%09print%28lower_square%29%0A%09elif%20args.above:%0A%09%09print%28upper_square%29%0A%09else:%20%20%23%20Default%20or%20--both%0A%09%09print%28f%22%7Blower_square%7D,%20%7Bupper_square%7D%22%29%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22:%0A%09main%28%29%0A%0A
squaressquares is a Python-based command-line application that finds the two closest square numbers to a given input number.
To install squares, use pip:
pip install squares-cli
This will install the squares command, allowing you to find the closest square numbers directly from the command line.
The squares command can find the two closest square numbers (one above and one below) to a given input number. You can use it in various ways, specifying options to control the output.
squares [OPTIONS] <number>
If no options are specified, squares will output both the closest square number below and the closest square number above the given input, separated by a comma.
--below - Outputs only the closest square below the input number.--above - Outputs only the closest square above the input number.--both - Outputs both the closest squares (this is the default behavior if no option is specified).-h or --help - Displays the help message.Here are some example commands and their expected outputs:
squares 12
Output:
9, 16
In this example, squares 12 finds the closest squares around 12, returning both the closest square below (9) and the closest square above (16).
squares --below 12
Output:
9
This example uses --below to return only the closest square below 12, which is 9.
squares --above 12
Output:
16
Here, --above is used to return only the closest square above 12, which is 16.
squares --both 12
Output:
9, 16
Using --both explicitly returns both closest squares around 12. This produces the same result as the default behavior.
A man page is also included in the installation. You can access it by typing:
man squares
This will display comprehensive information about the command, options, and examples, similar to what is provided in this README.
squares is compatible with UNIX-like systems (Linux and macOS) and Windows, though the man page installation may vary depending on the OS.This project is licensed under the MIT License.