codeparrot/xlcost-text-to-code huggingface dataset:
Maximum profit after buying and selling stocks with transaction fees | Javascript implementation of above approach ; b [ 0 ] will contain the maximum profit ; b [ 1 ] will contain the day on which we are getting the maximum profit ; here finding the max profit ; if we get less then or equal to zero it means we are not getting the profit ; check if sum is greater then maximum then store the new maximum ; Driver code
function max_profit ( a , b , n , fee ) { var i , j , profit ; var l , r , diff_day = 1 , sum = 0 ; b [ 0 ] = 0 ; b [ 1 ] = diff_day ; for ( i = 1 ; i < n ; i ++ ) { l = 0 ; r = diff_day ; sum = 0 ; for ( j = n - 1 ; j >= i ; j -- ) { profit = ( a [ r ] - a [ l ] ) - fee ; if ( profit > 0 ) { sum = sum + profit ; } l ++ ; r ++ ; } if ( b [ 0 ] < sum ) { b [ 0 ] = sum ; b [ 1 ] = diff_day ; } diff_day ++ ; } return 0 ; } var arr = [ 6 , 1 , 7 , 2 , 8 , 4 ] ; var n = arr . length ; var b = Array ( 2 ) . fill ( 0 ) ; var tranFee = 2 ; max_profit ( arr , b , n , tranFee ) ; document . write ( b [ 0 ] + " " + b [ 1 ] ) ;
Eggs dropping puzzle ( Binomial Coefficient and Binary Search Solution ) | Find sum of binomial coefficients xCi ( where i varies from 1 to n ) . If the sum becomes more than K ; Do binary search to find minimum number of trials in worst case . ; Initialize low and high as 1 st and last floors ; Do binary search , for every mid , find sum of binomial coefficients and check if the sum is greater than k or not . ; Driver code
function binomialCoeff ( x , n , k ) { var sum = 0 , term = 1 ; for ( var i = 1 ; i <= n && sum < k ; ++ i ) { term *= x - i + 1 ; term /= i ; sum += term ; } return sum ; } function minTrials ( n , k ) { var low = 1 , high = k ; while ( low < high ) { var mid = parseInt ( ( low + high ) / 2 ) ; if ( binomialCoeff ( mid , n , k ) < k ) low = mid + 1 ; else high = mid ; } return low ; } document . write ( minTrials ( 2 , 10 ) ) ;
Find next palindrome prime | Javascript program to find next palindromic prime for a given number . ; if ( 8 <= N <= 11 ) return 11 ; generate odd length palindrome number which will cover given constraint . ; if y >= N and it is a prime number then return it . ; Driver code
function isPrime ( num ) { if ( num < 2 num % 2 == 0 ) return num == 2 ; for ( i = 3 ; i * i <= num ; i += 2 ) if ( num % i == 0 ) return false ; return true ; } function primePalindrome ( N ) { if ( 8 <= N && N <= 11 ) return 11 ; for ( let x = 1 ; x < 100000 ; ++ x ) { let s = String ( x ) ; let r = s . split ( " " ) . reverse ( ) . join ( " " ) ; let y = parseInt ( s + r . substr ( 1 ) ) ; if ( y >= N && isPrime ( y ) == true ) return y ; } return - 1 ; } document . write ( primePalindrome ( 112 ) ) ;
Number of integral solutions for equation x = b * ( sumofdigits ( x ) ^ a ) + c | This function returns the sum of the digits of a number ; This function creates the array of valid numbers ; this computes s ( x ) ^ a ; this gives the result of equation ; checking if the sum same as i ; counter to keep track of numbers ; resultant array ; prints the number ; Driver Code ; calculate which value of x are possible
function getsum ( a ) { let r = 0 , sum = 0 ; while ( a > 0 ) { r = a % 10 ; sum = sum + r ; a = Math . floor ( a / 10 ) ; } return sum ; } function value ( a , b , c ) { let co = 0 , p = 0 ; let no , r = 0 , x = 0 , q = 0 , w = 0 ; let v = [ ] ; for ( let i = 1 ; i < 82 ; i ++ ) { no = Math . pow ( i , a ) ; no = b * no + c ; if ( no > 0 && no < 1000000000 ) { x = getsum ( no ) ; if ( x == i ) { q ++ ; v . push ( no ) ; w ++ ; } } } for ( let i = 0 ; i < v . length ; i ++ ) { document . write ( v [ i ] + " " ) ; } } let a = 2 , b = 2 , c = - 1 ; value ( a , b , c ) ;
Cunningham chain | Function to print Cunningham chain of the second kind ; Iterate till all elements are printed ; check prime or not ; Driver Code
function print ( p0 ) { var p1 , i = 0 , x , flag = 1 , k , m = 4 ; while ( flag ) { flag = 1 ; x = Math . pow ( 2 , i ) ; p1 = x * p0 - ( x - 1 ) ; for ( k = 2 ; k < p1 ; k ++ ) { if ( p1 % k == 0 ) { flag = 0 ; break ; } } if ( flag == 0 ) break ; document . write ( p1 + " " ) ; i ++ ; } } var p0 = 19 ; print ( p0 ) ;
Count pairs with Bitwise AND as ODD number | JavaScript program to count pairs with Odd AND ; Count total odd numbers in ; return count of even pair ; Driver main ; calling function findOddPair and print number of odd pair
function findOddPair ( A , N ) { let count = 0 ; for ( let i = 0 ; i < N ; i ++ ) if ( ( A [ i ] % 2 == 1 ) ) count ++ ; return count * ( count - 1 ) / 2 ; } let a = [ 5 , 1 , 3 , 2 ] ; let n = a . length ; document . write ( findOddPair ( a , n ) ) ;
Surd number | Returns true if x is Surd number ; Try all powers of i ; Driver Code
function isSurd ( n ) { for ( let i = 2 ; i * i <= n ; i ++ ) { let j = i ; while ( j < n ) j = j * i ; if ( j == n ) return false ; } return true ; } let n = 15 ; if ( isSurd ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Program to find last two digits of 2 ^ n | Find the first digit ; Get the last digit from the number ; Remove last digit from number ; Get the last digit from the number ( last second of num ) ; Take last digit to ten 's position  i.e. last second digit ; Add the value of ones and tens to make it complete 2 digit number ; return the first digit ; Driver program ; pow function used
function LastTwoDigit ( num ) { let one = num % 10 ; num = Math . floor ( num / 10 ) ; let tens = num % 10 ; tens *= 10 ; num = tens + one ; return num ; } let n = 10 ; let num = 1 ; num = Math . pow ( 2 , n ) ; document . write ( " " + 2 ) ; document . write ( " " + 2 ) ; document . write ( " " + n + " " ) ; document . write ( LastTwoDigit ( num ) + " " ) ;
Program to find last two digits of 2 ^ n | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; JavaScript function to calculate number of digits in x ; JavaScript function to prlet last 2 digits of 2 ^ n ; Generating 10 ^ 2 ; Calling modular exponentiation ; Printing leftmost zeros . Since ( 2 ^ n ) % 2 can have digits less then 2. In that case we need to prlet zeros ; If temp is not zero then print temp . If temp is zero then already prleted ; driver program
function power ( x , y , p ) { while ( y > 0 ) { let r = y & 1 ; if ( r == 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } function numberOfDigits ( x ) { let i = 0 ; while ( x != 0 ) { x /= 10 ; i ++ ; } return i ; } function LastTwoDigit ( n ) { document . write ( " " + 2 + " " + 2 + " " ) ; document . write ( n + " " ) ; let temp = 1 ; for ( let i = 1 ; i <= 2 ; i ++ ) temp *= 10 ; temp = power ( 2 , n , temp ) ; for ( let i = 0 ; i < ( 2 - numberOfDigits ( temp ) ) ; i ++ ) document . write ( 0 + " " ) ; if ( temp != 0 ) document . write ( temp ) ; } let n = 72 ; LastTwoDigit ( n ) ;
Find gcd ( a ^ n , c ) where a , n and c can vary from 1 to 10 ^ 9 | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Finds GCD of a and b ; Finds GCD of a ^ n and c ; check if c is a divisor of a ; First compute ( a ^ n ) % c ; Now simply return GCD of modulo power and c . ; Driver code
function modPower ( x , y , p ) { while ( y > 0 ) { if ( ( y & 1 ) > 0 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function gcdPow ( a , n , c ) { if ( a % c == 0 ) return c ; let modexpo = modPower ( a , n , c ) ; return gcd ( modexpo , c ) ; } let a = 10248585 , n = 1000000 , c = 12564 ; document . write ( gcdPow ( a , n , c ) ) ;
Number of sub arrays with odd sum | Javascript code to find count of sub - arrays with odd sum ; Find sum of all subarrays and increment result if sum is odd ; Driver code
function countOddSum ( ar , n ) { let result = 0 ; for ( let i = 0 ; i <= n - 1 ; i ++ ) { let val = 0 ; for ( let j = i ; j <= n - 1 ; j ++ ) { val = val + ar [ j ] ; if ( val % 2 != 0 ) result ++ ; } } return ( result ) ; } let ar = [ 5 , 4 , 4 , 5 , 1 , 3 ] ; let n = ar . length ; document . write ( " " + " " ) ; document . write ( countOddSum ( ar , n ) ) ;
Number of sub arrays with odd sum | Javascript code to find count of sub - arrays with odd sum ; A temporary array of size 2. temp [ 0 ] is going to store count of even subarrays and temp [ 1 ] count of odd . temp [ 0 ] is initialized as 1 because there a single odd element is also counted as a subarray ; Initialize count . sum is sum of elements under modulo 2 and ending with arr [ i ] . ; i ' th ▁ iteration ▁ computes ▁ sum ▁ ▁ of ▁ arr [ 0 . . i ] ▁ under ▁ modulo ▁ 2 ▁ ▁ and ▁ increments ▁ even / odd ▁ count ▁ ▁ according ▁ to ▁ sum ' s value ; 2 is added to handle negative numbers ; Increment even / odd count ; An odd can be formed by an even - odd pair ; Driver code
function countOddSum ( ar , n ) { let temp = [ 1 , 0 ] ; let result = 0 , val = 0 ; for ( let i = 0 ; i <= n - 1 ; i ++ ) { val = ( ( val + ar [ i ] ) % 2 + 2 ) % 2 ; temp [ val ] ++ ; } result = temp [ 0 ] * temp [ 1 ] ; return ( result ) ; } let ar = [ 5 , 4 , 4 , 5 , 1 , 3 ] ; let n = ar . length ; document . write ( " " + " " + countOddSum ( ar , n ) ) ;
Program to print factors of a number in pairs | Javascript program to print prime factors in pairs . ; Driver code
function printPFsInPairs ( n ) { for ( let i = 1 ; i * i <= n ; i ++ ) if ( n % i == 0 ) document . write ( i + " " + parseInt ( n / i ) + " " ) ; } let n = 24 ; printPFsInPairs ( n ) ;
Sum of elements in range L | Function to find the sum between L and R ; array created ; fill the first half of array ; fill the second half of array ; find the sum between range ; Driver Code
function rangesum ( n , l , r ) { let arr = new Array ( n ) ; let c = 1 , i = 0 ; while ( c <= n ) { arr [ i ++ ] = c ; c += 2 ; } c = 2 ; while ( c <= n ) { arr [ i ++ ] = c ; c += 2 ; } let sum = 0 ; for ( i = l - 1 ; i < r ; i ++ ) { sum += arr [ i ] ; } return sum ; } let n = 12 ; let l = 1 , r = 11 ; document . write ( rangesum ( n , l , r ) ) ;
Sum of elements in range L | Function to calculate the sum if n is even ; both l and r are to the left of mid ; first and last element ; Total number of terms in the sequence is r - l + 1 ; use of formula derived ; both l and r are to the right of mid ; first and last element ; Use of formula derived ; left is to the left of mid and right is to the right of mid ; Take two sums i . e left and right differently and add ; first and last element ; total terms ; no of terms ; The first even number is 2 ; The last element is given by 2 * ( r - n / 2 ) ; formula applied ; Function to calculate the sum if n is odd ; take ceil value if n is odd ; both l and r are to the left of mid ; first and last element ; number of terms ; formula ; both l and r are to the right of mid ; first and last term , ; no of terms ; formula used ; If l is on left and r on right ; calculate separate sums ; first half ; calculate terms ; second half ; add both halves ; Function to find the sum between L and R ; If n is even ; If n is odd ; Driver Code
function sumeven ( n , l , r ) { let sum = 0 ; let mid = Math . floor ( n / 2 ) ; if ( r <= mid ) { let first = ( 2 * l - 1 ) ; let last = ( 2 * r - 1 ) ; let no_of_terms = r - l + 1 ; sum = ( ( no_of_terms ) * ( ( first + last ) ) ) / 2 ; } else if ( l >= mid ) { let first = ( 2 * ( l - n / 2 ) ) ; let last = ( 2 * ( r - n / 2 ) ) ; let no_of_terms = r - l + 1 ; sum = ( ( no_of_terms ) * ( ( first + last ) ) ) / 2 ; } else { let sumleft = 0 , sumright = 0 ; let first_term1 = ( 2 * l - 1 ) ; let last_term1 = ( 2 * ( n / 2 ) - 1 ) ; let no_of_terms1 = n / 2 - l + 1 ; sumleft = ( ( no_of_terms1 ) * ( ( first_term1 + last_term1 ) ) ) / 2 ; let first_term2 = 2 ; let last_term2 = ( 2 * ( r - n / 2 ) ) ; let no_of_terms2 = r - mid ; sumright = ( ( no_of_terms2 ) * ( ( first_term2 + last_term2 ) ) ) / 2 ; sum = ( sumleft + sumright ) ; } return sum ; } function sumodd ( n , l , r ) { let mid = Math . floor ( n / 2 ) + 1 ; let sum = 0 ; if ( r <= mid ) { let first = ( 2 * l - 1 ) ; let last = ( 2 * r - 1 ) ; let no_of_terms = r - l + 1 ; sum = ( ( no_of_terms ) * ( ( first + last ) ) ) / 2 ; } else if ( l > mid ) { let first = ( 2 * ( l - mid ) ) ; let last = ( 2 * ( r - mid ) ) ; let no_of_terms = r - l + 1 ; sum = ( ( no_of_terms ) * ( ( first + last ) ) ) / 2 ; } else { let sumleft = 0 , sumright = 0 ; let first_term1 = ( 2 * l - 1 ) ; let last_term1 = ( 2 * mid - 1 ) ; let no_of_terms1 = mid - l + 1 ; sumleft = ( ( no_of_terms1 ) * ( ( first_term1 + last_term1 ) ) ) / 2 ; let first_term2 = 2 ; let last_term2 = ( 2 * ( r - mid ) ) ; let no_of_terms2 = r - mid ; sumright = ( ( no_of_terms2 ) * ( ( first_term2 + last_term2 ) ) ) / 2 ; sum = ( sumleft + sumright ) ; } return sum ; } function rangesum ( n , l , r ) { let sum = 0 ; if ( n % 2 == 0 ) return sumeven ( n , l , r ) ; else return sumodd ( n , l , r ) ; } let n = 12 ; let l = 1 , r = 11 ; document . write ( rangesum ( n , l , r ) ) ;
Program to find the Interior and Exterior Angle of a Regular Polygon | function to find the interior and exterior angle ; formula to find the interior angle ; formula to find the exterior angle ; Displaying the output ; Driver code ; Function calling
function findAngle ( n ) { let interiorAngle , exteriorAngle ; interiorAngle = Math . floor ( ( n - 2 ) * 180 / n ) ; exteriorAngle = Math . floor ( 360 / n ) ; document . write ( " " + interiorAngle + " " ) ; document . write ( " " + exteriorAngle ) ; } let n = 10 ; findAngle ( n ) ;
Program to calculate distance between two points in 3 D | Function for distance ; Driver code ; function call for distance
function distance ( x1 , y1 , z1 , x2 , y2 , z2 ) { var d = Math . pow ( ( Math . pow ( x2 - x1 , 2 ) + Math . pow ( y2 - y1 , 2 ) + Math . pow ( z2 - z1 , 2 ) * 1.0 ) , 0.5 ) ; document . write ( " " + d . toFixed ( 10 ) ) ; return ; } var x1 = 2 ; var y1 = - 5 ; var z1 = 7 ; var x2 = 3 ; var y2 = 4 ; var z2 = 5 ; distance ( x1 , y1 , z1 , x2 , y2 , z2 ) ;
Check if the large number formed is divisible by 41 or not | Check if a number is divisible by 41 or not ; array to store all the digits ; base values ; calculate remaining digits ; calculate answer ; check for divisibility ; driver program
function DivisibleBy41 ( first , second , c , n ) { let digit = new Array ( n ) . fill ( 0 ) ; digit [ 0 ] = first ; digit [ 1 ] = second ; for ( let i = 2 ; i < n ; i ++ ) digit [ i ] = ( digit [ i - 1 ] * c + digit [ i - 2 ] ) % 10 ; let ans = digit [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) ans = ( ans * 10 + digit [ i ] ) % 41 ; if ( ans % 41 == 0 ) return true ; else return false ; } let first = 1 , second = 2 , c = 1 , n = 3 ; if ( DivisibleBy41 ( first , second , c , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Program to print pentatope numbers upto Nth term | Function to generate nth tetrahedral number ; Function to print pentatope number series upto nth term . ; Initialize prev as 0. It store the sum of all previously generated pentatope numbers ; Loop to print pentatope series ; Find ith tetrahedral number ; Add ith tetrahedral number to sum of all previously generated tetrahedral number to get ith pentatope number ; Update sum of all previously generated tetrahedral number ; Driver code ; Function call to print pentatope number series
function findTetrahedralNumber ( n ) { return ( ( n * ( n + 1 ) * ( n + 2 ) ) / 6 ) ; } function printSeries ( n ) { let prev = 0 ; let curr ; for ( let i = 1 ; i <= n ; i ++ ) { curr = findTetrahedralNumber ( i ) ; curr = curr + prev ; document . write ( curr + " " ) ; prev = curr ; } } let n = 10 ; printSeries ( n ) ;
Program to print pentatope numbers upto Nth term | Function to print pentatope series up to nth term ; Loop to print pentatope number series ; calculate and print ith pentatope number ; Driver code ; Function call to print pentatope number series
function printSeries ( n ) { for ( let i = 1 ; i <= n ; i ++ ) { num = ( i * ( i + 1 ) * ( i + 2 ) * ( i + 3 ) / 24 ) ; document . write ( num + " " ) ; } } let n = 10 ; printSeries ( n ) ;
Find unique pairs such that each element is less than or equal to N | Finding the number of unique pairs ; Using the derived formula ; Printing the unique pairs ; Driver code
function No_Of_Pairs ( N ) { let i = 1 ; while ( ( i * i * i ) + ( 2 * i * i ) + i <= N ) i ++ ; return ( i - 1 ) ; } function print_pairs ( pairs ) { let i = 1 , mul ; for ( i = 1 ; i <= pairs ; i ++ ) { mul = i * ( i + 1 ) ; document . write ( " " + i + " " + ( mul * i ) + " " + mul * ( i + 1 ) + " " ) ; } } let N = 500 , pairs , mul , i = 1 ; pairs = No_Of_Pairs ( N ) ; document . write ( " " + pairs + " " ) ; print_pairs ( pairs ) ;
Program to print tetrahedral numbers upto Nth term | function to generate nth triangular number ; function to print tetrahedral number series up to n ; Initialize prev as 0. It stores the sum of all previously generated triangular number ; Loop to print series ; Find ith triangular number ; Add ith triangular number to sum of all previously generated triangular number to get ith tetrahedral number ; Update sum of all previously generated triangular number ; Driver code ; function call to print series
function findTriangularNumber ( n ) { return ( n * ( n + 1 ) ) / 2 ; } function printSeries ( n ) { var prev = 0 ; var curr ; for ( var i = 1 ; i <= n ; i ++ ) { curr = findTriangularNumber ( i ) ; curr = curr + prev ; document . write ( curr + " " ) ; prev = curr ; } } var n = 10 ; printSeries ( n ) ;
Program to print tetrahedral numbers upto Nth term | function to print tetrahedral number series up to n ; loop to print series ; Calculate and print ith tetrahedral number ; driver program ; function call to print series
function printSeries ( n ) { let i ; for ( i = 1 ; i <= n ; i ++ ) { let num = i * ( i + 1 ) * ( ( i + 2 ) / 6 ) ; document . write ( num + " " ) ; } } let n = 10 ; printSeries ( n ) ;
Number of odd and even results for every value of x in range [ min , max ] after performing N steps | Function that prints the number of odd and even results ; If constant at layer i is even , beven is true , otherwise false . If the coefficient of x at layer i is even , aeven is true , otherwise false . ; If any of the coefficients at any layer is found to be even , then the product of all the coefficients will always be even . ; Checking whether the constant added after all layers is even or odd . ; Assuming input x is even . ; Assuming input x is odd . ; Displaying the counts . ; Driver Code
function count_even_odd ( min , max , steps ) { var a , b , even , odd ; var beven = true , aeven = false ; var n = 2 ; for ( i = 0 ; i < n ; i ++ ) { a = steps [ i ] [ 0 ] ; b = steps [ i ] [ 1 ] ; if ( ! ( aeven || ( a & 1 ) > 0 ) ) aeven = true ; if ( beven ) { if ( ( b & 1 ) > 0 ) beven = false ; } else if ( ! ( ( a & 1 ) > 0 ) ) { if ( ! ( ( b & 1 ) > 0 ) ) beven = true ; } else { if ( ( b & 1 ) > 0 ) beven = true ; } } if ( beven ) { even = parseInt ( max / 2 ) - parseInt ( ( min - 1 ) / 2 ) ; odd = 0 ; } else { even = parseInt ( max / 2 ) - parseInt ( ( min - 1 ) / 2 ) ; odd = 0 ; } if ( ! ( beven ^ aeven ) ) even += max - min + 1 - parseInt ( max / 2 ) + parseInt ( ( min - 1 ) / 2 ) ; else odd += max - min + 1 - parseInt ( max / 2 ) + parseInt ( ( min - 1 ) / 2 ) ; document . write ( " " + even + " " + odd ) ; } var min = 1 , max = 4 ; var steps = [ [ 1 , 2 ] , [ 3 , 4 ] ] ; count_even_odd ( min , max , steps ) ;
Maximum number of ones in a N * N matrix with given constraints | Function that returns the maximum number of ones ; Minimum number of zeroes ; Totol cells = square of the size of the matrices ; Initialising the answer ; Initialising the variables
function getMaxOnes ( n , x ) { let zeroes = parseInt ( n / x , 10 ) ; zeroes = zeroes * zeroes ; let total = n * n ; let ans = total - zeroes ; return ans ; } let n = 5 ; let x = 2 ; document . write ( getMaxOnes ( n , x ) ) ;
Minimum operations required to make all the elements distinct in an array | Function that returns minimum number of changes ; Hash - table to store frequency ; Increase the frequency of elements ; Traverse in the map to sum up the ( occurrences - 1 ) of duplicate elements ; Driver Code
function minimumOperations ( a , n ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( mp . get ( a [ i ] ) != null ) { let x = mp . get ( a [ i ] ) ; mp . set ( a [ i ] , ++ x ) ; } else mp . set ( a [ i ] , 1 ) ; } let count = 0 ; for ( let [ key , value ] of mp . entries ( ) ) { if ( value > 1 ) { count += ( value - 1 ) ; } } return count ; } let a = [ 2 , 1 , 2 , 3 , 3 , 4 , 3 ] ; let n = a . length ; document . write ( minimumOperations ( a , n ) ) ;
Check if a M | Javascript program to check if M - th fibonacci divides N - th fibonacci ; exceptional case for F ( 2 ) ; if none of the above cases , hence not divisible ; Driver Code
function check ( n , m ) { if ( n == 2 m == 2 n % m == 0 ) { document . write ( " " + " " ) ; } else { document . write ( " " + " " ) ; } } let m = 3 , n = 9 ; check ( n , m ) ;
Surface Area and Volume of Hexagonal Prism | Function to calculate Surface area ; Formula to calculate surface area ; Display surface area ; Function to calculate Volume ; formula to calculate Volume ; Display Volume ; Driver Code ; surface area function call ; volume function call
function findSurfaceArea ( a , h ) { let Area ; Area = 6 * a * h + 3 * Math . sqrt ( 3 ) * a * a ; document . write ( " " + Area . toFixed ( 3 ) + " " ) ; } function findVolume ( a , h ) { let Volume ; Volume = 3 * Math . sqrt ( 3 ) * a * a * h / 2 ; document . write ( " " + Volume . toFixed ( 3 ) ) ; } let a = 5 , h = 10 ; findSurfaceArea ( a , h ) ; findVolume ( a , h ) ;
Minimum number of mails required to distribute all the questions | Function returns the min no of mails required ; Using the formula derived above ; No of questions ; No of students ; Maximum no of questions a mail can hold ; Calling function
function MinimumMail ( n , k , x ) { let m = ( n - 1 ) + Math . ceil ( ( n - 1 ) * 1.0 / x ) * ( n - 1 ) + Math . ceil ( n * 1.0 / x ) * ( k - n ) ; return m ; } let N = 4 ; let K = 9 ; let X = 2 ; document . write ( MinimumMail ( N , K , X ) + " " ) ;
Program to find the Area of an Ellipse | Function to find area of an ellipse . ; formula to find the area of an Ellipse . ; Display the result ; Driver code
function findArea ( a , b ) { var Area ; Area = 3.142 * a * b ; document . write ( " " + Area . toFixed ( 2 ) ) ; } var a = 5 , b = 4 ; findArea ( a , b ) ;
Compute power of power k times % m | Function to compute the given value ; compute power k times ; Driver Code ; Calling function
function calculate ( x , k , m ) { let result = x ; k = k - 1 ; while ( k -- ) { result = Math . pow ( result , x ) ; if ( result > m ) result %= m ; } return result ; } let x = 5 ; let k = 2 ; let m = 3 ; document . write ( calculate ( x , k , m ) ) ;
Recursive program to check if number is palindrome or not | recursive function that returns the reverse of digits ; base case ; stores the reverse of a number ; Driver Code
function rev ( n , temp ) { if ( n == 0 ) return temp ; temp = ( temp * 10 ) + ( n % 10 ) ; return rev ( Math . floor ( n / 10 ) , temp ) ; } let n = 121 ; let temp = rev ( n , 0 ) ; if ( temp == n ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;
Program to find greater value between a ^ n and b ^ n | Function to find the greater value ; If n is even ; Driver code
function findGreater ( a , b , n ) { if ( ! ( ( n & 1 ) > 0 ) ) { a = Math . abs ( a ) ; b = Math . abs ( b ) ; } if ( a == b ) document . write ( " " + " " ) ; else if ( a > b ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; } var a = 12 , b = 24 , n = 5 ; findGreater ( a , b , n ) ;
Print first n Fibonacci Numbers using direct formula | Function to calculate fibonacci using recurrence relation formula ; Using direct formula ; Driver code
function fibonacci ( n ) { var fib ; for ( i = 0 ; i < n ; i ++ ) { fib = ( Math . pow ( ( 1 + Math . sqrt ( 5 ) ) , i ) - Math . pow ( ( 1 - Math . sqrt ( 5 ) ) , i ) ) / ( Math . pow ( 2 , i ) * Math . sqrt ( 5 ) ) ; document . write ( parseInt ( fib ) + " " ) ; } } var n = 8 ; fibonacci ( n ) ;