How do you use the triangular function in python?

If a, b and c are three sides of a triangle. Then,

s = (a+b+c)/2
area = √(s(s-a)*(s-b)*(s-c))

Source Code

# Python Program to find the area of triangle

a = 5
b = 6
c = 7

# Uncomment below to take inputs from the user
# a = float(input('Enter first side: '))
# b = float(input('Enter second side: '))
# c = float(input('Enter third side: '))

# calculate the semi-perimeter
s = (a + b + c) / 2

# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)

Output

The area of the triangle is 14.70

In this program, area of the triangle is calculated when three sides are given using Heron's formula.

If you need to calculate area of a triangle depending upon the input from the user, input() function can be used.

This Python program checks whether a given number by user is Triangular number or not.

Triangular Numbers are those numbers which are obtained by continued summation of the natural numbers 1, 2, 3, 4, 5, ...

Triangular Number Example: 15 is Triangular Number because it can be obtained by 1+2+3+4+5+6 i.e. 1+2+3+4+5+6=15

List of Triangular Numbers: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666,

Also try: Check Triangular Number Online & Generate Triangular Numbers Online

Python Source Code: Triangular Number Check


# Python program to check Triangular Number

# Function to check Triangular

def is_triangular(n):

    if n==0 or n==1:
        return True
    
    triangular_sum = 0

    for i in range(n):
        triangular_sum += i

        if triangular_sum == n:
            return True

        if i == n:
            return False


# Reading number
number = int(input('Enter number: '))

# Making decision
if is_triangular(number):
    print('%d is TRIANGULAR.' %(number))
else:
    print('%d is NOT TRIANGULAR.' %(number))

Triangular Number Check Python Output

Run 1:
-----------------
Enter number: 15
15 is TRIANGULAR.

Run 2:
-----------------
Enter number: 696
696 is NOT TRIANGULAR.

Full name

random.triangular

Syntax

random.triangular(low, high [, mode])

Description

The random.triangular function returns a random number N drawn from a triangular distribution such that low <= N <= high, with the mode specified in the third argument, mode.

If the mode argument is not specified, it defaults to the value (low + high) / 2, that is, the value located at the midpoint of the range considered.

Parameters

  • low: Lower limit of the range from which to extract the random number.
  • high: Upper limit of the range from which to extract the random number.
  • mode: Optional argument. Mode of the distribution.

Result

The random.triangular function returns a real number.

Examples

We can generate a random number in the range [10.0, 15.0] extracted from a triangular distribution with a default mode equal to 12.5 with the following code:

random.triangular(10, 15)

14.034414418629337

We could repeat the previous example forcing a mode equal to 14 with the following code:

random.triangular(10, 15, 14)

11.991174889023462

To confirm the distribution from which the random numbers are extracted we can generate 10 thousand random numbers in the range [10.0, 15.0] with default mode and show its histogram:

import matplotlib.pyplot as plt

plt.figure(figsize = (8, 4))
plt.hist([random.triangular(10, 15) for i in range(10000)], bins = 50)
plt.show()

How do you use the triangular function in python?

If we repeat the previous example setting the value 14 as mode, we obtain the following result:

plt.figure(figsize = (8, 4))
plt.hist([random.triangular(10, 15, 14) for i in range(10000)], bins = 50)
plt.show()

How do you use the triangular function in python?

random.triangular(left, mode, right, size=None)#

Draw samples from the triangular distribution over the interval [left, right].

The triangular distribution is a continuous probability distribution with lower limit left, peak at mode, and upper limit right. Unlike the other distributions, these parameters directly define the shape of the pdf.

Note

New code should use the triangular method of a default_rng() instance instead; please see the Quick Start.

Parametersleftfloat or array_like of floats

Lower limit.

modefloat or array_like of floats

The value where the peak of the distribution occurs. The value must fulfill the condition left <= mode <= right.

rightfloat or array_like of floats

Upper limit, must be larger than left.

sizeint or tuple of ints, optional

Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. If size is None (default), a single value is returned if left, mode, and right are all scalars. Otherwise, np.broadcast(left, mode, right).size samples are drawn.

Returnsoutndarray or scalar

Drawn samples from the parameterized triangular distribution.

Notes

The probability density function for the triangular distribution is

\[\begin{split}P(x;l, m, r) = \begin{cases} \frac{2(x-l)}{(r-l)(m-l)}& \text{for $l \leq x \leq m$},\\ \frac{2(r-x)}{(r-l)(r-m)}& \text{for $m \leq x \leq r$},\\ 0& \text{otherwise}. \end{cases}\end{split}\]

The triangular distribution is often used in ill-defined problems where the underlying distribution is not known, but some knowledge of the limits and mode exists. Often it is used in simulations.

References

1

Wikipedia, “Triangular distribution” https://en.wikipedia.org/wiki/Triangular_distribution

Examples

Draw values from the distribution and plot the histogram:

>>> import matplotlib.pyplot as plt
>>> h = plt.hist(np.random.triangular(-3, 0, 8, 100000), bins=200,
...              density=True)
>>> plt.show()

How do you use the triangular function in python?