manyspikes

Operations on numbers

Initialising environment...

You can easily perform common arithmetic operations on numeric values using the following operators:

  • + for addition
  • - for subtraction
  • * for multiplication
  • / for division

Here is a snippet that demonstrates these operators in action:

annual_price = 10000 discount_percent = 5 monthly_price = annual_price * (1 - discount / 100) / 12

In the snippet above we are calculating the monthly price of a service. We apply a discount of 5% to its annual price and divide the result by 12 (months) to get the monthly price.

There are two other common operators for numeric types:

  • // for integer division
  • % for remainder (modulus)
  • ** for exponentiation

In addition to operators, there are also a number of useful functions you can use with numeric values, such as:

  • pow(a, b) to calculate a to the power of b
  • sqrt(a) to calculate the square root of a
  • round(a) to round the number a to the nearest integer

Instructions

A programmer was asked to develop a solution that efficiently extracts information from a dataset of size N (for instance, the size can be the number of rows in a table).

The programmer developed two algorithms and we want to know which algorithm is faster.

  • Algorithm A performs N * N operations 3 times, where N represents the dataset size
  • Algorithm B performs N operations 1000 times, where N represents the dataset size

The computer used to run the algorithms can execute one million operations per second and we are working on a dataset of size 10000.

Your task is to calculate the running times for algorithms A and B and assign them to two variables algorithm_a and algorithm_b, respectively.

You can assume that the running time for an algorithm is equal to the number of operations it performs divided by the number of operations that the computer can perform per second.