codeparrot/xlcost-text-to-code huggingface dataset:
Travelling Salesman Problem implementation using BackTracking | Javascript implementation of the approach ; Function to find the minimum weight Hamiltonian Cycle ; If last node is reached and it has a link to the starting node i . e the source then keep the minimum value out of the total cost of traversal and " ans " Finally return to check for more possible values ; BACKTRACKING STEP Loop to traverse the adjacency list of currPos node and increasing the count by 1 and cost by graph [ currPos ] [ i ] value ; Mark as visited ; Mark ith node as unvisited ; n is the number of nodes i . e . V ; Boolean array to check if a node has been visited or not ; Mark 0 th node as visited ; Find the minimum weight Hamiltonian Cycle ; ans is the minimum weight Hamiltonian Cycle
var V = 4 ; var ans = 1000000000 ; function tsp ( graph , currPos , n , count , cost ) { if ( count == n && graph [ currPos ] [ 0 ] ) { ans = Math . min ( ans , cost + graph [ currPos ] [ 0 ] ) ; return ; } for ( var i = 0 ; i < n ; i ++ ) { if ( ! v [ i ] && graph [ currPos ] [ i ] ) { v [ i ] = true ; tsp ( graph , i , n , count + 1 , cost + graph [ currPos ] [ i ] ) ; v [ i ] = false ; } } } ; var n = 4 ; var graph = [ [ 0 , 10 , 15 , 20 ] , [ 10 , 0 , 35 , 25 ] , [ 15 , 35 , 0 , 30 ] , [ 20 , 25 , 30 , 0 ] ] ; var v = Array ( n ) . fill ( false ) ; v [ 0 ] = true ; tsp ( graph , 0 , n , 1 , 0 ) ; document . write ( ans ) ;
Generate all the binary strings of N bits | Function to print the output ; Function to generate all binary strings ; First assign "0" at ith position and try for all other permutations for remaining positions ; And then assign "1" at ith position and try for all other permutations for remaining positions ; Driver Code ; Print all binary strings
function printTheArray ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; } document . write ( " " ) ; } function generateAllBinaryStrings ( n , arr , i ) { if ( i == n ) { printTheArray ( arr , n ) ; return ; } arr [ i ] = 0 ; generateAllBinaryStrings ( n , arr , i + 1 ) ; arr [ i ] = 1 ; generateAllBinaryStrings ( n , arr , i + 1 ) ; } let n = 4 ; let arr = new Array ( n ) ; arr . fill ( 0 ) ; generateAllBinaryStrings ( n , arr , 0 ) ;
Print all the combinations of a string in lexicographical order | function to print string ; Method to found all combination of string it is based in tree ; return if level is equal size of string ; if occurrence of char is 0 then skip the iteration of loop ; decrease the char occurrence by 1 ; store the char in result ; print the string till level ; call the function from level + 1 ; backtracking ; declare the map for store each char with occurrence ; initialize the input array with all unique char ; initialize the count array with occurrence the unique char ; temporary char array for store the result ; store the element of input array ; store the element of count array ; size of map ( no of unique char ) ; size of original string ; call function for print string combination ; Driver code
function printResult ( result , len ) { for ( var i = 0 ; i <= len ; i ++ ) document . write ( result [ i ] ) ; document . write ( " " ) ; } function stringCombination ( result , str , count , level , size , len ) { if ( level === size ) return ; for ( var i = 0 ; i < len ; i ++ ) { if ( count [ i ] === 0 ) continue ; count [ i ] -- ; result [ level ] = str [ i ] ; printResult ( result , level ) ; stringCombination ( result , str , count , level + 1 , size , len ) ; count [ i ] ++ ; } } function combination ( str ) { var i ; var mp = { } ; for ( i = 0 ; i < str . length ; i ++ ) if ( mp . hasOwnProperty ( str [ i ] ) ) mp [ str [ i ] ] = mp [ str [ i ] ] + 1 ; else mp [ str [ i ] ] = 1 ; var input = new Array ( Object . keys ( mp ) . length ) . fill ( 0 ) ; var count = new Array ( Object . keys ( mp ) . length ) . fill ( 0 ) ; var result = new Array ( str . length ) . fill ( 0 ) ; i = 0 ; for ( const [ key , value ] of Object . entries ( mp ) ) { input [ i ] = key ; count [ i ] = value ; i ++ ; } var len = Object . keys ( mp ) . length ; var size = str . length ; stringCombination ( result , input , count , 0 , size , len ) ; } var str = " " ; combination ( str ) ;
Fill two instances of all numbers from 1 to n in a specific way | A recursive utility function to fill two instances of numbers from 1 to n in res [ 0. .2 n - 1 ] . ' curr ' is current value of n . ; If current number becomes 0 , then all numbers are filled ; Try placing two instances of ' curr ' at all possible locations till solution is found ; Two ' curr ' should be placed at ' curr + 1' distance ; Plave two instances of ' curr ' ; Recur to check if the above placement leads to a solution ; If solution is not possible , then backtrack ; This function prints the result for input number ' n ' using fillUtil ( ) ; Create an array of size 2 n and initialize all elements in it as 0 ; If solution is possible , then print it . ; Driver Code
function fillUtil ( res , curr , n ) { if ( curr == 0 ) return true ; let i ; for ( i = 0 ; i < 2 * n - curr - 1 ; i ++ ) { if ( res [ i ] == 0 && res [ i + curr + 1 ] == 0 ) { res [ i ] = res [ i + curr + 1 ] = curr ; if ( fillUtil ( res , curr - 1 , n ) ) return true ; res [ i ] = res [ i + curr + 1 ] = 0 ; } } return false ; } function fill ( n ) { let res = new Array ( 2 * n ) ; let i ; for ( i = 0 ; i < ( 2 * n ) ; i ++ ) res [ i ] = 0 ; if ( fillUtil ( res , n , n ) ) { for ( i = 0 ; i < 2 * n ; i ++ ) document . write ( res [ i ] + " " ) ; } else document . write ( " " ) ; } fill ( 7 ) ;
m Coloring Problem | Backtracking | Javascript program for the above approach ; A node class which stores the color and the edges connected to the node ; Create a visited array of n nodes , initialized to zero ; maxColors used till now are 1 as all nodes are painted color 1 ; Do a full BFS traversal from all unvisited starting points ; If the starting point is unvisited , mark it visited and push it in queue ; BFS Travel starts here ; Checking all adjacent nodes to " top " edge in our queue ; IMPORTANT : If the color of the adjacent node is same , increase it by 1 ; If number of colors used shoots m , return 0 ; If the adjacent node is not visited , mark it visited and push it in queue ; Driver code ; Number of colors ; Create a vector of n + 1 nodes of type " node " The zeroth position is just dummy ( 1 to n to be used ) ; Push edges to each node as per given input ; Connect the undirected graph ; Display final answer
class node { constructor ( ) { this . color = 1 ; this . edges = new Set ( ) ; } } ; function canPaint ( nodes , n , m ) { var visited = [ ] ; for ( var i = 0 ; i < n + 1 ; i ++ ) { visited . push ( 0 ) ; } var maxColors = 1 ; for ( var sv = 1 ; sv <= n ; sv ++ ) { if ( visited [ sv ] > 0 ) continue ; visited [ sv ] = 1 ; var q = [ ] ; q . push ( sv ) ; while ( q . length != 0 ) { var top = q [ 0 ] ; q . shift ( ) ; for ( var it of nodes [ top ] . edges ) { if ( nodes [ top ] . color == nodes [ it ] . color ) nodes [ it ] . color += 1 ; maxColors = Math . max ( maxColors , Math . max ( nodes [ top ] . color , nodes [ it ] . color ) ) ; if ( maxColors > m ) return 0 ; if ( visited [ it ] == 0 ) { visited [ it ] = 1 ; q . push ( it ) ; } } } } return 1 ; } var n = 4 ; var graph = [ [ 0 , 1 , 1 , 1 ] , [ 1 , 0 , 1 , 0 ] , [ 1 , 1 , 0 , 1 ] , [ 1 , 0 , 1 , 0 ] ] ; var m = 3 ; var nodes = [ ] ; for ( var i = 0 ; i < n + 1 ; i ++ ) { nodes . push ( new node ( ) ) ; } for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < n ; j ++ ) { if ( graph [ i , j ] > 0 ) { nodes [ i ] . edges . push ( i ) ; nodes [ j ] . edges . push ( j ) ; } } } document . write ( canPaint ( nodes , n , m ) ) ;
Fast Doubling method to find the Nth Fibonacci number | Javascript program to find the Nth Fibonacci number using Fast Doubling Method ; Function calculate the N - th fibanacci number using fast doubling method ; Base Condition ; Here a = F ( n ) ; Here b = F ( n + 1 ) ; As F ( 2 n ) = F ( n ) [ 2F ( n + 1 ) F ( n ) ] Here c = F ( 2 n ) ; As F ( 2 n + 1 ) = F ( n ) ^ 2 + F ( n + 1 ) ^ 2 Here d = F ( 2 n + 1 ) ; Check if N is odd or even ; Driver code
let a , b , c , d ; let MOD = 1000000007 ; function FastDoubling ( n , res ) { if ( n == 0 ) { res [ 0 ] = 0 ; res [ 1 ] = 1 ; return ; } FastDoubling ( parseInt ( n / 2 , 10 ) , res ) ; a = res [ 0 ] ; b = res [ 1 ] ; c = 2 * b - a ; if ( c < 0 ) c += MOD ; c = ( a * c ) % MOD ; d = ( a * a + b * b ) % MOD ; if ( n % 2 == 0 ) { res [ 0 ] = c ; res [ 1 ] = d ; } else { res [ 0 ] = d ; res [ 1 ] = c + d ; } } let N = 6 ; let res = new Array ( 2 ) ; res . fill ( 0 ) ; FastDoubling ( N , res ) ; document . write ( res [ 0 ] ) ;
Frequency of an integer in the given array using Divide and Conquer | Function to return the frequency of x in the subarray arr [ low ... high ] ; If the subarray is invalid or the element is not found ; If there 's only a single element  which is equal to x ; Divide the array into two parts and then find the count of occurrences of x in both the parts ; Driver code
function count ( arr , low , high , x ) { if ( ( low > high ) || ( low == high && arr [ low ] != x ) ) return 0 ; if ( low == high && arr [ low ] == x ) return 1 ; return count ( arr , low , Math . floor ( ( low + high ) / 2 ) , x ) + count ( arr , 1 + Math . floor ( ( low + high ) / 2 ) , high , x ) ; } let arr = [ 30 , 1 , 42 , 5 , 56 , 3 , 56 , 9 ] ; let n = arr . length ; let x = 56 ; document . write ( count ( arr , 0 , n - 1 , x ) ) ;
Largest number N which can be reduced to 0 in K steps | Utility function to return the first digit of a number . ; Remove last digit from number till only one digit is left ; return the first digit ; Utility function that returns the count of numbers written down when starting from n ; Function to find the largest number N which can be reduced to 0 in K steps ; Get the sequence length of the mid point ; Until k sequence length is reached ; Update mid point ; Get count of the new mid point ; Update right to mid ; Update left to mid ; Increment mid point by one while count is equal to k to get the maximum value of mid point ; Driver Code
function firstDigit ( n ) { while ( n >= 10 ) { n = parseInt ( n / 10 , 10 ) ; } return n ; } function getCount ( n ) { let count = 1 ; while ( n != 0 ) { let leadDigit = firstDigit ( n ) ; n -= leadDigit ; count ++ ; } return count ; } function getLargestNumber ( k ) { let left = k ; let right = k * 10 ; let mid = parseInt ( ( left + right ) / 2 , 10 ) ; let len = getCount ( mid ) ; while ( len != k ) { mid = parseInt ( ( left + right ) / 2 , 10 ) ; len = getCount ( mid ) ; if ( len > k ) { right = mid ; } else { left = mid ; } } while ( len == k ) { if ( len != getCount ( mid + 1 ) ) { break ; } mid ++ ; } return ( mid ) ; } let k = 3 ; document . write ( getLargestNumber ( k ) ) ;
Check if point ( X , Y ) can be reached from origin ( 0 , 0 ) with jump of 1 and N perpendicularly simultaneously | Function to check if ( X , Y ) is reachable from ( 0 , 0 ) using the jumps of given type ; Case where source & destination are the same ; Check for even N ( X , Y ) is reachable or not ; If N is odd and parity of X and Y is different return , no valid sequence of jumps exist ; Driver Code
string checkReachability ( int N , int X , int Y ) { if ( X == 0 && Y == 0 ) { return " " ; } if ( N % 2 == 0 ) { return " " ; } else { if ( X % 2 != Y % 2 ) { return " " ; } else { return " " ; } } } let N = 2 , X = 5 , Y = 4 ; document . write ( checkReachability ( N , X , Y ) ) ;
Find three vertices in an N | Function to find three vertices that subtends an angle closest to A ; Stores the closest angle to A ; Stores the count of edge which subtend an angle of A ; Iterate in the range [ 1 , N - 2 ] ; Stores the angle subtended ; If absolute ( angle - A ) is less than absolute ( mi - A ) ; Update angle to mi , and also update i to ans ; Print the vertices ; Driver Code
function closestsAngle ( N , A ) { let mi = Number . MAX_VALUE ; let ans = 0 ; for ( let i = 1 ; i < N - 1 ; i ++ ) { let angle = 180.0 * i / N ; if ( Math . abs ( angle - A ) < Math . abs ( mi - A ) ) { mi = angle ; ans = i ; } } document . write ( 2 + ' ' + 1 + ' ' + parseInt ( 2 + ans ) ) ; } let N = 3 , A = 15 ; closestsAngle ( N , A ) ;
Area of a triangle with two vertices at midpoints of opposite sides of a square and the other vertex lying on vertex of a square | Function to find the area of the triangle that inscribed in square ; Stores the length of the first side of triangle ; Stores the length of the second side of triangle ; Stores the length of the third side of triangle ; Stores the area of the triangle ; Return the resultant area ; Driver code
function areaOftriangle ( side ) { let a = Math . sqrt ( Math . pow ( side / 2 , 2 ) + Math . pow ( side / 2 , 2 ) ) ; let b = Math . sqrt ( Math . pow ( side , 2 ) + Math . pow ( side / 2 , 2 ) ) ; let c = Math . sqrt ( Math . pow ( side , 2 ) + Math . pow ( side / 2 , 2 ) ) ; let s = ( a + b + c ) / 2 ; let area = Math . sqrt ( s * ( s - a ) * ( s - b ) * ( s - c ) ) ; return area . toFixed ( 1 ) ; } let N = 10 ; document . write ( areaOftriangle ( N ) ) ;
Equation of a straight line with perpendicular distance D from origin and an angle A between the perpendicular from origin and x | Function to find equation of a line whose distance from origin and angle made by the perpendicular from origin with x - axis is given ; Convert angle from degree to radian ; Handle the special case ; Calculate the sin and cos of angle ; Print the equation of the line ; Given Input ; Function Call
function findLine ( distance , degree ) { let x = degree * 3.14159 / 180 ; if ( degree > 90 ) { document . write ( " " ) ; return ; } let result_1 = Math . sin ( x ) ; let result_2 = Math . cos ( x ) ; document . write ( result_2 . toPrecision ( 2 ) + " " + result_1 . toPrecision ( 2 ) + " " + distance ) ; } let D = 10 ; let A = 30 ; findLine ( D , A ) ;
Count number of coordinates from an array satisfying the given conditions | Function to count the number of coordinates from a given set that satisfies the given conditions ; Stores the count of central points ; Store the count of each x and y coordinates ; Find all possible pairs ; Initialize variables c1 , c2 , c3 , c4 to define the status of conditions ; Stores value of each point ; Check the conditions for each point by generating all possible pairs ; If arr [ j ] [ 0 ] > x and arr [ j ] [ 1 ] == y ; If arr [ j ] [ 0 ] < x and arr [ j ] [ 1 ] = = y ; If arr [ j ] [ 1 ] > y and arr [ j ] [ 0 ] == x ; If arr [ j ] [ 1 ] < y and arr [ j ] [ 0 ] = = x ; If all conditions satisfy then point is central point ; Increment the count by 1 ; Return the count ; Driver Code
function centralPoints ( arr , N ) { let count = 0 ; let c1 , c2 , c3 , c4 ; for ( let i = 0 ; i < N ; i ++ ) { c1 = 0 ; c2 = 0 ; c3 = 0 ; c4 = 0 ; let x = arr [ i ] [ 0 ] ; let y = arr [ i ] [ 1 ] ; for ( let j = 0 ; j < N ; j ++ ) { if ( arr [ j ] [ 0 ] > x && arr [ j ] [ 1 ] == y ) { c1 = 1 ; } if ( arr [ j ] [ 1 ] > y && arr [ j ] [ 0 ] == x ) { c2 = 1 ; } if ( arr [ j ] [ 0 ] < x && arr [ j ] [ 1 ] == y ) { c3 = 1 ; } if ( arr [ j ] [ 1 ] < y && arr [ j ] [ 0 ] == x ) { c4 = 1 ; } } if ( c1 + c2 + c3 + c4 == 4 ) { count ++ ; } } return count ; } let arr = [ [ 1 , 0 ] , [ 2 , 0 ] , [ 1 , 1 ] , [ 1 , - 1 ] ] ; let N = arr . length ; document . write ( centralPoints ( arr , N ) ) ;
Displacement from origin after N moves of given distances in specified directions | Function to find the displacement from the origin and direction after performing the given set of moves ; Stores the distances travelled in the directions North , South , East , and West respectively ; Store the initial position of robot ; Traverse the array B [ ] ; If the current direction is North ; If the current direction is South ; If the current direction is East ; If the current direction is West ; Stores the total vertical displacement ; Stores the total horizontal displacement ; Find the displacement ; Print the displacement and direction after N moves ; Driver Code
function finalPosition ( a , b , M ) { let n = 0 , s = 0 , e = 0 , w = 0 ; let p = ' ' ; for ( let i = 0 ; i < M ; i ++ ) { if ( p == ' ' ) { if ( a [ i ] == ' ' ) { p = ' ' ; n = n + b [ i ] ; } else if ( a [ i ] == ' ' ) { p = ' ' ; s = s + b [ i ] ; } else if ( a [ i ] == ' ' ) { p = ' ' ; e = e + b [ i ] ; } else if ( a [ i ] == ' ' ) { p = ' ' ; w = w + b [ i ] ; } } else if ( p == ' ' ) { if ( a [ i ] == ' ' ) { p = ' ' ; s = s + b [ i ] ; } else if ( a [ i ] == ' ' ) { p = ' ' ; n = n + b [ i ] ; } else if ( a [ i ] == ' ' ) { p = ' ' ; w = w + b [ i ] ; } else if ( a [ i ] == ' ' ) { p = ' ' ; e = e + b [ i ] ; } } else if ( p == ' ' ) { if ( a [ i ] == ' ' ) { p = ' ' ; e = e + b [ i ] ; } else if ( a [ i ] == ' ' ) { p = ' ' ; w = w + b [ i ] ; } else if ( a [ i ] == ' ' ) { p = ' ' ; s = s + b [ i ] ; } else if ( a [ i ] == ' ' ) { p = ' ' ; n = n + b [ i ] ; } } else if ( p == ' ' ) { if ( a [ i ] == ' ' ) { p = ' ' ; w = w + b [ i ] ; } else if ( a [ i ] == ' ' ) { p = ' ' ; e = e + b [ i ] ; } else if ( a [ i ] == ' ' ) { p = ' ' ; n = n + b [ i ] ; } else if ( a [ i ] == ' ' ) { p = ' ' ; s = s + b [ i ] ; } } } let ver_disp = n - s ; let hor_disp = e - w ; let displacement = Math . ceil ( Math . sqrt ( ( ver_disp * ver_disp ) + ( hor_disp * hor_disp ) ) ) ; document . write ( displacement + " " + p ) ; } let A = [ ' ' , ' ' , ' ' , ' ' , ' ' ] ; let B = [ 1 , 1 , 1 , 1 , 0 ] ; let N = 1 ; finalPosition ( A , B , N ) ;
Program to find Length of Latus Rectum of an Ellipse | Function to calculate the length of the latus rectum of an ellipse ; Length of major axis ; Length of minor axis ; Length of the latus rectum ; Given lengths of semi - major and semi - minor axis ; Function call to calculate length of the latus rectum of a ellipse
function lengthOfLatusRectum ( A , B ) { var major = 2.0 * A ; var minor = 2.0 * B ; var latus_rectum = ( minor * minor ) / major ; return latus_rectum ; } var A = 3.0 , B = 2.0 ; document . write ( lengthOfLatusRectum ( A , B ) ) ;
Program to find slant height of cone and pyramid | Function to calculate slant height of a cone ; Store the slant height of cone ; Print the result ; Function to find the slant height of a pyramid ; Store the slant height of pyramid ; Print the result ; Dimensions of Cone ; Function Call for slant height of Cone ; Dimensions of Pyramid ; Function to calculate slant height of a pyramid
function coneSlantHeight ( cone_h , cone_r ) { var slant_height_cone = Math . sqrt ( Math . pow ( cone_h , 2 ) + Math . pow ( cone_r , 2 ) ) ; document . write ( " " + slant_height_cone + " " ) ; } function pyramidSlantHeight ( pyramid_h , pyramid_s ) { var slant_height_pyramid = Math . sqrt ( Math . pow ( pyramid_s / 2 , 2 ) + Math . pow ( pyramid_h , 2 ) ) ; document . write ( " " + slant_height_pyramid . toFixed ( 5 ) ) ; } var H1 = 4.5 , R = 6 ; coneSlantHeight ( H1 , R ) ; var H2 = 4 , S = 4.8 ; pyramidSlantHeight ( H2 , S ) ;
Program to find the length of Latus Rectum of a Parabola | Function to calculate distance between two points ; Calculating distance ; Function to calculate length of the latus rectum of a parabola ; Stores the co - ordinates of the vertex of the parabola ; Stores the co - ordinates of the focus of parabola ; Print the distance between focus and vertex ; Given a , b & c ; Function call
function distance ( x1 , y1 , x2 , y2 ) { return Math . sqrt ( ( x2 - x1 ) * ( x2 - x1 ) + ( y2 - y1 ) * ( y2 - y1 ) ) ; } function lengthOfLatusRectum ( a , b , c ) { let vertex = [ ( - b / ( 2 * a ) ) , ( ( ( 4 * a * c ) - ( b * b ) ) / ( 4 * a ) ) ] ; let focus = [ ( - b / ( 2 * a ) ) , ( ( ( 4 * a * c ) - ( b * b ) + 1 ) / ( 4 * a ) ) ] ; document . write ( ( 4 * distance ( focus [ 0 ] , focus [ 1 ] , vertex [ 0 ] , vertex [ 1 ] ) ) . toFixed ( 6 ) ) ; } let a = 3 , b = 5 , c = 1 ; lengthOfLatusRectum ( a , b , c ) ;
Program to convert polar co | Function to convert degree to radian ; Function to convert the polar coordinate to cartesian ; Convert degerees to radian ; Applying the formula : x = rcos ( theata ) , y = rsin ( theta ) ; Print cartesian coordinates ; Given polar coordinates ; Function to convert polar coordinates to equivalent cartesian coordinates
function ConvertDegToRad ( degree ) { let pi = 3.14159 ; return ( degree * ( pi / 180.0 ) ) ; } function ConvertToCartesian ( polar ) { polar [ 1 ] = ConvertDegToRad ( polar [ 1 ] ) ; let cartesian = [ polar [ 0 ] * Math . cos ( polar [ 1 ] ) , polar [ 0 ] * Math . sin ( polar [ 1 ] ) ] ; document . write ( ( cartesian [ 0 ] ) . toFixed ( 3 ) + " " + ( cartesian [ 1 ] ) . toFixed ( 3 ) ) ; } let polar = [ 1.4142 , 45 ] ; ConvertToCartesian ( polar ) ;
Distance between orthocenter and circumcenter of a right | Function to calculate Euclidean distance between the points p1 and p2 ; Stores x coordinates of both points ; Stores y coordinates of both points ; Return the Euclid distance using distance formula ; Function to find orthocenter of the right angled triangle ; Find the length of the three sides ; Orthocenter will be the vertex opposite to the largest side ; Function to find the circumcenter of right angle triangle ; Circumcenter will be located at center of hypotenuse ; If AB is the hypotenuse ; If BC is the hypotenuse ; If AC is the hypotenuse ; Function to find distance between orthocenter and circumcenter ; Find circumcenter ; Find orthocenter ; Find the distance between the orthocenter and circumcenter ; Print distance between orthocenter and circumcenter ; Driver Code ; Given coordinates A , B , and C ; Function Call
function distance ( p1 , p2 ) { var x1 = p1 [ 0 ] , x2 = p2 [ 0 ] ; var y1 = p1 [ 1 ] , y2 = p2 [ 1 ] ; return Math . sqrt ( Math . pow ( x2 - x1 , 2 ) + Math . pow ( y2 - y1 , 2 ) * 1.0 ) ; } function find_orthocenter ( A , B , C ) { var AB = distance ( A , B ) ; var BC = distance ( B , C ) ; var CA = distance ( C , A ) ; if ( AB > BC && AB > CA ) return C ; if ( BC > AB && BC > CA ) return A ; return B ; } function find_circumcenter ( A , B , C ) { var AB = distance ( A , B ) ; var BC = distance ( B , C ) ; var CA = distance ( C , A ) ; if ( AB > BC && AB > CA ) return [ Math . floor ( ( A [ 0 ] + B [ 0 ] ) / 2 ) , Math . floor ( ( A [ 1 ] + B [ 1 ] ) / 2 ) ] ; if ( BC > AB && BC > CA ) return [ Math . floor ( ( B [ 0 ] + C [ 0 ] ) / 2 ) , Math . floor ( ( B [ 1 ] + C [ 1 ] ) / 2 ) ] ; return [ Math . floor ( ( C [ 0 ] + A [ 0 ] ) / 2 ) , Math . floor ( ( C [ 1 ] + A [ 1 ] ) / 2 ) ] ; } function findDistance ( A , B , C ) { circumcenter = find_circumcenter ( A , B , C ) ; orthocenter = find_orthocenter ( A , B , C ) ; var distance_between = distance ( circumcenter , orthocenter ) ; document . write ( distance_between + " " ) ; } var A , B , C ; A = [ 0 , 0 ] ; B = [ 6 , 0 ] ; C = [ 0 , 8 ] ; findDistance ( A , B , C ) ;
Count triangles required to form a House of Cards of height N | Function to find the number of triangles ; Driver Code ; Function call
function noOfTriangles ( n ) { return Math . floor ( n * ( n + 2 ) * ( 2 * n + 1 ) / 8 ) ; } var n = 3 ; document . write ( noOfTriangles ( n ) ) ;
Check if N contains all digits as K in base B | Function to print the number of digits ; Calculate log using base change property and then take its floor and then add 1 ; Return the output ; Function that returns true if n contains all one 's in base b ; Calculate the sum ; Given number N ; Given base B ; Given digit K ; Function call
function findNumberOfDigits ( n , base ) { var dig = ( Math . floor ( Math . log ( n ) / Math . log ( base ) ) + 1 ) ; return ( dig ) ; } function isAllKs ( n , b , k ) { var len = findNumberOfDigits ( n , b ) ; var sum = k * ( 1 - Math . pow ( b , len ) ) / ( 1 - b ) ; if ( sum == n ) { return ( sum ) ; } } var N = 13 ; var B = 3 ; var K = 1 ; if ( isAllKs ( N , B , K ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }