Exemplo n.º 1
0
def show_ducci_sequence(L):
    w = max([len(str(i)) for i in L]) + 2
    for i in ducci_sequence(L):
        equal_spacing(i, w)


#show_ducci_sequence([52,999,78,967,25,7])
def long_multiplication_steps(A,B):
    # Get the digits
    dA = [int(i) for i in str(A)]
    dB = [int(i) for i in str(B)]
    
    # Go through the digits of each number backward doing single digit
    # multiplication
    rows = []
    for sh,b in enumerate(reversed(dB)):
        R = [0]*(len(dA)+len(dB))
        for pos,a in enumerate(reversed(dA)):
            # Store the results of the multiplication in a row
            R[-(sh+pos)-1] = a*b
        rows.append(R)
    
    # Deep copy of rows
    R = [rows[i].copy() for i in range(len(rows))]

    # Calculate the addition
    out = ""
    carry = 0
    C = []
    while len(rows[0]) > 0:
        C.append(carry)
        s = carry
        for row in rows:
            s += row.pop()
        carry = s // 10
        digit = s%10
        out = str(digit) + out

    equal_spacing(reversed(["  Carries"]+C),3)
    equal_spacing_grid(R,3)
    print()
    equal_spacing(out,3)
    
    return int(out)
Exemplo n.º 3
0
from Combinatorics import stirling_numbers_1, stirling_numbers_2
from GeneralUtils import equal_spacing

print("Stirling Numbers of the First Kind")
for i in range(8):
    L = []
    for j in range(i + 1):
        L.append(stirling_numbers_1(i, j))
    equal_spacing(L, 5, 'left')

print("\nStirling Numbers of the Second Kind")
for i in range(8):
    L = []
    for j in range(i + 1):
        L.append(stirling_numbers_2(i, j))
    equal_spacing(L, 4, 'left')
from Computation import addition_chain
from GeneralUtils import equal_spacing

for i in range(1,50):
    equal_spacing(addition_chain(i),3,'center')
Exemplo n.º 5
0
a, b = 620, 380
print(
    "The greatest common denominator of two natural numbers is the largest number that is a factor of both."
)
print(f"The GCD of the set {a} and {b} is {gcd(a,b)}")

print(
    "\nCalculating the GCD can be done reasonably quickly using the Euclidean Algorithm."
)

a, b = 629, 777
print(
    f"\nThis algorithm procedes by subtracting the smaller number from the larger until the numbers are equal. Here we find the GCD of {a} and {b}.\n"
)
equal_spacing([a, b], 5, 'left')
while a != b:
    if a > b:
        a = a - b
    else:
        b = b - a
    equal_spacing([a, b], 5, 'left')

print(f"\nSo the their greatest common denominator is {gcd(a,b)}")

print(
    "\nThere are slightly more efficient methods of calculation available today."
)

S = [5610, 24871, 1683]
print("\nWe can also find the GCD of a set of numbers")
def show_pascals_triangle(n,w=5):
    triangle = pascals_triangle(n)
    
    for ctr,row in enumerate(triangle):
        equal_spacing(row,w,'left')