Stan
Stan

Reputation: 1

the log in math library does not work for me

i have been trying to get log to work but it just doesnt get the same value as you would get with a calculator.

i tried these but none work; i want to calculate for example 600 log 600 but never has that actual value.

the only difference between the below codes is they calulate worst_mergesort defferently:

rows * math.log(rows)

math.log(rows)

math.log(rows, rows)

# import the math module for calulations involving logarithm
import math

# ask the user how many rows the table has
rows: int = int(input('how many rows does the table have?'))

def which_sort(rows: int) -> str:
    """ 
    This function first calculates the worst times for both quicksort and merge sort. 
    if mergesort is at least 100 times faster then quick sort it uses mergesort, otherwise quicksort.
    :param: rows - an integer that counts the amount of rows in a table.
    """
    # if rows is <= 1 worst_mergesort would be 0 resulting in a zerodevision error
    if rows <= 1:
        return 'use quicksort'

    # calculate the worst possible outcomes
    worst_quicksort: int = rows**2
    worst_mergesort: int = math.log(rows)
    print(worst_mergesort)
    print(rows)
    # checks wether the worst of merge sort is 100 faster then the worst of quick sort
    if worst_mergesort <= (worst_quicksort / 100):
        return 'use merge sort'

    else:
        return 'use quick sort'

    
    
which_sort(rows)
# import the math module for calulations involving logarithm
import math

# ask the user how many rows the table has
rows: int = int(input('how many rows does the table have?'))

def which_sort(rows: int) -> str:
    """ 
    This function first calculates the worst times for both quicksort and merge sort. 
    if mergesort is at least 100 times faster then quick sort it uses mergesort, otherwise quicksort.
    :param: rows - an integer that counts the amount of rows in a table.
    """
    # if rows is <= 1 worst_mergesort would be 0 resulting in a zerodevision error
    if rows <= 1:
        return 'use quicksort'

    # calculate the worst possible outcomes
    worst_quicksort: int = rows**2
    worst_mergesort: int = math.log(rows, rows)
    print(worst_mergesort)
    print(rows)
    # checks wether the worst of merge sort is 100 faster then the worst of quick sort
    if worst_mergesort <= (worst_quicksort / 100):
        return 'use merge sort'

    else:
        return 'use quick sort'

    
    
which_sort(rows)
# import the math module for calulations involving logarithm
import math

# ask the user how many rows the table has
rows: int = int(input('how many rows does the table have?'))

def which_sort(rows: int) -> str:
    """ 
    This function first calculates the worst times for both quicksort and merge sort. 
    if mergesort is at least 100 times faster then quick sort it uses mergesort, otherwise quicksort.
    :param: rows - an integer that counts the amount of rows in a table.
    """
    # if rows is <= 1 worst_mergesort would be 0 resulting in a zerodevision error
    if rows <= 1:
        return 'use quicksort'

    # calculate the worst possible outcomes
    worst_quicksort: int = rows**2
    worst_mergesort: int = rows * math.log(rows)
    print(worst_mergesort)
    print(rows)
    # checks wether the worst of merge sort is 100 faster then the worst of quick sort
    if worst_mergesort <= (worst_quicksort / 100):
        return 'use merge sort'

    else:
        return 'use quick sort'

    
    
which_sort(rows)

Upvotes: 0

Views: 263

Answers (1)

L Lewis
L Lewis

Reputation: 96

I think the problem is simply that the base of the logarithm is different in math.log than in a calculator.

math.log computes the natural logarithm, so the base is e and the calculators usually use base 10 by default.

In math.log, you can specify the base as a second argument e.g. 600*math.log(600, 10) should give you the expected answer.

Upvotes: 1

Related Questions