codeparrot/xlcost-text-to-code huggingface dataset:
Highest and Smallest power of K less than and greater than equal to N respectively | Function to return the highest power of k less than or equal to n ; Function to return the smallest power of k greater than or equal to n ; Function to print the result ; Driver code
function prevPowerofK ( n , k ) { var p = parseInt ( Math . log ( n ) / Math . log ( k ) ) ; return parseInt ( Math . pow ( k , p ) ) ; } function nextPowerOfK ( n , k ) { return prevPowerofK ( n , k ) * k ; } function printResult ( n , k ) { document . write ( prevPowerofK ( n , k ) + " " + nextPowerOfK ( n , k ) + " " ) ; } var n = 25 , k = 3 ; printResult ( n , k ) ;
Check if all elements of the given array can be made 0 by decrementing value in pairs | Function to check if all the elements can be made 0 in an array ; Variable to store sum and maximum element in an array ; Loop to calculate the sum and max value of the given array ; If n is 1 or sum is odd or sum - max element < max then no solution ; For the remaining case , print Yes ; Driver code
function canMake ( n , ar ) { var sum = 0 , maxx = - 1 ; for ( var i = 0 ; i < n ; i ++ ) { sum += ar [ i ] ; maxx = Math . max ( maxx , ar [ i ] ) ; } if ( n == 1 sum % 2 == 1 sum - maxx < maxx ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var n = 6 ; var arr = [ 1 , 1 , 2 , 3 , 6 , 11 ] ; canMake ( n , arr ) ;
Sum of all Perfect Squares lying in the range [ L , R ] for Q queries | Array to precompute the sum of squares from 1 to 100010 so that for every query , the answer can be returned in O ( 1 ) . ; Function to check if a number is a perfect square or not ; Find floating point value of square root of x . ; If square root is an integer ; Function to precompute the perfect squares upto 100000. ; Function to print the sum for each query ; To calculate the precompute function ; Calling the printSum function for every query
var pref = Array ( 100010 ) . fill ( 0 ) ; function isPerfectSquare ( x ) { var sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ? x : 0 ; } function compute ( ) { for ( var i = 1 ; i <= 100000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + isPerfectSquare ( i ) ; } } function printSum ( L , R ) { var sum = pref [ R ] - pref [ L - 1 ] ; document . write ( sum + " " ) ; } compute ( ) ; var Q = 4 ; arr = [ [ 1 , 10 ] , [ 1 , 100 ] , [ 2 , 25 ] , [ 4 , 50 ] ] ; for ( var i = 0 ; i < Q ; i ++ ) printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ;
Program to find all Factors of a Number using recursion | Recursive function to print factors of a number ; Checking if the number is less than N ; Calling the function recursively for the next number ; Driver code
function factors ( n , i ) { if ( i <= n ) { if ( n % i == 0 ) { document . write ( i + " " ) ; } factors ( n , i + 1 ) ; } } var N = 16 ; factors ( N , 1 ) ;
Maximize value of ( a + b ) such that ( a * a | Function to maximize the value of ( a + b ) such that ( a * a - b * b = n ) ; Driver code
function maxValue ( n ) { return n ; } var n = 1 ; document . write ( maxValue ( n ) ) ;
Maximum number of edges that N | Function to find the maximum number of edges in a N - vertex graph . ; According to the Mantel 's theorem  the maximum number of edges will be  floor of [(n^2)/4] ; Driver code
function solve ( n ) { var ans = ( n * n / 4 ) ; return ans ; } var n = 10 ; document . write ( solve ( n ) ) ;
Multiplication table till N rows where every Kth row is table of K upto Kth term | Function to print the multiplication table upto K - th term ; For loop to iterate from 1 to N where i serves as the value of K ; Inner loop which at every iteration goes till i ; Printing the table value for i ; New line after every row ; Driver code
function printMultiples ( N ) { for ( let i = 1 ; i <= N ; i ++ ) { for ( let j = 1 ; j <= i ; j ++ ) { document . write ( ( i * j ) + " " ) ; } document . write ( " " ) ; } } let N = 5 ; printMultiples ( N ) ;
Find Maximum and Minimum of two numbers using Absolute function | Function to return maximum among the two numbers ; Function to return minimum among the two numbers ; Driver code ; Displaying the maximum value ; Displaying the minimum value
function maximum ( x , y ) { return ( ( x + y + Math . abs ( x - y ) ) / 2 ) ; } function minimum ( x , y ) { return ( ( x + y - Math . abs ( x - y ) ) / 2 ) ; } let x = 99 , y = 18 ; document . write ( " " + maximum ( x , y ) + " " ) ; document . write ( " " + minimum ( x , y ) ) ;
Find an array of size N having exactly K subarrays with sum S | Function to find array with K subarrays with sum S ; Driver Code ; Function call
function SubarraysWithSumS ( n , k , s ) { for ( let i = 0 ; i < k ; i ++ ) document . write ( s + " " ) ; for ( let i = k ; i < n ; i ++ ) document . write ( s + 1 + " " ) ; } let n = 4 , k = 2 , s = 3 ; SubarraysWithSumS ( n , k , s ) ;
Minimum number of cuts required to pay salary from N length Gold Bar | Function to find the minimum number of cuts to pay the worker . ; Nearest Integer to the Log value of the number N ; Driver code ; Cuts Required in the Length of 15
function pay ( n ) { let cuts = parseInt ( Math . log ( n ) / Math . log ( 2 ) ) ; return cuts ; } let n = 5 ; let cuts = pay ( n ) ; document . write ( cuts + " " ) ; n = 15 ; cuts = pay ( n ) ; document . write ( cuts + " " ) ;
Sum of Maximum and Minimum prime factor of every number in the Array | Javascript implementation of the approach ; max_prime [ i ] represent maximum prime number that divides the number i ; min_prime [ i ] represent minimum prime number that divides the number i ; Function to store the minimum prime factor and the maximum prime factor in two arrays ; Check for prime number if min_prime [ i ] > 0 , then it is not a prime number ; if i is a prime number min_prime number that divide prime number and max_prime number that divide prime number is the number itself . ; If this number is being visited for first time then this divisor must be the smallest prime number that divides this number ; The last prime number that divides this number will be maximum . ; Function to find the sum of the minimum and the maximum prime factors of every number from the given array ; Pre - calculation ; For every element of the given array ; The sum of its smallest and largest prime factor ; Driver code
var MAX = 100000 ; var max_prime = Array . from ( { length : MAX + 1 } , ( _ , i ) => 0 ) ; var min_prime = Array . from ( { length : MAX + 1 } , ( _ , i ) => 0 ) ; function sieve ( n ) { for ( var i = 2 ; i <= n ; ++ i ) { if ( min_prime [ i ] > 0 ) { continue ; } min_prime [ i ] = i ; max_prime [ i ] = i ; var j = i + i ; while ( j <= n ) { if ( min_prime [ j ] == 0 ) { min_prime [ j ] = i ; } max_prime [ j ] = i ; j += i ; } } } function findSum ( arr , n ) { sieve ( MAX ) ; for ( i = 0 ; i < n ; i ++ ) { var sum = min_prime [ arr [ i ] ] + max_prime [ arr [ i ] ] ; document . write ( sum + " " ) ; } } var arr = [ 5 , 10 , 15 , 20 , 25 , 30 ] ; var n = arr . length ; findSum ( arr , n ) ;
Pair of integers ( a , b ) which satisfy the given equations | Function to count valid pairs ; Driver code
function pairCount ( n , m ) { var cnt = 0 , b , a ; for ( b = 0 ; b <= Math . sqrt ( m ) ; b ++ ) { a = m - b * b ; if ( a * a + b == n ) { cnt ++ ; } } return cnt ; } var n = 9 , m = 3 ; document . write ( pairCount ( n , m ) ) ;
Largest Divisor for each element in an array other than 1 and the number itself | Javascript implementation of the approach ; Divisors array to keep track of the maximum divisor ; Function to pre - compute the prime numbers and largest divisors ; Visited array to keep track of prime numbers ; 0 and 1 are not prime numbers ; Initialising divisors [ i ] = i ; For all the numbers divisible by 2 the maximum divisor will be number / 2 ; If divisors [ i ] is not equal to i then this means that divisors [ i ] contains minimum prime divisor for the number ; Update the answer to i / smallest_prime_divisor [ i ] ; Condition if i is a prime number ; If divisors [ j ] is equal to j then this means that i is the first prime divisor for j so we update divi [ j ] = i ; If the current element is prime then it has no divisors other than 1 and itself ; Driver code
var maxin = 100001 ; var divisors = Array ( maxin ) . fill ( 0 ) ; function Calc_Max_Div ( arr , n ) { var vis = Array ( maxin ) . fill ( 1 ) ; vis [ 0 ] = vis [ 1 ] = 0 ; var i , j ; for ( i = 1 ; i <= maxin ; i ++ ) divisors [ i ] = i ; for ( i = 4 ; i <= maxin ; i += 2 ) { vis [ i ] = 0 ; divisors [ i ] = i / 2 ; } for ( i = 3 ; i <= maxin ; i += 2 ) { if ( divisors [ i ] != i ) { divisors [ i ] = i / divisors [ i ] ; } if ( vis [ i ] == 1 ) { for ( j = i * i ; j < maxin ; j += i ) { vis [ j ] = 0 ; if ( divisors [ j ] == j ) divisors [ j ] = i ; } } } for ( i = 0 ; i < n ; i ++ ) { if ( divisors [ arr [ i ] ] == arr [ i ] ) document . write ( " " ) ; else document . write ( divisors [ arr [ i ] ] + " " ) ; } } var arr = [ 5 , 6 , 7 , 8 , 9 , 10 ] ; var n = arr . length ; Calc_Max_Div ( arr , n ) ;
Given a number N in decimal base , find the sum of digits in any base B | Function to compute sum of Digits of Number N in base B ; Initialize sum with 0 ; Compute unit digit of the number ; Add unit digit in sum ; Update value of Number ; Driver function
function sumOfDigit ( n , b ) { var unitDigit , sum = 0 ; while ( n > 0 ) { unitDigit = n % b ; sum += unitDigit ; n = parseInt ( n / b ) ; } return sum ; } var n = 50 ; var b = 2 ; document . write ( sumOfDigit ( n , b ) ) ;
Find the Nth digit from right in base B of the given number in Decimal base | Function to compute Nth digit from right in base B ; Skip N - 1 Digits in Base B ; Nth Digit from right in Base B ; Driver Code
function nthDigit ( a , n , b ) { for ( var i = 1 ; i < n ; i ++ ) a = parseInt ( a / b ) ; return a % b ; } var a = 100 ; var n = 3 ; var b = 4 ; document . write ( nthDigit ( a , n , b ) ) ;
Print all maximal increasing contiguous sub | Function to print each of maximal contiguous increasing subarray ; Loop to iterate through the array and print the maximal contiguous increasing subarray . ; Condition to check whether the element at i , is greater than its next neighbouring element or not . ; Driver function
function printmaxSubseq ( arr , n ) { let i ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i + 1 ] ) document . write ( arr [ i ] + " " ) ; else document . write ( arr [ i ] + " " ) ; } } let arr = [ 9 , 8 , 11 , 13 , 10 , 15 , 14 , 16 , 20 , 5 ] ; let n = arr . length ; printmaxSubseq ( arr , n ) ;
Count of non decreasing arrays of length N formed with values in range L to R | Function to return the count of different arrays ; No such combination exists ; Arrays formed with single elements ; Driver code
function countSum ( N , L , R ) { if ( L > R ) { return 0 ; } if ( N == 1 ) { return R - L + 1 ; } if ( N > 1 ) { return ( N - 2 ) * ( R - L ) + 1 ; } } var N = 4 , L = 4 , R = 6 ; document . write ( countSum ( N , L , R ) ) ;
Minimum decrement operations to make Array elements equal by only decreasing K each time | javascript implementation of the above approach ; Finding the minimum element ; Loop over all the elements and find the difference ; Solution found and returned ; Driver code
var INT_MAX = Number . MAX_VALUE ; function solve ( arr , n , k ) { var minx = INT_MAX ; var i ; for ( i = 0 ; i < n ; i ++ ) { minx = Math . min ( minx , arr [ i ] ) ; } var decrements = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] - minx ) % k != 0 ) { return - 1 ; } else { decrements += ( ( arr [ i ] - minx ) / k ) ; } } return decrements ; } var n , k ; n = 3 ; k = 3 ; var arr = [ 12 , 9 , 15 ] ; document . write ( solve ( arr , n , k ) ) ;
Count non decreasing subarrays of size N from N Natural numbers | Returns value of Binomial Coefficient C ( n , k ) ; Since nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Function to find the count of required subarrays ; The required count is the binomial coefficient as explained in the approach above ; Driver Function
function binomialCoeff ( n , k ) { var C = Array ( k + 1 ) . fill ( 0 ) ; C [ 0 ] = 1 ; for ( var i = 1 ; i <= n ; i ++ ) { for ( var j = Math . min ( i , k ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } return C [ k ] ; } function count_of_subarrays ( N ) { var count = binomialCoeff ( 2 * N - 1 , N ) ; return count ; } var N = 3 ; document . write ( count_of_subarrays ( N ) ) ;
Length of Smallest subarray in range 1 to N with sum greater than a given value | Function to do a binary search on a given range . ; Total sum is the sum of N numbers . ; Sum until mid ; If remaining sum is < the required value , then the required number is in the right half ; Driver code
function usingBinarySearch ( start , end , N , S ) { if ( start >= end ) return start ; let mid = start + ( end - start ) / 2 ; let totalSum = ( N * ( N + 1 ) ) / 2 ; let midSum = ( mid * ( mid + 1 ) ) / 2 ; if ( ( totalSum - midSum ) <= S ) { return usingBinarySearch ( start , mid , N , S ) ; } return usingBinarySearch ( mid + 1 , end , N , S ) ; } let N , S ; N = 5 ; S = 11 ; document . write ( ( N - usingBinarySearch ( 1 , N , N , S ) + 1 ) + " " ) ;
Distribution of a Number in Array within a Range | Function for the distribution of the number ; Distribute the number among k elements ; If there is some remaining sum to distribute ; If there are elements remaining to distribute i . e . ( n - k ) ; Divide the remaining sum into n - k elements ; Print the distribution ; Driver code
function distribution ( n , k , l , r , S , Sk ) { let a = new Array ( n ) ; let len = k , temp , rem , s ; let diff = S - Sk ; for ( let i = 0 ; i < len ; i ++ ) { temp = Sk / k ; rem = Sk % k ; if ( temp + rem >= l && temp + rem <= r ) { a [ i ] = temp ; } else if ( temp + rem > r ) { a [ i ] = r ; } else if ( temp + rem < r ) { document . write ( " " ) ; return ; } Sk = Sk - a [ i ] ; k = k - 1 ; } if ( Sk > 0 ) { document . write ( " " ) ; return ; } if ( len ) { k = n - len ; for ( let i = len ; i < n ; i ++ ) { temp = diff / k ; rem = diff % k ; if ( temp + rem >= l && temp + rem <= r ) { a [ i ] = temp ; } else if ( temp + rem > r ) { a [ i ] = r ; } else if ( temp + rem < r ) { document . write ( " " ) ; return ; } diff = diff - a [ i ] ; k = k - 1 ; } if ( diff ) { document . write ( " " ) ; return ; } } for ( let i = 0 ; i < n ; i ++ ) { document . write ( a [ i ] + " " ) ; } } let n = 5 , k = 3 , l = 1 , r = 5 , S = 13 , Sk = 9 ; distribution ( n , k , l , r , S , Sk ) ;
Number of valid indices in the permutation of first N natural numbers | Function to return the number of i 's such that Pi <= Pj for all 1 <= j <= i in the permutation of first N natural numbers ; To store the count of such indices ; Store the mini value ; For all the elements ; Return the required answer ; Driver code
function min_index ( p , n ) { let ans = 0 ; let mini = Number . MAX_SAFE_INTEGER ; for ( let i = 0 ; i < n ; i ++ ) { if ( p [ i ] <= mini ) mini = p [ i ] ; if ( mini == p [ i ] ) ans ++ ; } return ans ; } let P = [ 4 , 2 , 5 , 1 , 3 ] ; let n = P . length ; document . write ( min_index ( P , n ) ) ;
Check if Sum and XOR of all elements of array is equal | Function to Check if Sum and XOR of all elements of array is equal ; Sum and XOR of all elements ; Checking Sum and XOR to be equal ; Driver Function ; Check Sum and XOR is equal
function equal_xor_sum ( arr , n ) { let Sum = 0 ; let Xor = 0 ; for ( let i = 0 ; i < n ; i ++ ) { Sum = Sum + arr [ i ] ; Xor = Xor ^ arr [ i ] ; } if ( Sum === Xor ) document . write ( " " ) ; else document . write ( " " ) ; } let arr = [ 6 , 3 , 7 , 10 ] ; let n = arr . length ; equal_xor_sum ( arr , n ) ;
Count of odd length contiguous Palindromic sequences in a Matrix | Javascript code to Count the odd length contiguous Palindromic sequences in the matrix ; Function to count the number of contiguous palindromic sequences in the matrix ; Add the total number of elements in the matrix to the count ; Length of possible sequence to be checked for palindrome horizontally and vertically ; Iterate through each element of the matrix and count the number of palindromic sequences in each row and column ; Find the possible length of sequences that can be a palindrome ; From i , check if the sequence formed by elements to its left and right is palindrome or not ; if the sequence [ i , j - k ] to [ i , j + k ] is a palindrome , increment the count by 1 ; From i , check if the sequence formed by elements to its above and below is palindrome or not ; if the sequence [ i - k , j ] to [ i + k , j ] is a palindrome , increment the count by 1 ; Return the total count of the palindromic sequences ; Driver code
let MAX = 10 function countPalindromes ( n , m , matrix ) { let count = n * m ; let length_of_sequence_row ; let length_of_sequence_column ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { length_of_sequence_row = Math . min ( j , m - 1 - j ) ; length_of_sequence_column = Math . min ( i , n - i - 1 ) ; for ( let k = 1 ; k <= length_of_sequence_row ; k ++ ) { if ( matrix [ i ] [ j - k ] == matrix [ i ] [ j + k ] ) { count ++ ; } else { break ; } } for ( let k = 1 ; k <= length_of_sequence_column ; k ++ ) { if ( matrix [ i - k ] [ j ] == matrix [ i + k ] [ j ] ) { count ++ ; } else { break ; } } } } return count ; } let m = 3 , n = 3 ; let matrix = [ [ 2 , 1 , 2 ] , [ 1 , 1 , 1 ] , [ 2 , 1 , 2 ] ] ; document . write ( countPalindromes ( n , m , matrix ) + " " ) ;
Reduce a number to 1 by performing given operations | Set 2 | Function to return the number of set bits in n ; Function to return the minimum steps required to reach 1 ; If n is even then divide it by 2 ; If n is 3 or the number of set bits in ( n - 1 ) is less than the number of set bits in ( n + 1 ) ; Increment the number of steps ; Return the minimum number of steps ; Driver code
function set_bits ( n ) { let count = 0 ; while ( n ) { count += n % 2 ; n = parseInt ( n / 2 ) ; } return count ; } function minSteps ( n ) { let ans = 0 ; while ( n != 1 ) { if ( n % 2 == 0 ) n = parseInt ( n / 2 ) ; else if ( n == 3 || set_bits ( n - 1 ) < set_bits ( n + 1 ) ) n -- ; else n ++ ; ans ++ ; } return ans ; } let n = 15 ; document . write ( minSteps ( n ) ) ;
Minimum integer that can be obtained by swapping adjacent digits of different parity | Function to return the minimum number ; Store the elements which are divisible by two in stack1 ; Store the elements which are not divisible by two in stack2 . ; Concatenate the answer with smaller value of the topmost elements of both the stacks and then pop that element ; Concatenate the answer with remaining values of stack1 . ; Concatenate the answer with remaining values of stack2 . ; Driver code ; Function calling
function minimumNo ( n ) { var ans = 0 ; var stack1 = [ ] ; var stack2 = [ ] ; while ( n !== 0 ) { var r = n % 10 ; if ( r % 2 === 0 ) { stack1 . push ( r ) ; } else { stack2 . push ( r ) ; } n = parseInt ( n / 10 ) ; } while ( stack1 . length !== 0 && stack2 . length !== 0 ) { if ( stack1 [ stack1 . length - 1 ] < stack2 [ stack2 . length - 1 ] ) { ans = ans * 10 + stack1 [ stack1 . length - 1 ] ; stack1 . pop ( ) ; } else { ans = ans * 10 + stack2 [ stack2 . length - 1 ] ; stack2 . pop ( ) ; } } while ( stack1 . length !== 0 ) { ans = ans * 10 + stack1 [ stack1 . length - 1 ] ; stack1 . pop ( ) ; } while ( stack2 . length !== 0 ) { ans = ans * 10 + stack2 [ stack2 . length - 1 ] ; stack2 . pop ( ) ; } return ans ; } var n1 = 64432 ; document . write ( minimumNo ( n1 ) + " " ) ; var n2 = 3137 ; document . write ( minimumNo ( n2 ) + " " ) ;
Find two numbers with the given LCM and minimum possible difference | Function to return the LCM of a and b ; Function to find and print the two numbers ; To find the factors ; To check if i is a factor of x and the minimum possible number satisfying the given conditions ; Driver code
function lcm ( a , b ) { return ( a / __gcd ( a , b ) * b ) ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } function findNums ( x ) { let ans = - 1 ; for ( let i = 1 ; i <= Math . sqrt ( x ) ; i ++ ) { if ( x % i == 0 && lcm ( i , Math . floor ( x / i ) ) == x ) { ans = i ; } } document . write ( ans + " " + Math . floor ( x / ans ) ) ; } let x = 12 ; findNums ( x ) ;
Find an integer that is common in the maximum number of given arithmetic progressions | Javascript implementation of the approach ; Function to return element common in maximum number of APs ; Initialize the count variable ; Increment count for every element of an AP ; Find the index with maximum count ; Return the maximum common element ; Driver code
var MAXN = 1000000 ; function maxCommonElement ( A , D , N ) { var cnt = Array ( MAXN ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { for ( var j = A [ i ] ; j < MAXN ; j += D [ i ] ) cnt [ j ] ++ ; } var ans = 0 ; var com = 0 ; for ( var i = 0 ; i < MAXN ; i ++ ) { if ( cnt [ i ] > ans ) { ans = cnt [ i ] ; com = i ; } } return com ; } var A = [ 13 , 1 , 2 , 5 ] , D = [ 5 , 10 , 1 , 12 ] ; var N = A . length ; document . write ( maxCommonElement ( A , D , N ) ) ;
Number of trailing zeros in N * ( N | Function to return the count of trailing 0 s in the given function ; If n is odd ; If n is even ; Find the trailing zeros in n / 2 factorial ; Return the required answer ; Driver code
function findTrailingZeros ( n ) { if ( n & 1 ) return 0 ; else { let ans = 0 ; n = parseInt ( n / 2 ) ; while ( n ) { ans += parseInt ( n / 5 ) ; n = parseInt ( n / 5 ) ; } return ans ; } } let n = 12 ; document . write ( findTrailingZeros ( n ) ) ;
Divide N into K unique parts such that gcd of those parts is maximum | Function to calculate maximum GCD ; Minimum possible sum for K unique positive integers ; It is not possible to divide N into K unique parts ; All the factors greater than sqrt ( N ) are complementary of the factors less than sqrt ( N ) ; If i is a factor of N ; Driver code
function maxGCD ( N , K ) { var minSum = ( K * ( K + 1 ) ) / 2 ; if ( N < minSum ) return - 1 ; var i = parseInt ( Math . sqrt ( N ) ) ; var res = 1 ; while ( i >= 1 ) { if ( N % i == 0 ) { if ( i >= minSum ) res = Math . max ( res , N / i ) ; if ( N / i >= minSum ) res = Math . max ( res , i ) ; } i -- ; } return res ; } var N = 18 , K = 3 ; document . write ( maxGCD ( N , K ) ) ;
Radius of the inscribed circle within three tangent circles | Radius of the 3 given circles declared as double . ; Calculation of area of a triangle by Heron 's formula ; Applying binary search to find the radius r4 of the required circle ; Area of main triangle ; Loop runs until l and h becomes approximately equal ; Area of smaller triangles ; If sum of smaller triangles is less than main triangle ; If sum of smaller triangles is greater than or equal to main triangle ; Taking r1 , r2 , r3 as input ; Call to function binary search
let r1 , r2 , r3 ; function area ( a , b , c ) { let p = ( a + b + c ) / 2 ; return Math . sqrt ( p ) * Math . sqrt ( p - a ) * Math . sqrt ( p - b ) * Math . sqrt ( p - c ) ; } function binary_search ( ) { let s = area ( r1 + r2 , r2 + r3 , r3 + r1 ) ; let l = 0 , h = s / ( r1 + r2 + r3 ) ; while ( h - l >= 1.e-7 ) { let mid = ( l + h ) / 2 ; let s1 = area ( mid + r1 , mid + r2 , r1 + r2 ) ; let s2 = area ( mid + r1 , mid + r3 , r1 + r3 ) ; let s3 = area ( mid + r2 , mid + r3 , r2 + r3 ) ; if ( s1 + s2 + s3 < s ) { l = mid ; } else { h = mid ; } } return ( l + h ) / 2 ; } r1 = 1.0 ; r2 = 2.0 ; r3 = 3.0 ; document . write ( binary_search ( ) . toPrecision ( 6 ) ) ;
Radius of the inscribed circle within three tangent circles | Radius of the 3 given circles declared as double . ; Taking r1 , r2 , r3 as input ; Calculation of r4 using formula given above
let r1 , r2 , r3 ; r1 = 1 ; r2 = 2 ; r3 = 3 ; let r4 = ( r1 * r2 * r3 ) / ( r1 * r2 + r2 * r3 + r1 * r3 + 2.0 * Math . sqrt ( r1 * r2 * r3 * ( r1 + r2 + r3 ) ) ) ; document . write ( r4 . toFixed ( 6 ) + " " ) ;