Python Get Root Floor and Root Ceiling of a Number

BustACode 0 Tallied Votes 452 Views Share

Also because of my work in number theory, I often need to have the root floor and root ceing of a number.

This is my function for computing those two numbers from a target number.

# Imports #
import sys, os, importlib, inspect, locale, time, math
from datetime import datetime
locale.setlocale(locale.LC_ALL, '') # Set the locale for your system 'en_US.UTF-8'


    # Main #
def main():
        # Main Code
    print("Root floor: {}, Ceiling: {}".format(*f_RootSplit(91)))
    print("Root floor: {}, Ceiling: {}".format(*f_RootSplit(161)))

#-----------Basement------------

    # Functions #
        # Normal Functions
def f_RootSplit(v_Num):
    """&Syntax: INT; Returns: LIST of INT;
    Desc.: Converts v_Num into root floor and root ceiling.
    Test: print("Root floor: {}, Ceiling: {}".format(*f_RootSplit(91)))"""
    v_RootFloor = math.sqrt(v_Num) // 1 ## Compute sqrt floor
    v_RootCeiling = v_RootFloor + 1 ## Create sqrt ceiling
    return v_RootFloor, v_RootCeiling

    # Main Loop #
if __name__ == '__main__':
    main()
Gribouillis 1,391 Programming Explorer Team Colleague

Why not use math.floor() and math.ceil() ?

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Try ...

sf = "For {} Root floor: {}, Ceiling: {}"
n = 91
print(sf.format(n, math.floor(n**0.5), math.ceil(n**0.5)))
n = 161
print(sf.format(n, math.floor(n**0.5), math.ceil(n**0.5)))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.