Found 38 Articles for Misc Algorithms

Check whether a given point lies inside a Triangle

Ankith Reddy
Updated on 17-Jun-2020 09:12:37

2K+ Views

Three points of a triangle are given; another point P is also given to check whether the point P is inside the triangle or not.To solve the problem, let consider the points of the triangle are A, B, and C. When the area of triangle Δ𝐴𝐵𝐶 = Δ𝐴𝐵𝑃 + Δ𝑃𝐵𝐶 + Δ𝐴𝑃𝐶, then the point P is inside the triangle.Input and OutputInput: Points of the triangle {(0, 0), (20, 0), (10, 30)} and point p (10, 15) to check. Output: Point is inside the triangle.AlgorithmisInside(p1, p2, p3, p)Input: Three points of a triangle, the point p to check.Output: True, when ... Read More

Check if two line segments intersect

Samual Sam
Updated on 17-Jun-2020 09:40:21

4K+ Views

Let two line-segments are given. The points p1, p2 from the first line segment and q1, q2 from the second line segment. We have to check whether both line segments are intersecting or not.We can say that both line segments are intersecting when these cases are satisfied:When (p1, p2, q1) and (p1, p2, q2) have a different orientation and(q1, q2, p1) and (q1, q2, p2) have a different orientation.There is another condition is when (p1, p2, q1), (p1, p2, q2), (q1, q2, p1), (q1, q2, p2) are collinear.Input and OutputInput: Points of two line-segments Line-segment 1: (0, 0) to (5, ... Read More

Check if two given sets are disjoint?

George John
Updated on 17-Jun-2020 09:13:53

577 Views

Two sets are disjoint set when they have no common elements. In other words, if we get the intersection of two sets, then we will get null set.The method is simple, in this algorithm, two sets are given. We assume that both sets are already sorted, items are compared between two sets. when there is a match, then it is not a disjoint set, when no items are matched, they are disjoint sets.Input and OutputInput: Two sets: set1: {15, 12, 36, 21, 14} set2: {7, 89, 56, 32} Output: Both sets are disjointAlgorithmisDisjoint(set1, set2)Input: Two sets.Output: True when both sets ... Read More

Check Perfect Square or Not

Ankith Reddy
Updated on 17-Jun-2020 09:17:02

2K+ Views

A number is said to be a perfect square number if the square root of that number is an integer. In other words, when a square root is a whole number, then the number is called a perfect square number.We can check perfect square by finding the square root of that number and match with i again and again to get exact square root. When the square root crosses the value, it is not a perfect square number.But here to reduce effort, we have not checked the square root again and again. As we know that the square root of ... Read More

Check if a given point lies inside a Polygon

Arjun Thakur
Updated on 17-Jun-2020 09:18:13

2K+ Views

In this problem, one polygon is given, and a point P is also given. We need to check whether the point is inside the polygon or outside the polygon.For solving it we will draw a straight line from the point P. It extends to the infinity. The line is horizontal, or it is parallel to the x-axis.From that line, we will count how many times the line intersects the sides of a polygon. When the point is inside the polygon, it will intersect the sides, an odd number of times, if P is placed on any side of the polygon, ... Read More

Factorial of a large number

Monica Mona
Updated on 17-Jun-2020 09:19:15

956 Views

In computers, variables are stored in memory locations. But the size of the memory location is fixed, so when we try to find the factorial of some greater value like 15! or 20! the factorial value exceeds the memory range and returns wrong results.For calculation of large numbers, we have to use an array to store results. In each element of the array, is storing different digits of the result. But here we cannot multiply some number with the array directly, we have to perform manual multiplication process for all digits of the result array.Input and OutputInput: A big number: ... Read More

Babylonian method to find the square root

Chandu yadav
Updated on 17-Jun-2020 09:20:17

3K+ Views

The Babylonian method to find square root is based on one of the numerical method, which is based on the Newton- Raphson method for solving non-linear equations.The idea is simple, starting from an arbitrary value of x, and y as 1, we can simply get next approximation of root by finding the average of x and y. Then the y value will be updated with  number / x.Input and OutputInput: A number: 65 Output: The square root of 65 is: 8.06226AlgorithmsqRoot(number)Input: The number in real.Output: Square root of given number.Begin    x := number    y := 1    precision ... Read More

Adding base n numbers

Samual Sam
Updated on 17-Jun-2020 09:21:41

661 Views

In this problem, two numbers are given. The base of those numbers is n. We have to find the result of those number after addition in base n also.At first, the numbers are converted into decimal numbers. From the decimal values, we can simply add them. Finally, the numbers are converted to base n number again.The n base numbers are given as a string, because for those numbers, which have base greater than 9, it may contain some alphabets to represent numbers, like hexadecimal numbers, there are 6 letters (A-F).Input and OutputInput:  The base of a number system: 16 First ... Read More

Advertisements