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 curre...
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 ...
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 ri...
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_i...
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 t...
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 re...
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 __...
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...
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 ▁ perimet...
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 pr...
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 ▁ = " ...
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 pri...
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 ...
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 ) ) NE...
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 , " . " ) ; N...
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 ▁ Peri...
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 DEDE...
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 els...
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 exis...
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...
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 squa...
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 integ...
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_LIN...
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 DE...
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 ,...
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 ...
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 ; F...
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...
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 ) ...
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 t...
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_...
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 INDEN...
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 ( tria...
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 prin...
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_L...
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 :...
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 ] NE...
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 (...
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 -...
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 ...
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 __na...
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 *...
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_di...
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 " ...
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...
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 ▁ ...
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...
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 ; recu...
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 DEDE...
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 + r...
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 , " , ▁ " ,...
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...
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 ] == ...
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 ( pythagor...
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 ; Pri...
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...
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 ( " %...
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 ZE...
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_LIN...
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 ...
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 , ...
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 DEDE...
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...
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 DED...
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 co...
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 ; Findi...
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...
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 ...
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 ) NE...
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 S...
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...
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...
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 cur...
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 ) /...
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 ...
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...
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 (...
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 curre...
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...
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 ; Incremen...
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...
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 value...
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 =...
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 form...
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 ( countTrip...
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 ...
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 ) : N...
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 +=...
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 ; Lo...
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 ...