Beispiel #1
0
 def test_edge(self):
     #negative should find it based length of b
     self.assertEqual(subtract('123', '1234'), '-')
     #negative should find it based on the first digit
     self.assertEqual(subtract('123', '456'), '-')
     #negative, but not discovered until part way through
     self.assertEqual(subtract('555535555', '555545555'), '-')
     #large subtraction
     self.assertEqual(
         subtract('987654321987654321987654321',
                  '123456789123456789123456789'),
         '864197532864197532864197532')
def check_eleven_(c):
    ##    #input a number stored as a string e.g. '1234'
    ##    #it will return if it is divisible by ____ or not

    #make sure the number is long enough to not just do it with the modulus operator
    if len(c) > 4:
        alternate = True
        a = '0'
        b = '0'

        for each_char in c:
            if alternate:
                a = add(a, str(each_char))
                alternate = False
            else:
                b = add(b, str(each_char))
                alternate = True
        first = subtract(a, b)
        second = subtract(b, a)

        #if the first difference is negative
        if first == '-':
            #use the second result
            result_second = divide(second, '11')
            #if there was a remainder these will not be equal
            if multiply(result_second, '11') == second:
                #it divided evenly and should be removed
                return True
            else:
                #otherwise it did not divide evenly
                return False
        #otherwise the first way of subtracting is positive
        else:
            #use the first result
            result_first = divide(first, '11')
            #if there was a remainder these will not be equal
            if multiply(result_first, '11') == first:
                #it divided evenly and should be removed
                return True
            else:
                #otherwise it did not divide evenly
                return False
    #otherwise just do it with the mod operator
    else:
        if int(c) % 11 == 0:
            return True
        else:
            return False
Beispiel #3
0
 def test_obvious_small(self):
     self.assertEqual(subtract('100', '50'), '50')
     self.assertEqual(subtract('9999', '1234'), '8765')
     self.assertEqual(subtract('7', '0'), '7')
     self.assertEqual(subtract('0', '0'), '0')
     self.assertEqual(subtract('1234', '0'), '1234')
     self.assertEqual(subtract('1002', '3'), '999')
     self.assertEqual(subtract('5002', '1000'), '4002')
     self.assertEqual(subtract('999', '999'), '0')
def check_seven_(c):
    #input a number stored as a string e.g. '1234'
    #it will return if it is divisible by ____ or not
    running_tally = c
    l = len(running_tally)
    digit = ''
    while l > 4:
        l = len(running_tally)
        digit = running_tally[l - 1]
        running_tally = subtract(running_tally[:-1:], str(int(digit) * 2))

    if int(running_tally) % 7 == 0:
        return True
    else:
        return False
def divide(a, b):

    ##trivial cases
    #if either a or b are 0 then return 0
    #even though mathematically if b is 0, a black hole opens
    if a == '0' or b == '0':
        ##        print('one of the parameters is 0')
        return '0'
    #if either a or b are negative return negative
    if a[0] == '-' or b[0] == '-':
        ##        print('one of the parameters is negative')
        return '-'
    #if we are dividing by 1 then just return a
    if b == '1':
        ##        print('dividing by 1, the number remains unchanged')
        return a
    #if a is not big enough to subtract b from it then it is a fractional number
    #and should be rounded down to 0
    if subtract(a, b) == '-':
        ##        print('a was not bigger than b, so rounded down to 0')
        return '0'

    s = ''  #quotient
    top = '0'  #top number
    bottom = b  #dividend
    is_pos = ''  #flag if the number is still positive
    column_digit = 0  #keeping track of what the digit is for the column
    temp_top = ''  #
    ##    print('s: {}, top: {}, bottom: {}, is_pos: {}, column_digit: {}, temp_top: {}'.format(s,top,bottom,is_pos,column_digit,temp_top))

    ##    print('starting loops')
    for next_a in a:
        ##        print('next_a digit: {}'.format(next_a))
        ##        print('top: {}  next_a: {}'.format(top,next_a))
        top += next_a
        ##        print('top: {}'.format(top))
        ##        print('column_digit: {}'.format(column_digit))
        column_digit = 0
        ##        print('column_digit: {}'.format(column_digit))
        ##        print('is_pos: {}'.format(is_pos))
        is_pos = ''
        ##        print('is_pos: {}'.format(is_pos))

        while is_pos == '':
            top = top.lstrip('0')
            bottom = bottom.lstrip('0')
            ##            print('Subtracting: {} - {}'.format(top,bottom))
            temp_top = subtract(top, bottom)
            ##            print('temp_top: {}'.format(temp_top))
            if temp_top[0] != '-':
                ##                print('case_1')
                ##                print('column_digit before: {}'.format(column_digit))
                column_digit += 1  #to current column
                ##                print('column_digit after: {}'.format(column_digit))

                ##                print('top before: {}'.format(top))
                temp_top = temp_top.lstrip('0')
                top = temp_top
##                print('top after: {}'.format(top))

            else:
                ##                print('case_2')
                is_pos = '-'
                #break out of while loop

##        print('s (before): {}'.format(s))
        s += str(column_digit)


##        print('s (after): {}'.format(s))

##    #reverse s back to the correct order
##    s = s[::-1]
##    print('\n')

#remove leading zeros
    s = s.lstrip('0')

    ##    print('\n')
    ##    print("done w/ run \n\n")

    return s
Beispiel #6
0
def sq_root(a):
    ##    breakpoint()# s = step into, n = next line, c = continue

    # where dividend / divisor = quotient
    #establish variables
    dividend = ''
    divisor_left = ''
    quotient_digit = ''
    quotient = ''

    len_a = len(a)
    is_odd = int(str(len_a).strip()[-1]) % 2
    if is_odd:
        a = '0' + a

    dividend = (dividend + str(a[0]) + str(a[1])).lstrip('0')
    quotient_digit = floor(sqrt(int(dividend)))
    quotient += str(quotient_digit)
    dividend = subtract(dividend,
                        multiply(str(quotient_digit), str(quotient_digit)))
    divisor_left = add('0', multiply(quotient, '2'))

    for j in range(2, (len_a - 1) + is_odd, 2):
        ##        print('Starting off the loop for j = {} and {}'.format(j,j+1))
        ##        print('  dividend:       {}'.format(dividend))
        ##        print('  quotient_digit: {}'.format(quotient_digit))
        ##        print('  quotient:       {}'.format(quotient))
        ##        print('  divisor_left:   {}'.format(divisor_left))

        #add next two digits to the dividend
        dividend = (dividend + str(a[j]) + str(a[j + 1])).lstrip('0')
        ##        print('dividend becomes: {}'.format(dividend))

        #todo
        #this next part might be better formatted as a loop that iterates 1 through 9
        #stopping it at the unique cases, but combining the common code where applicable
        #the last two times i tried to combine it, i messed up it up. Also, this works fine for now
        #when this becomes a bottleneck I might need to refactor this to be more managable
        #if refactoring doesn't improve speed then I might just leave it

        #todo, if i switch to looping through the 0 through 9 digits i should keep the "best_found" flag
        #otherwise I should remove it (double check that if / elif / ... / elif /else works like i think it does
        best_found = False
        if best_found == False and subtract(
                dividend, multiply(divisor_left + '1', '1')) == '-':
            best_found == True
            ##            print('0 case chosen')
            quotient_digit = 0
            quotient += str(quotient_digit)
            #dividend remains unchanged this round
            divisor_left = divisor_left + '0'

        elif best_found == False and subtract(
                dividend, multiply(divisor_left + '2', '2')) == '-':
            best_found == True
            ##            print('1 case chosen')
            quotient_digit = 1
            quotient += str(quotient_digit)

        elif best_found == False and subtract(
                dividend, multiply(divisor_left + '3', '3')) == '-':
            best_found == True
            ##            print('2 case chosen')
            quotient_digit = 2
            quotient += str(quotient_digit)

        elif best_found == False and subtract(
                dividend, multiply(divisor_left + '4', '4')) == '-':
            best_found == True
            ##            print('3 case chosen')
            quotient_digit = 3
            quotient += str(quotient_digit)

        elif best_found == False and subtract(
                dividend, multiply(divisor_left + '5', '5')) == '-':
            best_found == True
            ##            print('4 case chosen')
            quotient_digit = 4
            quotient += str(quotient_digit)

        elif best_found == False and subtract(
                dividend, multiply(divisor_left + '6', '6')) == '-':
            best_found == True
            ##            print('5 case chosen')
            quotient_digit = 5
            quotient += str(quotient_digit)

        elif best_found == False and subtract(
                dividend, multiply(divisor_left + '7', '7')) == '-':
            best_found == True
            ##            print('6 case chosen')
            quotient_digit = 6
            quotient += str(quotient_digit)

        elif best_found == False and subtract(
                dividend, multiply(divisor_left + '8', '8')) == '-':
            best_found == True
            ##            print('7 case chosen')
            quotient_digit = 7
            quotient += str(quotient_digit)

        elif best_found == False and subtract(
                dividend, multiply(divisor_left + '9', '9')) == '-':
            best_found == True
            ##            print('8 case chosen')
            quotient_digit = 8
            quotient += str(quotient_digit)

        else:
            best_found == True
            ##            print('9 case chosen')
            quotient_digit = 9
            quotient += str(quotient_digit)

        if quotient_digit > 0:
            ##            print('since quotient_digit is > 0')
            dividend = subtract(
                dividend,
                multiply(divisor_left + str(quotient_digit),
                         str(quotient_digit)))
            ##            print('dividend becomes: {}'.format(dividend))
            divisor_left = add(multiply(divisor_left, '10'),
                               str(quotient_digit * 2))

    quotient = quotient.lstrip('0')
    return quotient
Beispiel #7
0
def is_prime_(candidate):
    #assume it is prime, until it is found to not be
    is_prime = True

    #identify the last number to check
    last_check = sq_root(candidate)

    #and store its length
    l_last = len(last_check)

    #to store the current divisor 
    current_divisor = ''


    #todo this whole block needs to be a generator
    #that just returns the next prime if it is called
    
    #The primes found directory is first
    #folders sorted by integer length
    for prime_length_folder in os.listdir(found_dir):
        plf = prime_length_folder
##        print('Dividing by primes that are: {} digits long.'.format(int(plf)))

        #then sorted by sub level
        for level_01 in os.listdir(found_dir + '\\' + plf):
##            print('Accessing the {}th sub directory.'.format(int(level_01)))
            l_01 = level_01

            #for each of the sequential files sub folder
            for each_prime in os.listdir(found_dir + '\\' + plf + '\\' + l_01):
##                print('Evaluating the file: {}'.format(each_prime))
                ep = each_prime

                #read the value from each file
                with open(found_dir + '\\' + plf + '\\' + l_01 + '\\' + ep) as temp_found:
                    current_divisor = str([a for a in temp_found][0])
##                    print('The value in this file is: {}'.format(current_divisor))

                #if the divisor is larger than the last number that needs to be checked
                if subtract(last_check,current_divisor) == '-':
                    #the number is prime
                    return is_prime

                #store the division of the candidate by the current divisor
                #since the divide function does not keep track of the remainder
                #and only keeps track of the truncated integer portion of the quotient
                a = divide(candidate,current_divisor)

                #the multiplication of that truncated integer should reveal if it was
                #truncated or divided evenly
                b = multiply(a,current_divisor)

                #by seeing if they are equal, it will reveal if the candidate is still prime
                if b == candidate:
                    #or not
                    is_prime = False


                #a better way to do this might be to capture if there was a remainder from the division
                #function and return it as a parameter e.g. "return truncated_integer, has_remainder
                #this would save on a multiplication and a compare at the end

    #return None, in case it gets here, it should throw an error
    return None