Programming Search⌘K

Math in daily code

The small number tasks that programs need and that task lists hold: divisors, primes, factorials, means, roman numerals, complex numbers and matrices.

Concepts

  • Complex numbers in languages

    A complex number type holds a real part and an imaginary part, usually two floats. Some languages have it built in, some in the library and some not at all.

    core

Tasks

  • Check whether a number is prime

    Check whether an integer is prime with trial division up to its square root, and use a library test for big numbers. Numbers below 2 are not prime.

    10 languagesintro
  • Compute Fibonacci numbers

    Compute the nth Fibonacci number with a loop that keeps the last two numbers. F(92) is the largest in 64 bits. Naive recursion is slow, and a float formula is not exact.

    10 languagesintro
  • Compute a factorial

    Compute the factorial of a non-negative integer with a loop, with recursion or with a library function. 20! is the largest factorial in 64 bits, so larger ones need big integers.

    10 languagesintro
  • Compute the greatest common divisor

    Compute the greatest common divisor and the least common multiple of two integers with Euclid's algorithm or a library function, with negative numbers and zero.

    10 languagesintro
  • Compute the mean and median

    Compute the arithmetic mean and the median of a list of integers. The mean needs a floating-point division, and the median of an even-length list is the mean of the two middle values.

    10 languagesintro
  • Convert to and from Roman numerals

    Convert an integer from 1 to 3999 to a Roman numeral with a table of values, and parse a Roman numeral back. A strict parser rejects forms such as IIII and IC.

    10 languagesintro
  • Sum and multiply a list

    Compute the sum and the product of a list of integers with a loop or a library function. The sum of an empty list is 0 and the product is 1, and a large product can overflow.

    10 languagesintro
  • Use mathematical constants and functions

    Use pi, e, the square root, the power, the trigonometric functions and the logarithms of the math library, here to turn a distance and an angle in degrees into x and y coordinates.

    10 languagesintro
  • Do complex number arithmetic

    Add, multiply and divide two complex numbers, and find the absolute value and the phase. Use the built-in or library type, or a small type of your own.

    10 languagescore
  • Do matrix arithmetic

    Multiply two matrices and transpose a matrix, with a list of rows. The columns of the first matrix must match the rows of the second, and a times b is not b times a.

    10 languagescore