text
stringlengths 17
3.65k
| code
stringlengths 70
5.84k
|
---|---|
Area of a leaf inside a square | Python3 code to find the area of leaf inside a square ; Function to find the area of leaf ; Driver code | PI = 3.14159265 NEW_LINE def area_leaf ( a ) : NEW_LINE INDENT return ( a * a * ( PI / 2 - 1 ) ) NEW_LINE DEDENT a = 7 NEW_LINE print ( area_leaf ( a ) ) NEW_LINE |
Length of rope tied around three equal circles touching each other | Python3 code to find the length of rope ; Function to find the length of rope ; Driver code | PI = 3.14159265 NEW_LINE def length_rope ( r ) : NEW_LINE INDENT return ( ( 2 * PI * r ) + 6 * r ) NEW_LINE DEDENT r = 7 NEW_LINE print ( length_rope ( r ) ) NEW_LINE |
Number of triangles that can be formed with given N points | Python3 implementation of the above approach ; This function returns the required number of triangles ; Hash Map to store the frequency of slope corresponding to a point ( X , Y ) ; Iterate over all possible points ; Calculate slope of all elements with current element ; find the slope with reduced fraction ; Total number of ways to form a triangle having one point as current element ; Subtracting the total number of ways to form a triangle having the same slope or are collinear ; Driver Code | from collections import defaultdict NEW_LINE from math import gcd NEW_LINE def countTriangles ( P , N ) : NEW_LINE INDENT mp = defaultdict ( lambda : 0 ) NEW_LINE ans = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT mp . clear ( ) NEW_LINE for j in range ( i + 1 , N ) : NEW_LINE INDENT X = P [ i ] [ 0 ] - P [ j ] [ 0 ] NEW_LINE Y = P [ i ] [ 1 ] - P [ j ] [ 1 ] NEW_LINE g = gcd ( X , Y ) NEW_LINE X //= g NEW_LINE Y //= g NEW_LINE mp [ ( X , Y ) ] += 1 NEW_LINE DEDENT num = N - ( i + 1 ) NEW_LINE ans += ( num * ( num - 1 ) ) // 2 NEW_LINE for j in mp : NEW_LINE INDENT ans -= ( mp [ j ] * ( mp [ j ] - 1 ) ) // 2 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT P = [ [ 0 , 0 ] , [ 2 , 0 ] , [ 1 , 1 ] , [ 2 , 2 ] ] NEW_LINE N = len ( P ) NEW_LINE print ( countTriangles ( P , N ) ) NEW_LINE DEDENT |
Area of Incircle of a Right Angled Triangle | Python3 code to find the area of inscribed circle of right angled triangle ; Function to find the area of inscribed circle ; Driver code | PI = 3.14159265 NEW_LINE def area_inscribed ( P , B , H ) : NEW_LINE INDENT return ( ( P + B - H ) * ( P + B - H ) * ( PI / 4 ) ) NEW_LINE DEDENT P = 3 NEW_LINE B = 4 NEW_LINE H = 5 NEW_LINE print ( area_inscribed ( P , B , H ) ) NEW_LINE |
Area of Circumcircle of a Right Angled Triangle | Python3 code to find the area of circumscribed circle of right angled triangle ; Function to find the area of circumscribed circle ; Driver code | PI = 3.14159265 NEW_LINE def area_cicumscribed ( c ) : NEW_LINE INDENT return ( c * c * ( PI / 4 ) ) NEW_LINE DEDENT c = 8.0 NEW_LINE print ( area_cicumscribed ( c ) ) NEW_LINE |
Largest right circular cylinder that can be inscribed within a cone | Python 3 Program to find the biggest right circular cylinder that can be fit within a right circular cone ; Function to find the biggest right circular cylinder ; radius and height cannot be negative ; radius of right circular cylinder ; height of right circular cylinder ; volume of right circular cylinder ; Driver code | import math NEW_LINE def cyl ( r , h ) : NEW_LINE INDENT if ( r < 0 and h < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT R = ( 2 * r ) / 3 NEW_LINE H = ( 2 * h ) / 3 NEW_LINE V = 3.14 * math . pow ( R , 2 ) * H NEW_LINE return V NEW_LINE DEDENT r = 4 ; h = 8 ; NEW_LINE print ( cyl ( r , h ) , " " ) NEW_LINE |
Largest cube that can be inscribed within a right circular cylinder | Python 3 Program to find the biggest cube inscribed within a right circular cylinder ; Function to find the volume of the cube ; height and radius cannot be negative ; volume of the cube ; Driver code | import math NEW_LINE def cube ( h , r ) : NEW_LINE INDENT if ( h < 0 and r < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT a = math . pow ( h , 3 ) NEW_LINE return a NEW_LINE DEDENT h = 5 ; r = 4 ; NEW_LINE print ( cube ( h , r ) ) ; NEW_LINE |
Volume of biggest sphere within a right circular cylinder | Function to find the biggest sphere ; radius and height cannot be negative ; radius of sphere ; Driver code | def sph ( r , h ) : NEW_LINE INDENT if ( r < 0 and h < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT R = r NEW_LINE return float ( R ) NEW_LINE DEDENT r , h = 4 , 8 NEW_LINE print ( sph ( r , h ) ) NEW_LINE |
Volume of largest right circular cylinder within a Sphere | Python 3 Program to find the biggest right circular cylinder that can be fit within a sphere ; Function to find the biggest right circular cylinder ; radius cannot be negative ; volume of cylinder ; Driver code | import math NEW_LINE def cyl ( R ) : NEW_LINE INDENT if ( R < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT V = ( ( 2 * 3.14 * math . pow ( R , 3 ) ) / ( 3 * math . sqrt ( 3 ) ) ) ; NEW_LINE return float ( V ) NEW_LINE DEDENT R = 4 NEW_LINE print ( cyl ( R ) ) NEW_LINE |
Longest rod that can be inserted within a right circular cylinder | Python 3 Program to find the longest rod that can be fit within a right circular cylinder ; Function to find the side of the cube ; height and radius cannot be negative ; length of rod ; Driver code | import math NEW_LINE def rod ( h , r ) : NEW_LINE INDENT if ( h < 0 and r < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT l = ( math . sqrt ( math . pow ( h , 2 ) + 4 * math . pow ( r , 2 ) ) ) NEW_LINE return float ( l ) NEW_LINE DEDENT h , r = 4 , 1.5 NEW_LINE print ( rod ( h , r ) ) NEW_LINE |
Program to calculate the Area and Perimeter of Incircle of an Equilateral Triangle | Python3 code to find the area of inscribed circle of equilateral triangle ; Function to find the area of inscribed circle ; Function to find the perimeter of inscribed circle ; Driver code | import math NEW_LINE PI = 3.14159265 NEW_LINE def area_inscribed ( a ) : NEW_LINE INDENT return ( a * a * ( PI / 12 ) ) NEW_LINE DEDENT def perm_inscribed ( a ) : NEW_LINE INDENT return ( PI * ( a / math . sqrt ( 3 ) ) ) NEW_LINE DEDENT a = 6.0 NEW_LINE print ( " Area β of β inscribed β circle β is β : % β f " % area_inscribed ( a ) ) NEW_LINE print ( " Perimeter of inscribed circle is : % f " % perm_inscribed ( a ) ) NEW_LINE |
Largest cube that can be inscribed within a right circular cone | Python3 Program to find the biggest cube inscribed within a right circular cone ; Function to find the side of the cube ; height and radius cannot be negative ; side of the cube ; Driver code | import math NEW_LINE def cubeSide ( h , r ) : NEW_LINE INDENT if ( h < 0 and r < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT a = ( ( h * r * math . sqrt ( 2 ) ) / ( h + math . sqrt ( 2 ) * r ) ) NEW_LINE return a NEW_LINE DEDENT h = 5 ; r = 6 ; NEW_LINE print ( cubeSide ( h , r ) , " " ) NEW_LINE |
Largest right circular cone that can be inscribed within a sphere | Python 3 Program to find the biggest cone that can be inscribed within a sphere ; Function to find the radius of the cone ; radius cannot be negative ; radius of the cone ; Function to find the height of the cone ; side cannot be negative ; height of the cone ; Driver code | import math NEW_LINE def coner ( R ) : NEW_LINE INDENT if ( R < 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT r = ( 2 * math . sqrt ( 2 ) * R ) / 3 NEW_LINE return float ( r ) NEW_LINE DEDENT def coneh ( R ) : NEW_LINE INDENT if ( R < 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT h = ( 4 * R ) / 3 NEW_LINE return float ( h ) NEW_LINE DEDENT R = 10 NEW_LINE print ( " r β = β " , coner ( R ) , " , β " , " h β = β " , coneh ( R ) ) NEW_LINE |
Largest cone that can be inscribed within a cube | Python 3 Program to find the biggest cone inscribed within a cube ; Function to find the radius of the cone ; side cannot be negative ; radius of the cone ; Function to find the height of the cone ; side cannot be negative ; height of the cone ; Driver code | import math NEW_LINE def coneRadius ( a ) : NEW_LINE INDENT if ( a < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT r = a / math . sqrt ( 2 ) NEW_LINE return r NEW_LINE DEDENT def coneHeight ( a ) : NEW_LINE INDENT if ( a < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT h = a NEW_LINE return h NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 6 NEW_LINE print ( " r β = β " , coneRadius ( a ) , " h β = β " , coneHeight ( a ) ) NEW_LINE DEDENT |
Largest cube that can be inscribed within the sphere | Python 3 Program to find the biggest cube inscribed within a sphere ; Function to find the side of the cube ; radius cannot be negative ; side of the cube ; Driver code | from math import sqrt NEW_LINE def largestCube ( r ) : NEW_LINE INDENT if ( r < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT a = ( 2 * r ) / sqrt ( 3 ) NEW_LINE return a NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT r = 5 NEW_LINE print ( " { 0 : . 6 } " . format ( largestCube ( r ) ) ) NEW_LINE DEDENT |
Largest sphere that can be inscribed inside a cube | Function to find the radius of the sphere ; side cannot be negative ; radius of the sphere ; Driver code | def sphere ( a ) : NEW_LINE INDENT if ( a < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT r = a / 2 NEW_LINE return r NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 5 NEW_LINE print ( sphere ( a ) ) NEW_LINE DEDENT |
Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts | function that calculates the maximum no . of cuts ; Driver code ; Calling function . | def numberOfCuts ( M , N ) : NEW_LINE INDENT result = 0 NEW_LINE result = ( M - 1 ) * ( N - 1 ) NEW_LINE return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT M , N = 4 , 4 NEW_LINE Cuts = numberOfCuts ( M , N ) NEW_LINE print ( " Maximum β cuts β = β " , Cuts ) NEW_LINE DEDENT |
Find maximum volume of a cuboid from the given perimeter and area | Python3 implementation of the above approach ; function to return maximum volume ; calculate length ; calculate volume ; return result ; Driver code ; Function call | from math import sqrt NEW_LINE def maxVol ( P , A ) : NEW_LINE INDENT l = ( P - sqrt ( P * P - 24 * A ) ) / 12 NEW_LINE V = l * ( A / 2.0 - l * ( P / 4.0 - l ) ) NEW_LINE return V NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT P = 20 NEW_LINE A = 16 NEW_LINE print ( maxVol ( P , A ) ) NEW_LINE DEDENT |
Program to calculate area and perimeter of a rhombus whose diagonals are given | Python 3 Program to calculate area and perimeter of a rhombus using diagonals ; calculate area and perimeter of a rhombus ; Driver code | from math import sqrt , pow NEW_LINE def rhombusAreaPeri ( d1 , d2 ) : NEW_LINE INDENT area = ( d1 * d2 ) / 2 NEW_LINE perimeter = 2 * sqrt ( pow ( d1 , 2 ) + pow ( d2 , 2 ) ) NEW_LINE print ( " The β area β of β rhombus β with β diagonals " , d1 , " and " , d2 , " is " , area , " . " ) NEW_LINE print ( " The β perimeter β of β rhombus β with β diagonals " , d1 , " and " , d2 , " is " , perimeter , " . " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT d1 = 2 NEW_LINE d2 = 4 NEW_LINE rhombusAreaPeri ( d1 , d2 ) NEW_LINE DEDENT |
Area of decagon inscribed within the circle | Python3 Program to find the area of the decagon inscribed within a circle ; Function to find the area of the decagon ; radius cannot be negative ; area of the decagon ; Driver code | from math import sqrt , pow NEW_LINE def area ( r ) : NEW_LINE INDENT if r < 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT area = ( 5 * pow ( r , 2 ) * ( 3 - sqrt ( 5 ) ) * ( sqrt ( 5 ) + ( 2 * sqrt ( 5 ) ) ) ) / 4 NEW_LINE return area NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT r = 8 NEW_LINE print ( area ( r ) ) NEW_LINE DEDENT |
Maximum area of rectangle possible with given perimeter | Python3 program to find maximum area rectangle ; Function to find max area ; return area ; Driver code | from math import ceil , floor NEW_LINE def maxArea ( perimeter ) : NEW_LINE INDENT length = int ( ceil ( perimeter / 4 ) ) NEW_LINE breadth = int ( floor ( perimeter / 4 ) ) NEW_LINE return length * breadth NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 38 NEW_LINE print ( " Maximum β Area β = " , maxArea ( n ) ) NEW_LINE DEDENT |
Find the foot of perpendicular of a point in a 3 D plane | Function to find foot of perpendicular ; Driver Code ; function call | def foot ( a , b , c , d , x1 , y1 , z1 ) : NEW_LINE INDENT k = ( - a * x1 - b * y1 - c * z1 - d ) / ( a * a + b * b + c * c ) ; NEW_LINE x2 = a * k + x1 ; NEW_LINE y2 = b * k + y1 ; NEW_LINE z2 = c * k + z1 ; NEW_LINE print ( " x2 β = " , round ( x2 , 1 ) ) NEW_LINE print ( " y2 β = " , round ( y2 , 1 ) ) NEW_LINE print ( " z2 β = " , round ( z2 , 1 ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 1 NEW_LINE b = - 2 NEW_LINE c = 0 NEW_LINE d = 0 NEW_LINE x1 = - 1 NEW_LINE y1 = 3 NEW_LINE z1 = 4 NEW_LINE foot ( a , b , c , d , x1 , y1 , z1 ) NEW_LINE DEDENT |
Equation of parabola from its focus and directrix | Function to find equation of parabola . ; Driver Code | def equation_parabola ( x1 , y1 , a , b , c ) : NEW_LINE INDENT t = a * a + b * b NEW_LINE a1 = t - ( a * a ) NEW_LINE b1 = t - ( b * b ) ; NEW_LINE c1 = ( - 2 * t * x1 ) - ( 2 * c * a ) NEW_LINE d1 = ( - 2 * t * y1 ) - ( 2 * c * b ) NEW_LINE e1 = - 2 * a * b NEW_LINE f1 = ( - c * c ) + ( t * x1 * x1 ) + ( t * y1 * y1 ) NEW_LINE print ( " equation β of β parabola β is " , a1 , " x ^ 2 β + " , b1 , " y ^ 2 β + " , c1 , " x β + " , d1 , " y β + β " , e1 , " xy β + " , f1 , " = β 0 . " ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x1 , y1 , a , b , c = 0 , 0 , 3 , - 4 , 2 NEW_LINE equation_parabola ( x1 , y1 , a , b , c ) NEW_LINE DEDENT |
Minimum squares to evenly cut a rectangle | Python3 code to find minimum number of squares to make a given rectangle . ; if we take gcd ( l , w ) , this will be largest possible side for square , hence minimum number of square . ; Number of squares . ; Driver Code | import math NEW_LINE def countRectangles ( l , w ) : NEW_LINE INDENT squareSide = math . gcd ( l , w ) NEW_LINE return ( l * w ) / ( squareSide * squareSide ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT l = 4 NEW_LINE w = 6 NEW_LINE ans = countRectangles ( l , w ) NEW_LINE print ( int ( ans ) ) NEW_LINE DEDENT |
Equation of circle from center and radius | Function to find the equation of circle ; Printing result ; Driver code | def circle_equation ( x1 , y1 , r ) : NEW_LINE INDENT a = - 2 * x1 ; NEW_LINE b = - 2 * y1 ; NEW_LINE c = ( r * r ) - ( x1 * x1 ) - ( y1 * y1 ) ; NEW_LINE print ( " x ^ 2 β + β ( " , a , " x ) β + β " , end = " " ) ; NEW_LINE print ( " y ^ 2 β + β ( " , b , " y ) β = β " , end = " " ) ; NEW_LINE print ( c , " . " ) ; NEW_LINE DEDENT x1 = 2 ; NEW_LINE y1 = - 3 ; NEW_LINE r = 8 ; NEW_LINE circle_equation ( x1 , y1 , r ) ; NEW_LINE |
Program to find the Area and Perimeter of a Semicircle | Function for calculating the area ; Formula for finding the area ; Function for calculating the perimeter ; Formula for finding the perimeter ; driver code ; Get the radius ; Find the area ; Find the perimeter | def area ( r ) : NEW_LINE INDENT return ( 0.5 ) * ( 3.14 ) * ( r * r ) NEW_LINE DEDENT def perimeter ( r ) : NEW_LINE INDENT return ( 3.14 ) * ( r ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT r = 10 NEW_LINE print ( " The β Area β of β Semicircle : β " , area ( r ) ) NEW_LINE print ( " The β Perimeter β of β Semicircle : β " , perimeter ( r ) ) NEW_LINE DEDENT |
Check if the given vectors are at equilibrium or not | Function to check the equilibrium of three vectors ; summing the x coordinates ; summing the y coordinates ; summing the z coordinates ; Checking the condition for equilibrium ; Driver code ; Checking for equilibrium | def checkEquilibrium ( x1 , y1 , z1 , x2 , y2 , z2 , x3 , y3 , z3 ) : NEW_LINE INDENT resx = x1 + x2 + x3 NEW_LINE resy = y1 + y2 + y3 NEW_LINE resz = z1 + z2 + z3 NEW_LINE if ( resx == 0 and resy == 0 and resz == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT x1 = - 2 ; y1 = - 7 ; z1 = - 9 NEW_LINE x2 = 5 ; y2 = - 14 ; z2 = 14 NEW_LINE x3 = - 3 ; y3 = 21 ; z3 = - 5 NEW_LINE if ( checkEquilibrium ( x1 , y1 , z1 , x2 , y2 , z2 , x3 , y3 , z3 ) ) : NEW_LINE INDENT print ( " The β vectors β are β at β equilibrium . " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " The β vectors β are β not β at β equilibrium . " ) NEW_LINE DEDENT |
Find Tangent at a given point on the curve | function for find Tangent ; differentiate given equation ; check that point on the curve or not ; if differentiate is negative ; differentiate is positive ; differentiate is zero ; Driver code ; declare variable ; call function findTangent | def findTangent ( A , x , y ) : NEW_LINE INDENT dif = A - x * 2 NEW_LINE if y == ( 2 * x - x * x ) : NEW_LINE INDENT if dif < 0 : NEW_LINE INDENT print ( " y β = " , dif , " x " , ( x * dif ) + ( y ) ) NEW_LINE DEDENT elif dif > 0 : NEW_LINE INDENT print ( " y β = " , dif , " x + " , - x * dif + y ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not β Possible " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A , x , y = 2 , 2 , 0 NEW_LINE findTangent ( A , x , y ) NEW_LINE DEDENT |
Find Four points such that they form a square whose sides are parallel to x and y axes | find the largest square ; map to store which points exist ; mark the available points ; a nested loop to choose the opposite corners of square ; remove the chosen point ; remove the chosen point ; check if the other two points exist ; if the square is largest then store it ; add the removed point ; add the removed point ; display the largest square ; Driver code ; given points ; find the largest square | def findLargestSquare ( points , n ) : NEW_LINE INDENT m = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT m [ ( points [ i ] [ 0 ] , points [ i ] [ 1 ] ) ] = m . get ( ( points [ i ] [ 0 ] , points [ i ] [ 1 ] ) , 0 ) + 1 NEW_LINE DEDENT side = - 1 NEW_LINE x = - 1 NEW_LINE y = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT m [ ( points [ i ] [ 0 ] , points [ i ] [ 1 ] ) ] -= 1 NEW_LINE for j in range ( n ) : NEW_LINE INDENT m [ ( points [ j ] [ 0 ] , points [ j ] [ 1 ] ) ] -= 1 NEW_LINE if ( i != j and ( points [ i ] [ 0 ] - points [ j ] [ 0 ] ) == ( points [ i ] [ 1 ] - points [ j ] [ 1 ] ) ) : NEW_LINE INDENT if ( m [ ( points [ i ] [ 0 ] , points [ j ] [ 1 ] ) ] > 0 and m [ ( points [ j ] [ 0 ] , points [ i ] [ 1 ] ) ] > 0 ) : NEW_LINE INDENT if ( side < abs ( points [ i ] [ 0 ] - points [ j ] [ 0 ] ) or ( side == abs ( points [ i ] [ 0 ] - points [ j ] [ 0 ] ) and ( ( points [ i ] [ 0 ] * points [ i ] [ 0 ] + points [ i ] [ 1 ] * points [ i ] [ 1 ] ) < ( x * x + y * y ) ) ) ) : NEW_LINE INDENT x = points [ i ] [ 0 ] NEW_LINE y = points [ i ] [ 1 ] NEW_LINE side = abs ( points [ i ] [ 0 ] - points [ j ] [ 0 ] ) NEW_LINE DEDENT DEDENT DEDENT m [ ( points [ j ] [ 0 ] , points [ j ] [ 1 ] ) ] += 1 NEW_LINE DEDENT m [ ( points [ i ] [ 0 ] , points [ i ] [ 1 ] ) ] += 1 NEW_LINE DEDENT if ( side != - 1 ) : NEW_LINE INDENT print ( " Side β of β the β square β is β : β " , side , " , points of the square are " , x , " , " , y , " β " , ( x + side ) , " , β " , y , " β " , ( x ) , " , β " , ( y + side ) , " β " , ( x + side ) , " , β " , ( y + side ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No β such β square " ) NEW_LINE DEDENT DEDENT n = 6 NEW_LINE points = [ [ 1 , 1 ] , [ 4 , 4 ] , [ 3 , 4 ] , [ 4 , 3 ] , [ 1 , 4 ] , [ 4 , 1 ] ] NEW_LINE findLargestSquare ( points , n ) NEW_LINE |
Find length of Diagonal of Hexagon | Function to find the diagonal of the hexagon ; side cannot be negative ; diagonal of the hexagon ; Driver code | def hexadiagonal ( a ) : NEW_LINE INDENT if ( a < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return 2 * a NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 4 NEW_LINE print ( hexadiagonal ( a ) ) NEW_LINE DEDENT |
Program to find the side of the Octagon inscribed within the square | Python 3 Program to find the side of the octagon which can be inscribed within the square ; Function to find the side of the octagon ; side cannot be negative ; side of the octagon ; Driver code ; Get he square side ; Find the side length of the square | from math import sqrt NEW_LINE def octaside ( a ) : NEW_LINE INDENT if a < 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT s = a / ( sqrt ( 2 ) + 1 ) NEW_LINE return s NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 4 NEW_LINE print ( " { 0 : . 6 } " . format ( octaside ( a ) ) ) NEW_LINE DEDENT |
Check whether it is possible to join two points given on circle such that distance between them is k | Python3 program to implement above approach ; Return distance between the centers ; Distance between centers ; Case 5 ; SubCase 1 ; Subcase 2 ; Case 1 ; Case 3 ; Case 4 ; Case 2 ; Since value of k wialways be an integer ; Driver Code | from math import sqrt , ceil , floor NEW_LINE def dis ( x1 , y1 , x2 , y2 ) : NEW_LINE INDENT return sqrt ( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ) NEW_LINE DEDENT def check ( c1 , c2 , k ) : NEW_LINE INDENT min = 0 NEW_LINE max = 0 NEW_LINE de = dis ( c1 [ 0 ] , c1 [ 1 ] , c2 [ 0 ] , c2 [ 1 ] ) NEW_LINE if ( de == 0 ) : NEW_LINE INDENT if ( c1 [ 2 ] == c2 [ 2 ] ) : NEW_LINE INDENT min = 0 NEW_LINE max = 0 NEW_LINE DEDENT else : NEW_LINE INDENT if ( c1 [ 2 ] - c2 [ 2 ] > 0 ) : NEW_LINE INDENT min = c1 [ 2 ] - c2 [ 2 ] NEW_LINE max = min + 2 * c2 [ 2 ] NEW_LINE DEDENT else : NEW_LINE INDENT min = c2 [ 2 ] - c1 [ 2 ] NEW_LINE max = min + 2 * c1 [ 2 ] NEW_LINE DEDENT DEDENT DEDENT elif ( de >= c1 [ 2 ] + c2 [ 2 ] ) : NEW_LINE INDENT min = de - c1 [ 2 ] - c2 [ 2 ] NEW_LINE max = de + c1 [ 2 ] + c2 [ 2 ] NEW_LINE DEDENT elif ( de + c2 [ 2 ] < c1 [ 2 ] ) : NEW_LINE INDENT max = c2 [ 2 ] + c1 [ 2 ] + de NEW_LINE min = c1 [ 2 ] - de - c2 [ 2 ] NEW_LINE DEDENT elif ( de + c1 [ 2 ] < c2 [ 2 ] ) : NEW_LINE INDENT max = c2 [ 2 ] + c1 [ 2 ] + de NEW_LINE min = c2 [ 2 ] - de - c1 [ 2 ] NEW_LINE DEDENT elif ( ( de + c2 [ 2 ] >= c1 [ 2 ] ) or ( de + c1 [ 2 ] >= c2 [ 2 ] ) ) : NEW_LINE INDENT max = c2 [ 2 ] + c1 [ 2 ] + de NEW_LINE min = 0 NEW_LINE DEDENT temin = ceil ( min ) NEW_LINE re = max NEW_LINE if ( k >= temin and k <= re ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT circle1 = [ 0 , 0 , 5 ] NEW_LINE circle2 = [ 8 , 3 , 2 ] NEW_LINE k = 3 NEW_LINE if ( check ( circle1 , circle2 , k ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT |
Check if it is possible to move from ( 0 , 0 ) to ( x , y ) in N steps | Function to check whether it is possible or not to move from ( 0 , 0 ) to ( x , y ) in exactly n steps ; Driver code | def Arrive ( a , b , n ) : NEW_LINE INDENT if ( n >= abs ( a ) + abs ( b ) and ( n - ( abs ( a ) + abs ( b ) ) ) % 2 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT a = 5 NEW_LINE b = 5 NEW_LINE n = 11 NEW_LINE if ( Arrive ( a , b , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT |
Check if it is possible to move from ( a , 0 ) to ( b , 0 ) with given jumps | Function to check if it is possible ; Driver code ; function call | def Move ( a , x , b ) : NEW_LINE INDENT if ( ( ( ( b - a ) % x == 0 ) or ( ( b - a - 1 ) % x == 0 ) and a + 1 != b ) and b >= a ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 3 NEW_LINE x = 2 NEW_LINE b = 7 NEW_LINE if ( Move ( a , x , b ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT |
Area of a triangle inscribed in a rectangle which is inscribed in an ellipse | Function to find the area of the triangle ; length of a and b cannot be negative ; area of the triangle ; Driver code | def area ( a , b ) : NEW_LINE INDENT if ( a < 0 or b < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT A = a * b NEW_LINE return A NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 5 NEW_LINE b = 2 NEW_LINE print ( area ( a , b ) ) NEW_LINE DEDENT |
Circumradius of the rectangle | Python Program to find the radius of the circumcircle of the given rectangle ; Function to find the radius of the circumcircle ; the sides cannot be negative ; Radius of the circumcircle ; Return the radius ; Get the sides of the triangle ; Find the radius of the circumcircle | import math NEW_LINE def findRadiusOfcircumcircle ( l , b ) : NEW_LINE INDENT if ( l < 0 or b < 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT radius = ( math . sqrt ( pow ( l , 2 ) + pow ( b , 2 ) ) / 2 ) ; NEW_LINE return radius ; NEW_LINE DEDENT l = 4 ; NEW_LINE b = 3 ; NEW_LINE print ( findRadiusOfcircumcircle ( l , b ) ) ; NEW_LINE |
Area of the circumcircle of any triangles with sides given | Python3 Program to find the area the circumcircle of the given triangle ; Function to find the area of the circumcircle ; the sides cannot be negative ; semi - perimeter of the circle ; area of triangle ; area of the circle ; Get the sides of the triangle ; Find and print the area of the circumcircle | import math NEW_LINE def circlearea ( a , b , c ) : NEW_LINE INDENT if ( a < 0 or b < 0 or c < 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT p = ( a + b + c ) / 2 ; NEW_LINE At = math . sqrt ( p * ( p - a ) * ( p - b ) * ( p - c ) ) ; NEW_LINE A = 3.14 * pow ( ( ( a * b * c ) / ( 4 * At ) ) , 2 ) ; NEW_LINE return A ; NEW_LINE DEDENT a = 4 ; NEW_LINE b = 5 ; NEW_LINE c = 3 ; NEW_LINE print ( float ( circlearea ( a , b , c ) ) ) ; NEW_LINE |
Find the altitude and area of an isosceles triangle | Python 3 program to find the Altitude Area of an isosceles triangle ; function to find the altitude ; return altitude ; function to find the area ; return area ; Driver Code | import math NEW_LINE def altitude ( a , b ) : NEW_LINE INDENT return math . sqrt ( pow ( a , 2 ) - ( pow ( b , 2 ) / 4 ) ) NEW_LINE DEDENT def area ( b , h ) : NEW_LINE INDENT return ( 1 * b * h ) / 2 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 2 NEW_LINE b = 3 NEW_LINE h = altitude ( a , b ) NEW_LINE print ( " Altitude β = β " + str ( round ( h , 3 ) ) , end = " , β " ) NEW_LINE print ( " Area β = β " + str ( round ( area ( b , h ) , 3 ) ) ) NEW_LINE DEDENT |
Program to find the surface area of the square pyramid | function to find the surface area ; Driver Code ; surface area of the square pyramid | def surfaceArea ( b , s ) : NEW_LINE INDENT return 2 * b * s + pow ( b , 2 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT b = 3 NEW_LINE s = 4 NEW_LINE print ( surfaceArea ( b , s ) ) NEW_LINE DEDENT |
Area of largest triangle that can be inscribed within a rectangle | Function to find the area of the triangle ; a and b cannot be negative ; area of the triangle ; Driver code | def trianglearea ( l , b ) : NEW_LINE INDENT if ( l < 0 or b < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT area = ( l * b ) / 2 NEW_LINE return area NEW_LINE DEDENT l = 5 NEW_LINE b = 4 NEW_LINE print ( trianglearea ( l , b ) ) NEW_LINE |
Check if any square ( with one colored cell ) can be divided into two equal parts | function to check if it 's possible to divide the square in two equal parts ; if the painted square is linked anyway to the center of the square then it 's not possible ; else yes it 's possible ; Driver code ; initialize the size of the square ; initialize the dimension of the painted square | def halfsquare ( n , x , y ) : NEW_LINE INDENT half = n // 2 NEW_LINE if ( ( half == x or half == x - 1 ) and ( half == y or half == y - 1 ) ) : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 100 NEW_LINE x , y = 51 , 100 NEW_LINE halfsquare ( n , x , y ) NEW_LINE DEDENT |
Check if it is possible to reach vector B by rotating vector A and adding vector C to it | function to check if vector B is possible from vector A ; if d = 0 , then you need to add nothing to vector A ; for all four quadrants ; initialize all three vector coordinates | def check ( a , b , p , q ) : NEW_LINE INDENT d = p * p + q * q ; NEW_LINE if ( d == 0 ) : NEW_LINE INDENT return a == 0 and b == 0 ; NEW_LINE DEDENT else : NEW_LINE INDENT return ( ( a * p + b * q ) % d == 0 and ( b * p - a * q ) % d == 0 ) ; NEW_LINE DEDENT DEDENT def checks ( a , b , x , y , p , q ) : NEW_LINE INDENT if ( check ( a - x , b - y , p , q ) or check ( a + x , b + y , p , q ) or check ( a - y , b + x , p , q ) or check ( a + y , b - x , p , q ) ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT a = - 4 ; NEW_LINE b = - 2 ; NEW_LINE x = 0 ; NEW_LINE y = 0 ; NEW_LINE p = - 2 ; NEW_LINE q = - 1 ; NEW_LINE if ( checks ( a , b , x , y , p , q ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT |
Largest triangle that can be inscribed in an ellipse | Python 3 Program to find the biggest triangle which can be inscribed within the ellipse ; Function to find the area of the triangle ; a and b cannot be negative ; area of the triangle ; Driver Code | from math import * NEW_LINE def trianglearea ( a , b ) : NEW_LINE INDENT if a < 0 or b < 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT area = ( 3 * sqrt ( 3 ) * pow ( a , 2 ) ) / ( 4 * b ) NEW_LINE return area NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a , b = 4 , 2 NEW_LINE print ( round ( trianglearea ( a , b ) , 4 ) ) NEW_LINE DEDENT |
Area of the Largest square that can be inscribed in an ellipse | Function to find the area of the square ; a and b cannot be negative ; area of the square ; Driver code | def squarearea ( a , b ) : NEW_LINE INDENT if ( a < 0 or b < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT area = 4 * ( ( ( pow ( a , 2 ) + pow ( b , 2 ) ) / ( pow ( a , 2 ) * pow ( b , 2 ) ) ) ) NEW_LINE return area NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 4 NEW_LINE b = 2 NEW_LINE print ( squarearea ( a , b ) ) NEW_LINE DEDENT |
Largest triangle that can be inscribed in a semicircle | Function to find the area of the triangle ; the radius cannot be negative ; area of the triangle ; Driver Code | def trianglearea ( r ) : NEW_LINE INDENT if r < 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return r * r NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT r = 5 NEW_LINE print ( trianglearea ( r ) ) NEW_LINE DEDENT |
Largest square that can be inscribed in a semicircle | Function to find the area of the square ; the radius cannot be negative ; area of the square ; Driver code | def squarearea ( r ) : NEW_LINE INDENT if ( r < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT a = 4 * ( pow ( r , 2 ) / 5 ) NEW_LINE return a NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT r = 5 NEW_LINE print ( int ( squarearea ( r ) ) ) NEW_LINE DEDENT |
Area of Largest rectangle that can be inscribed in an Ellipse | Function to find the area of the rectangle ; a and b cannot be negative ; area of the rectangle ; Driver code | def rectanglearea ( a , b ) : NEW_LINE INDENT if a < 0 or b < 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return 2 * a * b NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a , b = 10 , 8 NEW_LINE print ( rectanglearea ( a , b ) ) NEW_LINE DEDENT |
Area of a largest square fit in a right angle triangle | Function to find the area of the biggest square ; the height or base or hypotenuse cannot be negative ; side of the square ; squaring to get the area ; Driver Code | def squareArea ( l , b , h ) : NEW_LINE INDENT if l < 0 or b < 0 or h < 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT a = ( l * b ) / ( l + b ) NEW_LINE return a * a NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l , b , h = 5 , 12 , 13 NEW_LINE print ( round ( squareArea ( l , b , h ) , 4 ) ) NEW_LINE DEDENT |
Queries to check if it is possible to join boxes in a circle | Python 3 implementation of above approach ; Print the answer to each query ; setting the flag for exception ; replacing the greater element in i and j ; checking if that box is not used in previous query . ; checking if connecting to the same box ; case 1 : x < i and y lies between i and j ; case 2 : x lies between i and j and y > j ; if flag is not reset inbetween . ; Driver code | MAX = 50 NEW_LINE def solveQuery ( n , q , qi , qj ) : NEW_LINE INDENT arr = [ None ] * MAX NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT arr [ i ] = 0 NEW_LINE DEDENT for k in range ( q ) : NEW_LINE INDENT flag = 0 NEW_LINE if ( qj [ k ] < qi [ k ] ) : NEW_LINE INDENT qj [ k ] , qi [ k ] = qi [ k ] , qj [ k ] NEW_LINE DEDENT if ( arr [ qi [ k ] ] != 0 or arr [ qj [ k ] ] != 0 ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT elif ( qi [ k ] == qj [ k ] ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( 1 , qi [ k ] ) : NEW_LINE INDENT if ( arr [ i ] != 0 and arr [ i ] < qj [ k ] and qi [ k ] < arr [ i ] ) : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT for i in range ( qi [ k ] + 1 , qj [ k ] ) : NEW_LINE INDENT if ( arr [ i ] != 0 and arr [ i ] > qj [ k ] ) : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE arr [ qi [ k ] ] = qj [ k ] NEW_LINE arr [ qj [ k ] ] = qi [ k ] NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 NEW_LINE q = 7 NEW_LINE qi = [ 1 , 2 , 2 , 2 , 9 , 10 , 8 ] NEW_LINE qj = [ 5 , 7 , 3 , 4 , 9 , 9 , 6 ] NEW_LINE solveQuery ( n , q , qi , qj ) NEW_LINE DEDENT |
Minimum squares to cover a rectangle | Python3 program to find the minimum number of squares to cover the surface of the rectangle with given dimensions ; function to count the number of squares that can cover the surface of the rectangle ; Driver code | import math NEW_LINE def squares ( l , b , a ) : NEW_LINE INDENT return math . ceil ( l / a ) * math . ceil ( b / a ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l = 11 NEW_LINE b = 23 NEW_LINE a = 14 NEW_LINE print ( squares ( l , b , a ) ) NEW_LINE DEDENT |
Smallest square formed with given rectangles | Recursive function to return gcd of a and b ; Everything divides 0 ; Base case ; a is greater ; Function to find the area of the smallest square ; the length or breadth or side cannot be negative ; LCM of length and breadth ; squaring to get the area ; Driver code | def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 or b == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( a == b ) : NEW_LINE INDENT return a NEW_LINE DEDENT if ( a > b ) : NEW_LINE INDENT return gcd ( a - b , b ) NEW_LINE DEDENT return gcd ( a , b - a ) NEW_LINE DEDENT def squarearea ( l , b ) : NEW_LINE INDENT if ( l < 0 or b < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT n = ( l * b ) / gcd ( l , b ) NEW_LINE return n * n NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT l = 6 NEW_LINE b = 4 NEW_LINE print ( int ( squarearea ( l , b ) ) ) NEW_LINE DEDENT |
Find all angles of a triangle in 3D | Python Code for finding all angles of a triangle ; function for finding the angle ; driver code | import math NEW_LINE def angle_triangle ( x1 , x2 , x3 , y1 , y2 , y3 , z1 , z2 , z3 ) : NEW_LINE INDENT num = ( x2 - x1 ) * ( x3 - x1 ) + ( y2 - y1 ) * ( y3 - y1 ) + ( z2 - z1 ) * ( z3 - z1 ) NEW_LINE den = math . sqrt ( ( x2 - x1 ) ** 2 + ( y2 - y1 ) ** 2 + ( z2 - z1 ) ** 2 ) * math . sqrt ( ( x3 - x1 ) ** 2 + ( y3 - y1 ) ** 2 + ( z3 - z1 ) ** 2 ) NEW_LINE angle = math . degrees ( math . acos ( num / den ) ) NEW_LINE return round ( angle , 3 ) NEW_LINE DEDENT x1 = - 1 NEW_LINE y1 = 3 NEW_LINE z1 = 2 NEW_LINE x2 = 2 NEW_LINE y2 = 3 NEW_LINE z2 = 5 NEW_LINE x3 = 3 NEW_LINE y3 = 5 NEW_LINE z3 = - 2 NEW_LINE angle_A = angle_triangle ( x1 , x2 , x3 , y1 , y2 , y3 , z1 , z2 , z3 ) NEW_LINE angle_B = angle_triangle ( x2 , x3 , x1 , y2 , y3 , y1 , z2 , z3 , z1 ) NEW_LINE angle_C = angle_triangle ( x3 , x2 , x1 , y3 , y2 , y1 , z3 , z2 , z1 ) NEW_LINE print ( " Angles β are β : " ) NEW_LINE print ( " angle β A β = β " , angle_A , " degree " ) NEW_LINE print ( " angle β B β = β " , angle_B , " degree " ) NEW_LINE print ( " angle β C β = β " , angle_C , " degree " ) NEW_LINE |
Minimum number of square tiles required to fill the rectangular floor | Function to find the number of tiles ; if breadth is divisible by side of square ; tiles required is N / s ; one more tile required ; if length is divisible by side of square ; tiles required is M / s ; one more tile required ; Driver Code ; input length and breadth of rectangle and side of square | def solve ( M , N , s ) : NEW_LINE INDENT if ( N % s == 0 ) : NEW_LINE INDENT N = N // s NEW_LINE DEDENT else : NEW_LINE INDENT N = ( N // s ) + 1 NEW_LINE DEDENT if ( M % s == 0 ) : NEW_LINE INDENT M = M // s NEW_LINE DEDENT else : NEW_LINE INDENT M = ( M // s ) + 1 NEW_LINE DEDENT return M * N NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N , M , s = 12 , 13 , 4 NEW_LINE print ( solve ( M , N , s ) ) NEW_LINE DEDENT |
Minimum number of square tiles required to fill the rectangular floor | Python 3 implementation of above approach ; Function to find the number of tiles ; no of tiles ; Driver Code ; input length and breadth of rectangle and side of square | import math NEW_LINE def solve ( M , N , s ) : NEW_LINE INDENT ans = ( ( math . ceil ( M / s ) ) * ( math . ceil ( N / s ) ) ) ; NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 12 NEW_LINE M = 13 NEW_LINE s = 4 NEW_LINE print ( solve ( M , N , s ) ) NEW_LINE DEDENT |
Program to find equation of a plane passing through 3 points | Function to find equation of plane . ; Driver Code | def equation_plane ( x1 , y1 , z1 , x2 , y2 , z2 , x3 , y3 , z3 ) : NEW_LINE INDENT a1 = x2 - x1 NEW_LINE b1 = y2 - y1 NEW_LINE c1 = z2 - z1 NEW_LINE a2 = x3 - x1 NEW_LINE b2 = y3 - y1 NEW_LINE c2 = z3 - z1 NEW_LINE a = b1 * c2 - b2 * c1 NEW_LINE b = a2 * c1 - a1 * c2 NEW_LINE c = a1 * b2 - b1 * a2 NEW_LINE d = ( - a * x1 - b * y1 - c * z1 ) NEW_LINE print " equation β of β plane β is β " , NEW_LINE print a , " x β + " , NEW_LINE print b , " y β + " , NEW_LINE print c , " z β + " , NEW_LINE print d , " = β 0 . " NEW_LINE DEDENT x1 = - 1 NEW_LINE y1 = 2 NEW_LINE z1 = 1 NEW_LINE x2 = 0 NEW_LINE y2 = - 3 NEW_LINE z2 = 2 NEW_LINE x3 = 1 NEW_LINE y3 = 1 NEW_LINE z3 = - 4 NEW_LINE equation_plane ( x1 , y1 , z1 , x2 , y2 , z2 , x3 , y3 , z3 ) NEW_LINE |
Perpendicular distance between a point and a Line in 2 D | Python program to find the distance between a given point and a given line in 2 D . ; Function to find distance ; Driver Code | import math NEW_LINE def shortest_distance ( x1 , y1 , a , b , c ) : NEW_LINE INDENT d = abs ( ( a * x1 + b * y1 + c ) ) / ( math . sqrt ( a * a + b * b ) ) NEW_LINE print ( " Perpendicular β distance β is " ) , d NEW_LINE DEDENT x1 = 5 NEW_LINE y1 = 6 NEW_LINE a = - 2 NEW_LINE b = 3 NEW_LINE c = 4 NEW_LINE shortest_distance ( x1 , y1 , a , b , c ) NEW_LINE |
Program to determine the octant of the axial plane | Function to print octant ; Driver Code | def octant ( x , y , z ) : NEW_LINE INDENT if x >= 0 and y >= 0 and z >= 0 : NEW_LINE INDENT print " Point β lies β in β 1st β octant " NEW_LINE DEDENT elif x < 0 and y >= 0 and z >= 0 : NEW_LINE INDENT print " Point β lies β in β 2nd β octant " NEW_LINE DEDENT elif x < 0 and y < 0 and z >= 0 : NEW_LINE INDENT print " Point β lies β in β 3rd β octant " NEW_LINE DEDENT elif x >= 0 and y < 0 and z >= 0 : NEW_LINE INDENT print " Point β lies β in β 4th β octant " NEW_LINE DEDENT elif x >= 0 and y >= 0 and z < 0 : NEW_LINE INDENT print " Point β lies β in β 5th β octant " NEW_LINE DEDENT elif x < 0 and y >= 0 and z < 0 : NEW_LINE INDENT print " Point β lies β in β 6th β octant " NEW_LINE DEDENT elif x < 0 and y < 0 and z < 0 : NEW_LINE INDENT print " Point β lies β in β 7th β octant " NEW_LINE DEDENT elif x >= 0 and y < 0 and z < 0 : NEW_LINE INDENT print " Point β lies β in β 8th β octant " NEW_LINE DEDENT DEDENT x , y , z = 2 , 3 , 4 NEW_LINE octant ( x , y , z ) NEW_LINE x , y , z = - 4 , 2 , - 8 NEW_LINE octant ( x , y , z ) NEW_LINE x , y , z = - 6 , - 2 , 8 NEW_LINE octant ( x , y , z ) NEW_LINE |
Number of triangles in a plane if no more than two points are collinear | Function to find number of triangles in a plane . ; Formula to find number of triangles nC3 = n * ( n - 1 ) * ( n - 2 ) / 6 ; Driver Code | def countNumberOfTriangles ( n ) : NEW_LINE INDENT return ( n * ( n - 1 ) * ( n - 2 ) // 6 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE print ( countNumberOfTriangles ( n ) ) NEW_LINE DEDENT |
Centered Dodecagonal Number | Function to calculate centered dodecagonal number ; Formula to calculate nth centered dodecagonal number ; Driver code | def centeredDodecagonal ( n ) : NEW_LINE INDENT return 6 * n * ( n - 1 ) + 1 ; NEW_LINE DEDENT n = 2 NEW_LINE print ( centeredDodecagonal ( n ) ) ; NEW_LINE n = 9 NEW_LINE print ( centeredDodecagonal ( n ) ) ; NEW_LINE |
Centered tridecagonal number | Function to find centered tridecagonal number ; Formula to calculate nth centered tridecagonal number ; Driver Code | def centeredTridecagonalNum ( n ) : NEW_LINE INDENT return ( 13 * n * ( n - 1 ) + 2 ) // 2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE print ( centeredTridecagonalNum ( n ) ) NEW_LINE n = 10 NEW_LINE print ( centeredTridecagonalNum ( n ) ) NEW_LINE DEDENT |
Pentagonal Pyramidal Number | function to get nth Pentagonal pyramidal number . ; Running loop from 1 to n ; get nth pentagonal number ; add to sum ; Driver Program | def pentagon_pyramidal ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT p = ( 3 * i * i - i ) / 2 NEW_LINE sum = sum + p NEW_LINE DEDENT return sum NEW_LINE DEDENT n = 4 NEW_LINE print ( int ( pentagon_pyramidal ( n ) ) ) NEW_LINE |
Pentagonal Pyramidal Number | function to get nth Pentagonal pyramidal number . ; Driver Program | def pentagon_pyramidal ( n ) : NEW_LINE INDENT return n * n * ( n + 1 ) / 2 NEW_LINE DEDENT n = 4 NEW_LINE print ( int ( pentagon_pyramidal ( n ) ) ) NEW_LINE |
Check if three straight lines are concurrent or not | Return true if three line are concurrent , else false . ; Driven Program | def checkConcurrent ( a1 , b1 , c1 , a2 , b2 , c2 , a3 , b3 , c3 ) : NEW_LINE INDENT return ( a3 * ( b1 * c2 - b2 * c1 ) + b3 * ( c1 * a2 - c2 * a1 ) + c3 * ( a1 * b2 - a2 * b1 ) == 0 ) NEW_LINE DEDENT a1 = 2 NEW_LINE b1 = - 3 NEW_LINE c1 = 5 NEW_LINE a2 = 3 NEW_LINE b2 = 4 NEW_LINE c2 = - 7 NEW_LINE a3 = 9 NEW_LINE b3 = - 5 NEW_LINE c3 = 8 NEW_LINE if ( checkConcurrent ( a1 , b1 , c1 , a2 , b2 , c2 , a3 , b3 , c3 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT |
Centered Octadecagonal Number | Centered octadecagonal number function ; Formula to calculate nth centered octadecagonal number & return it into main function . ; Driver Code | def center_octadecagon_num ( n ) : NEW_LINE INDENT return ( 9 * n * n - 9 * n + 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE print ( n , " rd β centered β octadecagonal β " + " number β : β " , center_octadecagon_num ( n ) ) NEW_LINE n = 13 NEW_LINE print ( n , " th β centered β octadecagonal β " + " number β : β " , center_octadecagon_num ( n ) ) NEW_LINE DEDENT |
Centered decagonal number | Centered decagonal number function ; Formula to calculate nth centered decagonal number & return it into main function . ; Driver Code | def centereddecagonalnum ( n ) : NEW_LINE INDENT return ( 5 * n * n + 5 * n + 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE print ( n , " th β centered β decagonal β " + " number β : β " , centereddecagonalnum ( n ) ) NEW_LINE n = 9 NEW_LINE print ( n , " th β centered β decagonal β " + " number β : β " , centereddecagonalnum ( n ) ) NEW_LINE DEDENT |
Lexicographically Kth smallest way to reach given coordinate from origin | Return ( a + b ) ! / a ! b ! ; finding ( a + b ) ! ; finding ( a + b ) ! / a ! ; finding ( a + b ) ! / b ! ; Return the Kth smallest way to reach given coordinate from origin ; if at origin ; if on y - axis ; decrement y . ; Move vertical ; recursive call to take next step . ; If on x - axis ; decrement x . ; Move horizontal . ; recursive call to take next step . ; If x + y C x is greater than K ; Move Horizontal ; recursive call to take next step . ; Move vertical ; recursive call to take next step . ; Driver Code | def factorial ( a , b ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( 1 , a + b + 1 ) : NEW_LINE INDENT res = res * i NEW_LINE DEDENT for i in range ( 1 , a + 1 ) : NEW_LINE INDENT res = res // i NEW_LINE DEDENT for i in range ( 1 , b + 1 ) : NEW_LINE INDENT res = res // i NEW_LINE DEDENT return res NEW_LINE DEDENT def Ksmallest ( x , y , k ) : NEW_LINE INDENT if x == 0 and y == 0 : NEW_LINE INDENT return NEW_LINE DEDENT elif x == 0 : NEW_LINE INDENT y -= 1 NEW_LINE print ( " V " , end = " " ) NEW_LINE Ksmallest ( x , y , k ) NEW_LINE DEDENT elif y == 0 : NEW_LINE INDENT x -= 1 NEW_LINE print ( " H " , end = " " ) NEW_LINE Ksmallest ( x , y , k ) NEW_LINE DEDENT else : NEW_LINE INDENT if factorial ( x - 1 , y ) > k : NEW_LINE INDENT print ( " H " , end = " " ) NEW_LINE Ksmallest ( x - 1 , y , k ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " V " , end = " " ) NEW_LINE Ksmallest ( x , y - 1 , k - factorial ( x - 1 , y ) ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x , y , k = 2 , 2 , 2 NEW_LINE Ksmallest ( x , y , k ) NEW_LINE DEDENT |
Centered pentagonal number | Function to calculate Centered pentagonal number . ; Formula to calculate nth Centered pentagonal number . ; Driver Code | def centered_pentagonal_Num ( n ) : NEW_LINE INDENT return ( 5 * n * n - 5 * n + 2 ) // 2 NEW_LINE DEDENT n = 7 NEW_LINE print ( " % sth β Centered β pentagonal β number β : β " % n , centered_pentagonal_Num ( n ) ) NEW_LINE |
Find maximum and minimum distance between magnets | Python 3 program for max and min distance ; Function for finding distance between pivots ; Function for minimum distance ; Function for maximum distance ; Drivers code | import math NEW_LINE def pivotDis ( x0 , y0 , x1 , y1 ) : NEW_LINE INDENT return math . sqrt ( ( x1 - x0 ) * ( x1 - x0 ) + ( y1 - y0 ) * ( y1 - y0 ) ) NEW_LINE DEDENT def minDis ( D , r1 , r2 ) : NEW_LINE INDENT return max ( ( D - r1 - r2 ) , 0 ) NEW_LINE DEDENT def maxDis ( D , r1 , r2 ) : NEW_LINE INDENT return D + r1 + r2 NEW_LINE DEDENT x0 = 0 NEW_LINE y0 = 0 NEW_LINE x1 = 8 NEW_LINE y1 = 0 NEW_LINE r1 = 4 NEW_LINE r2 = 5 NEW_LINE D = pivotDis ( x0 , y0 , x1 , y1 ) NEW_LINE print ( " Distance β while β repulsion β = β " , int ( maxDis ( D , r1 , r2 ) ) ) NEW_LINE print ( " Distance β while β attraction β = β " , minDis ( D , r1 , r2 ) ) NEW_LINE |
Maximize a value for a semicircle of given radius | Function to find the maximum value of F ; using the formula derived for getting the maximum value of F ; Drivers code | def maximumValueOfF ( R ) : NEW_LINE INDENT return 4 * R * R + 0.25 NEW_LINE DEDENT R = 3 NEW_LINE print ( maximumValueOfF ( R ) ) NEW_LINE |
Find the other end point of a line with given one end and mid | function to find the end point of a line ; find end point for x coordinates ; find end point for y coordinates ; Driven Program | def otherEndPoint ( x1 , y1 , m1 , m2 ) : NEW_LINE INDENT x2 = ( 2 * m1 - x1 ) NEW_LINE y2 = ( 2 * m2 - y1 ) NEW_LINE print ( " x2 β = β { } , β y2 β = β { } " . format ( x2 , y2 ) ) NEW_LINE DEDENT x1 = - 4 NEW_LINE y1 = - 1 NEW_LINE m1 = 3 NEW_LINE m2 = 5 NEW_LINE otherEndPoint ( x1 , y1 , m1 , m2 ) NEW_LINE |
Coordinates of rectangle with given points lie inside | function to print coordinate of smallest rectangle ; find Xmax and Xmin ; find Ymax and Ymin ; print all four coordinates ; driver program | def printRect ( X , Y , n ) : NEW_LINE INDENT Xmax = max ( X ) NEW_LINE Xmin = min ( X ) NEW_LINE Ymax = max ( Y ) NEW_LINE Ymin = min ( Y ) NEW_LINE print ( " { " , Xmin , " , β " , Ymin , " } " , sep = " " ) NEW_LINE print ( " { " , Xmin , " , β " , Ymax , " } " , sep = " " ) NEW_LINE print ( " { " , Xmax , " , β " , Ymax , " } " , sep = " " ) NEW_LINE print ( " { " , Xmax , " , β " , Ymin , " } " , sep = " " ) NEW_LINE DEDENT X = [ 4 , 3 , 6 , 1 , - 1 , 12 ] NEW_LINE Y = [ 4 , 1 , 10 , 3 , 7 , - 1 ] NEW_LINE n = len ( X ) NEW_LINE printRect ( X , Y , n ) NEW_LINE |
Check if a line passes through the origin | Python program to find if line passing through two coordinates also passes through origin or not ; Driver code | def checkOrigin ( x1 , y1 , x2 , y2 ) : NEW_LINE INDENT return ( x1 * ( y2 - y1 ) == y1 * ( x2 - x1 ) ) NEW_LINE DEDENT if ( checkOrigin ( 1 , 28 , 2 , 56 ) == True ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT |
Number of horizontal or vertical line segments to connect 3 points | Python program to find number of horizontal ( or vertical line segments needed to connect three points . ; Function to check if the third point forms a rectangle with other two points at corners ; Returns true if point k can be used as a joining point to connect using two line segments ; Check for the valid polyline with two segments ; Check whether the X - coordinates or Y - cocordinates are same . ; Iterate over all pairs to check for two line segments ; Otherwise answer is three . ; driver code | import math NEW_LINE def isBetween ( a , b , c ) : NEW_LINE INDENT return min ( a , b ) <= c and c <= max ( a , b ) NEW_LINE DEDENT def canJoin ( x , y , i , j , k ) : NEW_LINE INDENT return ( x [ k ] == x [ i ] or x [ k ] == x [ j ] ) and isBetween ( y [ i ] , y [ j ] , y [ k ] ) or ( y [ k ] == y [ i ] or y [ k ] == y [ j ] ) and isBetween ( x [ i ] , x [ j ] , x [ k ] ) NEW_LINE DEDENT def countLineSegments ( x , y ) : NEW_LINE INDENT if ( ( x [ 0 ] == x [ 1 ] and x [ 1 ] == x [ 2 ] ) or ( y [ 0 ] == y [ 1 ] and y [ 1 ] == y [ 2 ] ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif ( canJoin ( x , y , 0 , 1 , 2 ) or canJoin ( x , y , 0 , 2 , 1 ) or canJoin ( x , y , 1 , 2 , 0 ) ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT else : NEW_LINE INDENT return 3 NEW_LINE DEDENT DEDENT x = [ - 1 , - 1 , 4 ] NEW_LINE y = [ - 1 , 3 , 3 ] NEW_LINE print ( countLineSegments ( x , y ) ) NEW_LINE |
Pythagorean Quadruple | Python code to detect Pythagorean Quadruples . ; function for checking ; driver code | import math NEW_LINE def pythagorean_quadruple ( a , b , c , d ) : NEW_LINE INDENT sum = a * a + b * b + c * c ; NEW_LINE if ( d * d == sum ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT a = 1 NEW_LINE b = 2 NEW_LINE c = 2 NEW_LINE d = 3 NEW_LINE if ( pythagorean_quadruple ( a , b , c , d ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT |
Program for Volume and Surface area of Frustum of Cone | Python3 code to calculate Volume and Surface area of frustum of cone ; Function to calculate Volume of frustum of cone ; Function to calculate Curved Surface area of frustum of cone ; Function to calculate Total Surface area of frustum of cone ; Driver Code ; Printing value of volume and surface area | import math NEW_LINE pi = math . pi NEW_LINE def volume ( r , R , h ) : NEW_LINE INDENT return 1 / 3 * pi * h * ( r * r + R * R + r * R ) NEW_LINE DEDENT def curved_surface_area ( r , R , l ) : NEW_LINE INDENT return pi * l * ( R + r ) NEW_LINE DEDENT def total_surface_area ( r , R , l , h ) : NEW_LINE INDENT return pi * l * ( R + r ) + pi * ( r * r + R * R ) NEW_LINE DEDENT small_radius = 3 NEW_LINE big_radius = 8 NEW_LINE slant_height = 13 NEW_LINE height = 12 NEW_LINE print ( " Volume β Of β Frustum β of β Cone β : β " , end = ' ' ) NEW_LINE print ( volume ( small_radius , big_radius , height ) ) NEW_LINE print ( " Curved β Surface β Area β Of β Frustum " + " β of β Cone β : β " , end = ' ' ) NEW_LINE print ( curved_surface_area ( small_radius , big_radius , slant_height ) ) NEW_LINE print ( " Total β Surface β Area β Of β Frustum " + " β of β Cone β : β " , end = ' ' ) NEW_LINE print ( total_surface_area ( small_radius , big_radius , slant_height , height ) ) NEW_LINE |
Program to find Perimeter / Circumference of Square and Rectangle | Python3 Program to find Circumference of a square ; Driver code | def Circumference ( a ) : NEW_LINE INDENT return ( 4 * a ) NEW_LINE DEDENT a = 5 NEW_LINE c = Circumference ( a ) NEW_LINE print ( " Circumference β of β a β " + " square β is β % β d " % ( c ) ) NEW_LINE |
Maximum area of quadrilateral | Python3 program to find maximum area of a quadrilateral ; Calculating the semi - perimeter of the given quadrilateral ; Applying Brahmagupta 's formula to get maximum area of quadrilateral ; Driver code | import math NEW_LINE def maxArea ( a , b , c , d ) : NEW_LINE INDENT semiperimeter = ( a + b + c + d ) / 2 NEW_LINE return math . sqrt ( ( semiperimeter - a ) * ( semiperimeter - b ) * ( semiperimeter - c ) * ( semiperimeter - d ) ) NEW_LINE DEDENT a = 1 NEW_LINE b = 2 NEW_LINE c = 1 NEW_LINE d = 2 NEW_LINE print ( " % .2f " % maxArea ( a , b , c , d ) ) NEW_LINE |
Direction of a Point from a Line Segment | Structure for point in cartesian plane . ; Constant integers for directions ; Subtracting co - ordinates of point A from B and P , to make A as origin ; Determining cross Product ; Return RIGHT if cross product is positive ; Return LEFT if cross product is negative ; Return ZERO if cross product is zero ; Driver code ; A . y = 10 A ( - 30 , 10 ) ; B . y = - 15 B ( 29 , - 15 ) ; P . y = 28 P ( 15 , 28 ) | class point : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . x = 0 NEW_LINE self . y = 0 NEW_LINE DEDENT DEDENT RIGHT = 1 NEW_LINE LEFT = - 1 NEW_LINE ZERO = 0 NEW_LINE def directionOfPoint ( A , B , P ) : NEW_LINE INDENT global RIGHT , LEFT , ZERO NEW_LINE B . x -= A . x NEW_LINE B . y -= A . y NEW_LINE P . x -= A . x NEW_LINE P . y -= A . y NEW_LINE cross_product = B . x * P . y - B . y * P . x NEW_LINE if ( cross_product > 0 ) : NEW_LINE INDENT return RIGHT NEW_LINE DEDENT if ( cross_product < 0 ) : NEW_LINE INDENT return LEFT NEW_LINE DEDENT return ZERO NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = point ( ) NEW_LINE B = point ( ) NEW_LINE P = point ( ) NEW_LINE A . x = - 30 NEW_LINE B . x = 29 NEW_LINE P . x = 15 NEW_LINE direction = directionOfPoint ( A , B , P ) NEW_LINE if ( direction == 1 ) : NEW_LINE INDENT print ( " Right β Direction " ) NEW_LINE DEDENT elif ( direction == - 1 ) : NEW_LINE INDENT print ( " Left β Direction " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Point β is β on β the β Line " ) NEW_LINE DEDENT DEDENT |
Find minimum radius such that atleast k point lie inside the circle | Return minimum distance required so that aleast k point lie inside the circle . ; Finding distance between of each point from origin ; Sorting the distance ; Driver Program | def minRadius ( k , x , y , n ) : NEW_LINE INDENT dis = [ 0 ] * n NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT dis [ i ] = x [ i ] * x [ i ] + y [ i ] * y [ i ] NEW_LINE DEDENT dis . sort ( ) NEW_LINE return dis [ k - 1 ] NEW_LINE DEDENT k = 3 NEW_LINE x = [ 1 , - 1 , 1 ] NEW_LINE y = [ 1 , - 1 , - 1 ] NEW_LINE n = len ( x ) NEW_LINE print ( minRadius ( k , x , y , n ) ) NEW_LINE |
Program for Area And Perimeter Of Rectangle | Utility function ; Driver function | def areaRectangle ( a , b ) : NEW_LINE INDENT return ( a * b ) NEW_LINE DEDENT def perimeterRectangle ( a , b ) : NEW_LINE INDENT return ( 2 * ( a + b ) ) NEW_LINE DEDENT a = 5 ; NEW_LINE b = 6 ; NEW_LINE print ( " Area β = β " , areaRectangle ( a , b ) ) NEW_LINE print ( " Perimeter β = β " , perimeterRectangle ( a , b ) ) NEW_LINE |
Program for Area Of Square | Python3 code to find the area of the square ; Driver Code | def areaSquare ( side ) : NEW_LINE INDENT area = side * side NEW_LINE return area NEW_LINE DEDENT side = 4 NEW_LINE print ( areaSquare ( side ) ) NEW_LINE |
Minimum Perimeter of n blocks | Python3 program to find minimum perimeter using n blocks . ; if n is a perfect square ; Number of rows ; perimeter of the rectangular grid ; if there are blocks left ; Driver code | import math NEW_LINE def minPerimeter ( n ) : NEW_LINE INDENT l = math . sqrt ( n ) NEW_LINE sq = l * l NEW_LINE if ( sq == n ) : NEW_LINE INDENT return l * 4 NEW_LINE DEDENT else : NEW_LINE INDENT row = n / l NEW_LINE perimeter = 2 * ( l + row ) NEW_LINE if ( n % l != 0 ) : NEW_LINE INDENT perimeter += 2 NEW_LINE DEDENT return perimeter NEW_LINE DEDENT DEDENT n = 10 NEW_LINE print ( int ( minPerimeter ( n ) ) ) NEW_LINE |
Find if it 's possible to rotate the page by an angle or not. | Function to find if it 's possible to rotate page or not ; Calculating distance b / w points ; If distance is not equal ; If the points are in same line ; Points a , b , and c | def possibleOrNot ( a1 , a2 , b1 , b2 , c1 , c2 ) : NEW_LINE INDENT dis1 = ( pow ( b1 - a1 , 2 ) + pow ( b2 - a2 , 2 ) ) NEW_LINE dis2 = ( pow ( c1 - b1 , 2 ) + pow ( c2 - b2 , 2 ) ) NEW_LINE if ( dis1 != dis2 ) : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT elif ( b1 == ( ( a1 + c1 ) // 2.0 ) and b2 == ( ( a2 + c2 ) // 2.0 ) ) : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT DEDENT a1 , b1 , c1 = 1 , 2 , 3 NEW_LINE a2 = b2 = c2 = 0 NEW_LINE possibleOrNot ( a1 , a2 , b1 , b2 , c1 , c2 ) NEW_LINE |
Check if two given circles touch or intersect each other | Python3 program to check if two circles touch each other or not . ; Driver code | def circle ( x1 , y1 , x2 , y2 , r1 , r2 ) : NEW_LINE INDENT distSq = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ; NEW_LINE radSumSq = ( r1 + r2 ) * ( r1 + r2 ) ; NEW_LINE if ( distSq == radSumSq ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif ( distSq > radSumSq ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT x1 = - 10 NEW_LINE y1 = 8 NEW_LINE x2 = 14 NEW_LINE y2 = - 24 NEW_LINE r1 = 30 NEW_LINE r2 = 10 NEW_LINE t = circle ( x1 , y1 , x2 , y2 , r1 , r2 ) NEW_LINE if ( t == 1 ) : NEW_LINE INDENT print ( " Circle β touch β to β each β other . " ) NEW_LINE DEDENT elif ( t < 0 ) : NEW_LINE INDENT print ( " Circle β not β touch β to β each β other . " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Circle β intersect β to β each β other . " ) NEW_LINE DEDENT |
Count of obtuse angles in a circle with ' k ' equidistant points between 2 given points | C ++ program to count number of obtuse angles for given two points . ; There are two arcs connecting a and b . Let us count points on both arcs . ; Both arcs have same number of points ; Points on smaller arc is answer ; Driver code | def countObtuseAngles ( a , b , k ) : NEW_LINE INDENT c1 = ( b - a ) - 1 NEW_LINE c2 = ( k - b ) + ( a - 1 ) NEW_LINE if ( c1 == c2 ) : NEW_LINE return 0 NEW_LINE return min ( c1 , c2 ) NEW_LINE DEDENT k , a , b = 6 , 1 , 3 NEW_LINE print countObtuseAngles ( a , b , k ) NEW_LINE |
Count of acute , obtuse and right triangles with given sides | Find the number of acute , right , obtuse triangle that can be formed from given array . ; Finding the square of each element of array ; Sort the sides of array and their squares . ; x for acute triangles y for right triangles z for obtuse triangles ; Finding the farthest point p where a ^ 2 + b ^ 2 >= c ^ 2. ; Finding the farthest point q where a + b > c . ; If point p make right triangle . ; All triangle between j and p are acute triangles . So add p - j - 1 in x . ; Increment y by 1. ; All triangle between q and p are acute triangles . So add q - p in z . ; If no right triangle ; All triangle between j and p are acute triangles . So add p - j in x . ; All triangle between q and p are acute triangles . So add q - p in z . ; Driver Code | def findTriangle ( a , n ) : NEW_LINE INDENT b = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT b . append ( a [ i ] * a [ i ] ) NEW_LINE DEDENT a . sort ( ) NEW_LINE b . sort ( ) NEW_LINE x , y , z = 0 , 0 , 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT p = i + 1 NEW_LINE q = i + 1 NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT while ( p < n - 1 and b [ i ] + b [ j ] >= b [ p + 1 ] ) : NEW_LINE INDENT p += 1 NEW_LINE DEDENT q = max ( q , p ) NEW_LINE while ( q < n - 1 and a [ i ] + a [ j ] > a [ q + 1 ] ) : NEW_LINE INDENT q += 1 NEW_LINE DEDENT if ( b [ i ] + b [ j ] == b [ p ] ) : NEW_LINE INDENT x += max ( p - j - 1 , 0 ) NEW_LINE y += 1 NEW_LINE z += q - p NEW_LINE DEDENT else : NEW_LINE INDENT x += max ( p - j , 0 ) NEW_LINE z += q - p NEW_LINE DEDENT DEDENT DEDENT print ( " Acute β Triangle : " , x ) NEW_LINE print ( " Right β Triangle : " , y ) NEW_LINE print ( " Obtuse β Triangle : " , z ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 9 , 10 , 12 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE findTriangle ( arr , n ) NEW_LINE DEDENT |
Check whether a point exists in circle sector or not . | Python3 program to check if a point lies inside a circle sector . ; calculate endAngle ; Calculate polar co - ordinates ; Check whether polarradius is less then radius of circle or not and Angle is between startAngle and endAngle or not ; Driver code | import math NEW_LINE def checkPoint ( radius , x , y , percent , startAngle ) : NEW_LINE INDENT endAngle = 360 / percent + startAngle NEW_LINE polarradius = math . sqrt ( x * x + y * y ) NEW_LINE Angle = math . atan ( y / x ) NEW_LINE if ( Angle >= startAngle and Angle <= endAngle and polarradius < radius ) : NEW_LINE INDENT print ( " Point β ( " , x , " , " , y , " ) β " " exist β in β the β circle β sector " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Point β ( " , x , " , " , y , " ) β " " does β not β exist β in β the β circle β sector " ) NEW_LINE DEDENT DEDENT radius , x , y = 8 , 3 , 4 NEW_LINE percent , startAngle = 12 , 0 NEW_LINE checkPoint ( radius , x , y , percent , startAngle ) NEW_LINE |
Area of a polygon with given n ordered vertices | ( X [ i ] , Y [ i ] ) are coordinates of i 'th point. ; Initialize area ; Calculate value of shoelace formula ; Return absolute value ; Driver program to test above function | def polygonArea ( X , Y , n ) : NEW_LINE INDENT area = 0.0 NEW_LINE j = n - 1 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT area += ( X [ j ] + X [ i ] ) * ( Y [ j ] - Y [ i ] ) NEW_LINE DEDENT return int ( abs ( area / 2.0 ) ) NEW_LINE DEDENT X = [ 0 , 2 , 4 ] NEW_LINE Y = [ 1 , 3 , 7 ] NEW_LINE n = len ( X ) NEW_LINE print ( polygonArea ( X , Y , n ) ) NEW_LINE |
Difference of count of distinct elements present to left and right for each array element | Function to find the difference of count of distince elements to the left and right for each array elements ; Stores distinct array element in the left and right ; Traverse the array ; Insert all element to the left in the set S1 ; Insert all element to the right in the set S2 ; Print the difference ; Driver Code | def findDifferenceArray ( arr , N ) : NEW_LINE INDENT S1 = set ( ) ; NEW_LINE S2 = set ( ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( i ) : NEW_LINE INDENT S1 . add ( arr [ j ] ) ; NEW_LINE DEDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT S2 . add ( arr [ j ] ) ; NEW_LINE DEDENT print ( abs ( len ( S1 ) - len ( S2 ) ) , end = ' β ' ) ; NEW_LINE S1 . clear ( ) ; NEW_LINE S2 . clear ( ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 7 , 7 , 3 , 2 , 3 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE findDifferenceArray ( arr , N ) ; NEW_LINE DEDENT |
Maximum subarray sum with same first and last element formed by removing elements | Python3 program for the above approach ; Function to find the maximum sum of sub - array ; Initialize the variables ; Traverse over the range ; Add the current value to the variable currsum for prefix sum ; Calculate the result ; Return the answer ; Driver Code | import sys NEW_LINE def maximumSubarraySum ( arr ) : NEW_LINE INDENT N = len ( arr ) ; NEW_LINE memo = { } ; NEW_LINE res = - ( sys . maxsize - 1 ) ; NEW_LINE currsum = 0 ; NEW_LINE currval = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT currval = arr [ i ] ; NEW_LINE currsum = currsum + currval ; NEW_LINE if currval in memo : NEW_LINE INDENT if ( currval > 0 ) : NEW_LINE INDENT res = max ( res , currsum - memo [ currval ] + currval ) ; NEW_LINE DEDENT else : NEW_LINE INDENT res = max ( res , currsum - memo [ currval ] + 2 * currval ) ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT memo [ currval ] = currsum ; NEW_LINE DEDENT if ( currval < 0 ) : NEW_LINE INDENT currsum = currsum - currval ; NEW_LINE DEDENT DEDENT return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ - 1 , - 3 , 4 , 0 , - 1 , - 2 ] ; NEW_LINE print ( maximumSubarraySum ( arr ) ) ; NEW_LINE DEDENT |
Find Nth root of a number using Bisection method | Function that returns the value of the function at a given value of x ; calculating the value of the differential of the function ; The function that returns the root of given number ; Defining range on which answer can be found ; finding mid value ; Driver code | def f ( x , p , num ) : NEW_LINE INDENT return pow ( x , p ) - num NEW_LINE DEDENT def f_prime ( x , p ) : NEW_LINE INDENT return p * pow ( x , p - 1 ) NEW_LINE DEDENT def root ( num , p ) : NEW_LINE INDENT left = - num NEW_LINE right = num NEW_LINE x = 0 NEW_LINE while ( True ) : NEW_LINE INDENT x = ( left + right ) / 2.0 NEW_LINE value = f ( x , p , num ) NEW_LINE prime = f_prime ( x , p ) NEW_LINE if ( value * prime <= 0 ) : NEW_LINE INDENT left = x NEW_LINE DEDENT else : NEW_LINE INDENT right = x NEW_LINE DEDENT if ( value < 0.000001 and value >= 0 ) : NEW_LINE INDENT return x NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT P = 1234321 NEW_LINE N = 2 NEW_LINE ans = root ( P , N ) NEW_LINE print ( ans ) NEW_LINE DEDENT |
Count of permutations of an Array having maximum MEXs sum of prefix arrays | To calculate the factorial ; To return the number of permutations of an array with maximum MEXs sum of prefix array ; Map to store the frequency of each element ; Running a loop from i = 0 to i < n ; If continuity breaks , then break the loop ; Considering choices available to be filled at this position , i . e . mp [ i ] ; Decrement the count of remaining right elements ; Adding all permutations of the elements present to the right of the point where continuity breaks . ; Driver Code | def factorial ( n ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT res *= i NEW_LINE DEDENT return res NEW_LINE DEDENT def countPermutations ( ar , n ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE ans = 1 NEW_LINE cnt = n NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ar [ i ] in mp ) : NEW_LINE INDENT mp [ ar [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ ar [ i ] ] = 1 NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( i not in mp ) : NEW_LINE INDENT break NEW_LINE DEDENT ans = ( ans * mp [ i ] ) NEW_LINE cnt -= 1 NEW_LINE DEDENT ans = ans * factorial ( cnt ) NEW_LINE return ans NEW_LINE DEDENT arr = [ 1 , 0 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countPermutations ( arr , N ) ) NEW_LINE |
Convert 0 to N by adding 1 or multiplying by 2 in minimum steps | Function to count number of set bits in N ; Stores the count of set bits ; If N is odd , then it a set bit ; Return the result ; Driver Code | def minimumAdditionOperation ( N ) : NEW_LINE INDENT count = 0 NEW_LINE while ( N ) : NEW_LINE INDENT if ( N & 1 == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT N = N >> 1 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 6 NEW_LINE print ( minimumAdditionOperation ( N ) ) NEW_LINE DEDENT |
Count of unordered pairs of semi | python program of the above approach ; Stores the count of distinct prime number in factor of current number ; Function to return the vector of semi prime numbers in range [ 1 , N ] ; Count of distinct prime number in the factor of current number using Sieve of Eratosthenes ; If current number is prime ; Stores the semi prime numbers ; If p has 2 distinct prime factors ; Return vector ; Function to count unordered pairs of semi prime numbers with prime sum ; Stores the final count ; Loop to iterate over al the l unordered pairs ; If sum of current semi prime numbers is a prime number ; Return answer ; Driver Code | maxn = 100000 NEW_LINE prime = [ 0 for _ in range ( maxn ) ] NEW_LINE def semiPrimes ( N ) : NEW_LINE INDENT for p in range ( 2 , maxn ) : NEW_LINE INDENT if ( prime [ p ] == 0 ) : NEW_LINE INDENT for i in range ( 2 * p , maxn , p ) : NEW_LINE INDENT prime [ i ] += 1 NEW_LINE DEDENT DEDENT DEDENT sPrimes = [ ] NEW_LINE for p in range ( 2 , N + 1 ) : NEW_LINE INDENT if ( prime [ p ] == 2 ) : NEW_LINE INDENT sPrimes . append ( p ) NEW_LINE DEDENT DEDENT return sPrimes NEW_LINE DEDENT def countPairs ( semiPrimes ) : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( 0 , len ( semiPrimes ) ) : NEW_LINE INDENT for j in range ( i + 1 , len ( semiPrimes ) ) : NEW_LINE INDENT if ( prime [ semiPrimes [ i ] + semiPrimes [ j ] ] == 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 100 NEW_LINE print ( countPairs ( semiPrimes ( N ) ) ) NEW_LINE DEDENT |
Minimum divide by 2 operations required to make GCD odd for given Array | python program for the above approac ; Function to find the minimum number of operations to make the GCD of the array odd ; Stores the minimum operations required ; Stores the powers of two for the current array element ; Dividing by 2 ; Increment the count ; Update the minimum operation required ; Return the result required ; Driver Code | INT_MAX = 2147483647 NEW_LINE def minimumOperations ( arr , N ) : NEW_LINE INDENT mini = INT_MAX NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT count = 0 NEW_LINE while ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT arr [ i ] = arr [ i ] // 2 NEW_LINE count += 1 NEW_LINE DEDENT if ( mini > count ) : NEW_LINE INDENT mini = count NEW_LINE DEDENT DEDENT return mini NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE print ( minimumOperations ( arr , N ) ) NEW_LINE DEDENT |
Minimum size of the array with MEX as A and XOR of the array elements as B | Function to find the minimum size of array with given MEX and XOR ; Find the XOR of values from 0 to A - 1 ; If A is a multiple of 4 ; If A % 4 gives remainder 1 ; If A % 4 gives remainder 2 ; Initializing array size by A ; If XOR of all values of array is equal to B ; If the required integer to be added is equal to A ; Else any integer can be added ; Driver Code | def minimumSizeArr ( A , B ) : NEW_LINE INDENT currXor = 0 NEW_LINE reminder = ( A - 1 ) % 4 NEW_LINE if ( reminder == 0 ) : NEW_LINE INDENT currXor = A - 1 NEW_LINE DEDENT elif ( reminder == 1 ) : NEW_LINE INDENT currXor = 1 NEW_LINE DEDENT elif ( reminder == 2 ) : NEW_LINE INDENT currXor = A NEW_LINE DEDENT minSize = A NEW_LINE if ( currXor == B ) : NEW_LINE INDENT return minSize NEW_LINE DEDENT elif ( currXor ^ B == A ) : NEW_LINE INDENT return minSize + 2 NEW_LINE DEDENT else : NEW_LINE INDENT return minSize + 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = 1 NEW_LINE B = 999 NEW_LINE print ( minimumSizeArr ( A , B ) ) NEW_LINE DEDENT |
Count of triplets till N whose product is at most N | Function to find number of triplets ( A , B , C ) having A * B * C <= N ; Stores the count of triplets ; Iterate a loop fixing the value of A ; Iterate a loop fixing the value of A ; Find the total count of triplets and add it to cnt ; Return the total triplets formed ; Driver Code | def countTriplets ( N ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE for A in range ( 1 , N + 1 ) : NEW_LINE INDENT for B in range ( 1 , N // A + 1 ) : NEW_LINE INDENT cnt += N // ( A * B ) ; NEW_LINE DEDENT DEDENT return cnt ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2 ; NEW_LINE print ( countTriplets ( N ) ) ; NEW_LINE DEDENT |
Largest integer upto N having greatest prime factor greater than its square root | python program for the above approach ; Stores the Greatest Prime Factor ; Modified Sieve to find the Greatest Prime Factor of all integers in the range [ 1 , maxn ] ; Initialize the array with 0 ; Iterate through all values of i ; If i is not a prime number ; Update the multiples of i ; Function to find integer in the range [ 1 , N ] such that its Greatest Prime factor is greater than its square root ; Iterate through all values of i in the range [ N , 1 ] ; If greatest prime factor of i is greater than its sqare root ; Return answer ; If no valid integer exist ; Driver Code | import math NEW_LINE maxn = 100001 NEW_LINE gpf = [ 0 for _ in range ( maxn ) ] NEW_LINE def modifiedSieve ( ) : NEW_LINE INDENT gpf [ 0 ] = 0 NEW_LINE gpf [ 1 ] = 1 NEW_LINE for i in range ( 2 , maxn ) : NEW_LINE INDENT if ( gpf [ i ] > 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT for j in range ( i , maxn , i ) : NEW_LINE INDENT gpf [ j ] = max ( i , gpf [ j ] ) NEW_LINE DEDENT DEDENT DEDENT def greatestValidInt ( N ) : NEW_LINE INDENT modifiedSieve ( ) NEW_LINE for i in range ( N , 0 , - 1 ) : NEW_LINE INDENT if ( gpf [ i ] > math . sqrt ( i ) ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 25 NEW_LINE print ( greatestValidInt ( N ) ) NEW_LINE DEDENT |
Find subfactorial of a number | Function to find the subfactorial of the number ; Initialize variables ; Iterating over range N ; Fact variable store factorial of the i ; If count is even ; Increase the value of count by 1 ; Driver Code | def subfactorial ( N ) : NEW_LINE INDENT res = 0 NEW_LINE fact = 1 NEW_LINE count = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT fact = fact * i NEW_LINE if ( count % 2 == 0 ) : NEW_LINE INDENT res = res - ( 1 / fact ) NEW_LINE DEDENT else : NEW_LINE INDENT res = res + ( 1 / fact ) NEW_LINE DEDENT count += 1 NEW_LINE DEDENT return fact * ( 1 + res ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE print ( subfactorial ( N ) ) NEW_LINE DEDENT |
Minimize operations to delete all elements of permutation A by removing a subsequence having order as array B | Function to find the minimum number of operations to delete all elements of permutation A in order described by B ; Stores the count of operations ; Stores the index of current integer in B to be deleted ; Loop to iterate over all values of B ; Stores the current index in A ; Iterate over all values A ; If current integer of B and A equal , increment the index of the current integer of B ; As the permutation A has been traversed completely , increment the count of operations by 1 ; Return Answer ; Driver Code | def minOperations ( A , B , N ) : NEW_LINE INDENT cnt = 0 NEW_LINE i = 0 NEW_LINE while ( i < N ) : NEW_LINE INDENT j = 0 NEW_LINE while ( j < N and i < N ) : NEW_LINE INDENT if ( B [ i ] == A [ j ] ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT j += 1 NEW_LINE DEDENT cnt += 1 NEW_LINE DEDENT return cnt NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 2 , 4 , 6 , 1 , 5 , 3 ] NEW_LINE B = [ 6 , 5 , 4 , 2 , 3 , 1 ] NEW_LINE N = len ( A ) NEW_LINE print ( minOperations ( A , B , N ) ) NEW_LINE DEDENT |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.