Python Break Factor Function

BustACode 1 Tallied Votes 327 Views Share

As a result of my research in number theory I find that I often need to break a number down into its numerical parts while stepping through one divisor to another.

I call this process "break factoring," as I am just breaking a number down into parts, and not necessarily factoring it.
A "break factor" result has three columns of numbers: A multiplier (M), the quotient (Q), and the remainder (R). For each level of the results: (M * Q) + R = Number

This function returns the printed table.

Also not earth shattering, but hey, someone might want to use it.

def f_BreakFac(v_Num, v_Divider):
    """Syntax: (INT, INT); Returns: PRINT & NONE;
    Desc.: Break-factors a number using a divisor, usually 2, into a Multiplier, Quotient and Remainder
    Test: print("Break factors of: {}, using: {}".format(91, 2))
    f_BreakFac(91, 2)"""

    v_Multiplier = v_Divider ## Init Multiplier to Divider
    v_IntPart, v_DecPart = divmod(v_Num, v_Divider) ## Use "divmod" to execute first break into Int-Part and Dec-Part

    print("Num: {0:<0n}".format(v_Num)) ## Print the num being broken
    print("{0:<0s}".format("-----"))
    print("Multi: {0:<10n}Int: {1:<10n}Rem: {2:<10n}".format(v_Multiplier, v_IntPart, v_DecPart)) ## Print the results of first break

    while v_Multiplier < v_IntPart: ## While the multiplier is less than Int-Part
        v_PriorMulitplier = v_Multiplier ## Creates a Prior multiplier for later computation
        v_Multiplier = v_Multiplier * 2 ## Increments current multiplier by (* 2)
        v_IntPart, v_DecHold = divmod(v_IntPart, v_Divider) ## Next break run
        v_DecPart = v_DecPart + (v_DecHold * v_PriorMulitplier) ## Computes new dec-part from new dec-part and priormultiplier (I.e. a remainder's valuei is the prior multiplier times the current remainder
        print("Multi: {0:<10n}Int: {1:<10n}Rem: {2:<10n}".format(v_Multiplier, v_IntPart, v_DecPart)) ## Print the new set of nums
    v_PriorMulitplier = v_Multiplier ## One last break after the "while loop" completes for completeness
    v_Multiplier = v_Multiplier * 2
    v_IntPart, v_DecHold = divmod(v_IntPart, v_Divider)
    v_DecPart = v_DecPart + (v_DecHold * v_PriorMulitplier)
    print("Multi: {0:<10n}Int: {1:<10n}Dec: {2:<10n}".format(v_Multiplier, v_IntPart, v_DecPart))
    return
Gribouillis 1,391 Programming Explorer Team Colleague

If you are working in number theory, you might be interested in the python interface to the PARI GP calculator system: pari-python. It contains many functions for number theory (it probably does not work with python 3 however)

commented: Thanks. +0
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

A note to coding style:
In just about all computer languages programmers try not to exceed about 80 characters per line of code. You could add your comments above the code line to help out, otherwise word wrap makes code pretty hard to read and understand.

Again, there are a couple of IDE programs that will help. Amongst them the Ninja IDE, free at:
http://www.ninja-ide.org/

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.