Number-theoretic Functions

What You'll Learn: In this tutorial, you'll discover various math functions available in Python's math module. These functions help you perform a wide range of mathematical tasks.

Number-theoretic Functions

  1. math.ceil() - Rounds a number up to the nearest integer.
    • Example:

      python
      import math
      x = math.ceil(2.3)
      print(x)  # Output: 3
      
  2. math.comb(n, k) - Returns the number of ways to choose k items from n items without repetition and order.
    • Example:

      python
      import math
      x = math.comb(5, 2)
      print(x)  # Output: 10
      
  3. math.copysign(x, y) - Returns x with the sign of y.
    • Example:

      python
      import math
      x = math.copysign(3, -1)
      print(x)  # Output: -3.0
      
  4. math.fabs(x) - Returns the absolute value of x.
    • Example:

      python
      import math
      x = math.fabs(-5.5)
      print(x)  # Output: 5.5
      
  5. math.factorial(x) - Returns the factorial of x.
    • Example:

      python
      import math
      x = math.factorial(4)
      print(x)  # Output: 24
      
  6. math.floor(x) - Rounds a number down to the nearest integer.
    • Example:

      python
      import math
      x = math.floor(2.9)
      print(x)  # Output: 2
      
  7. math.fmod(x, y) - Returns the remainder of x divided by y.
    • Example:

      python
      import math
      x = math.fmod(7, 3)
      print(x)  # Output: 1.0
      
  8. math.frexp(x) - Returns the mantissa and exponent of x.
    • Example:

      python
      import math
      x = math.frexp(8)
      print(x)  # Output: (0.5, 4)
      
  9. math.fsum(iterable) - Returns an accurate floating-point sum of values in an iterable.
    • Example:

      python
      import math
      x = math.fsum([0.1, 0.1, 0.1])
      print(x)  # Output: 0.30000000000000004
      
  10. math.gcd(a, b) - Returns the greatest common divisor of a and b.
    • Example:

      python
      import math
      x = math.gcd(8, 12)
      print(x)  # Output: 4
      
  11. math.isclose(a, b) - Returns True if a is close to b.
    • Example:

      python
      import math
      x = math.isclose(1.001, 1.002, abs_tol=0.01)
      print(x)  # Output: True
      
  12. math.isfinite(x) - Returns True if x is a finite number.
    • Example:

      python
      import math
      x = math.isfinite(10)
      print(x)  # Output: True
      
  13. math.isinf(x) - Returns True if x is an infinity.
    • Example:

      python
      import math
      x = math.isinf(float('inf'))
      print(x)  # Output: True
      
  14. math.isnan(x) - Returns True if x is a NaN (not a number).
    • Example:

      python
      import math
      x = math.isnan(float('nan'))
      print(x)  # Output: True
      
  15. math.isqrt(n) - Returns the integer square root of n.
    • Example:

      python
      import math
      x = math.isqrt(16)
      print(x)  # Output: 4
      
  16. math.lcm(a, b) - Returns the least common multiple of a and b.
    • Example:

      python
      import math
      x = math.lcm(6, 8)
      print(x)  # Output: 24
      
  17. math.ldexp(x, i) - Returns x * (2**i).
    • Example:

      python
      import math
      x = math.ldexp(2.5, 3)
      print(x)  # Output: 20.0
      
  18. math.modf(x) - Returns the fractional and integer parts of x.
    • Example:

      python
      import math
      x = math.modf(3.14)
      print(x)  # Output: (0.14, 3.0)
      
  19. math.nextafter(x, y) - Returns the next floating-point value after x towards y.
    • Example:

      python
      import math
      x = math.nextafter(1, 2)
      print(x)  # Output: 1.0000000000000002
      
  20. math.perm(n, k) - Returns the number of ways to choose and arrange k items from n items without repetition.
    • Example:

      python
      import math
      x = math.perm(5, 2)
      print(x)  # Output: 20
      
  21. math.prod(iterable) - Returns the product of all elements in the iterable.
    • Example:

      python
      import math
      x = math.prod([1, 2, 3, 4])
      print(x)  # Output: 24
      
  22. math.remainder(x, y) - Returns the IEEE 754-style remainder of x divided by y.
    • Example:

      python
      import math
      x = math.remainder(7, 3)
      print(x)  # Output: 1.0
      
  23. math.trunc(x) - Returns the truncated integer part of x.
    • Example:

      python
      import math
      x = math.trunc(3.14)
      print(x)  # Output: 3
      
  24. math.ulp(x) - Returns the value of the least significant bit of x.
    • Example:

      python
      import math
      x = math.ulp(1.0)
      print(x)  # Output: 2.220446049250313e-16
      

Try It Yourself: Fun Exercises

  1. Calculate Areas:
    • Use the math.pi constant and math.pow() function to calculate the area of a circle given its radius.
  2. Explore Trigonometric Functions:
    • Experiment with math.sin(), math.cos(), and math.tan() to see how they work with different angles.
  3. Work with Factorials:
    • Use math.factorial() to calculate the factorial of different numbers and understand how quickly the result grows.

Summary:

In this Python tutorial, we explored various number-theoretic functions available in the math module. These functions are useful for performing a wide range of mathematical operations in your programs. Keep experimenting and have fun with Python!