codeparrot/xlcost-text-to-code huggingface dataset:
Bakhshali Approximation for computing square roots | javascript program gives result approximated to 5 decimal places . { ; This will be the nearest perfect square to s ; This is the sqrt of pSq ; Find the nearest perfect square to s ; calculate d ; calculate P ; calculate A ; calculate sqrt ( S ) . ; Driver program
function sqroot ( s ) { var pSq = 0 ; var N = 0 ; for ( i = parseInt ( s ) ; i > 0 ; i -- ) { for ( j = 1 ; j < i ; j ++ ) { if ( j * j == i ) { pSq = i ; N = j ; break ; } } if ( pSq > 0 ) break ; } var d = s - pSq ; var P = ( d / ( 2.0 * N ) ) ; var A = N + P ; var sqrt_of_s = A - ( ( P * P ) / ( 2.0 * A ) ) ; return sqrt_of_s ; } var num = 9.2345 ; var sqroot_of_num = sqroot ( num ) ; document . write ( " " + num + " " + ( Math . round ( sqroot_of_num * 100000.0 ) / 100000.0 ) . toFixed ( 6 ) ) ;
Breaking an Integer to get Maximum Product | Method return x ^ a in log ( a ) time ; Method returns maximum product obtained by breaking N ; Base case 2 = 1 + 1 ; Base case 3 = 2 + 1 ; Breaking based on mod with 3 ; If divides evenly , then break into all 3 ; If division gives mod as 1 , then break as 4 + power of 3 for remaining part ; If division gives mod as 2 , then break as 2 + power of 3 for remaining part ; Driver code
function power ( x , a ) { let res = 1 ; while ( a > 0 ) { if ( ( a & 1 ) > 0 ) res = res * x ; x = x * x ; a >>= 1 ; } return res ; } function breakInteger ( N ) { if ( N == 2 ) return 1 ; if ( N == 3 ) return 2 ; let maxProduct ; switch ( N % 3 ) { case 0 : maxProduct = power ( 3 , N / 3 ) ; break ; case 1 : maxProduct = 2 * 2 * power ( 3 , ( N / 3 ) - 1 ) ; break ; case 2 : maxProduct = 2 * power ( 3 , N / 3 ) ; break ; } return maxProduct ; } let maxProduct = breakInteger ( 10 ) ; document . write ( maxProduct ) ;
Finding sum of digits of a number until sum becomes single digit | Javascript program to find sum of digits of a number until sum becomes single digit . ; Function to get sum of digits ; function call
let n = 1234 ; function getSum ( n ) { let sum = 0 ; while ( n > 0 sum > 9 ) { if ( n == 0 ) { n = sum ; sum = 0 ; } sum = sum + n % 10 ; n = Math . floor ( n / 10 ) ; } return sum ; } document . write ( getSum ( n ) ) ;
Finding sum of digits of a number until sum becomes single digit | Driver code
function digSum ( n ) { if ( n == 0 ) return 0 ; return ( n % 9 == 0 ) ? 9 : ( n % 9 ) ; } n = 9999 ; document . write ( digSum ( n ) ) ;
Multiples of 3 or 7 | Returns count of all numbers smaller than or equal to n and multiples of 3 or 7 or both ; Driver code
function countMultiples ( n ) { return Math . floor ( n / 3 + n / 7 - n / 21 ) ; } document . write ( " " + countMultiples ( 25 ) ) ;
Find Last Digit of a ^ b for Large Numbers | Function to find b % a ; Initialize result ; calculating mod of b with a to make b like 0 <= b < a ; return mod ; return modulo ; function to find last digit of a ^ b ; if a and b both are 0 ; if exponent is 0 ; if base is 0 ; if exponent is divisible by 4 that means last digit will be pow ( a , 4 ) % 10. if exponent is not divisible by 4 that means last digit will be pow ( a , b % 4 ) % 10 ; Find last digit in ' a ' and compute its exponent ; Return last digit of result ; Driver program to run test case
function Modulo ( a , b ) { let mod = 0 ; for ( let i = 0 ; i < b . length ; i ++ ) mod = ( mod * 10 + b [ i ] - ' ' ) % a ; } function LastDigit ( a , b ) { let len_a = a . length ; let len_b = b . length ; if ( len_a == 1 && len_b == 1 && b [ 0 ] == ' ' && a [ 0 ] == ' ' ) return 1 ; if ( len_b == 1 && b [ 0 ] == ' ' ) return 1 ; if ( len_a == 1 && a [ 0 ] == ' ' ) return 0 ; exp = ( Modulo ( 4 , b ) == 0 ) ? 4 : Modulo ( 4 , b ) ; res = Math . pow ( a [ len_a - 1 ] - ' ' , exp ) ; return res % 10 ; } let a = " " ; let b = " " ; document . write ( LastDigit ( a , b ) ) ;
Reverse and Add Function | Iterative function to reverse digits of num ; Function to check whether he number is palindrome or not ; Reverse and Add Function ; Reversing the digits of the number ; Adding the reversed number with the original ; Checking whether the number is palindrome or not ; Driver code
function reversDigits ( num ) { let rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = parseInt ( num / 10 , 10 ) ; } return rev_num ; } function isPalindrome ( num ) { return ( reversDigits ( num ) == num ) ; } function ReverseandAdd ( num ) { let rev_num = 0 ; while ( num <= 4294967295 ) { rev_num = reversDigits ( num ) ; num = num + rev_num ; if ( isPalindrome ( num ) ) { document . write ( num + " " ) ; break ; } else if ( num > 4294967295 ) { document . write ( " " + " " ) ; } } } ReverseandAdd ( 195 ) ; ReverseandAdd ( 265 ) ;
Stein 's Algorithm for finding GCD | Function to implement Stein 's Algorithm ; GCD ( 0 , b ) == b ; GCD ( a , 0 ) == a , GCD ( 0 , 0 ) == 0 ; look for factors of 2 if ( ( ~ a & 1 ) == 1 ) a is even ; if ( ( b & 1 ) == 1 ) b is odd ; else both a and b are even ; a is odd , b is even ; reduce larger number ; Driver Code
function gcd ( a , b ) { if ( a == b ) return a ; if ( a == 0 ) return b ; if ( b == 0 ) return a ; { return gcd ( a >> 1 , b ) ; return gcd ( a >> 1 , b >> 1 ) << 1 ; } if ( ( ~ b & 1 ) == 1 ) return gcd ( a , b >> 1 ) ; if ( a > b ) return gcd ( ( a - b ) >> 1 , b ) ; return gcd ( ( b - a ) >> 1 , a ) ; } let a = 34 , b = 17 ; document . write ( " " + " " + gcd ( a , b ) ) ;
Sub | Array to store the sum of digits ; Utility function to evaluate a character 's  integer value ; This function receives the string representation of the number and precomputes the sum array ; This function receives l and r representing the indices and prints the required output ; Driver Code
let sum = [ ] ; function toInt ( x ) { return x - ' ' ; } function prepareSum ( s ) { sum [ 0 ] = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { sum [ i + 1 ] = sum [ i ] + toInt ( s [ i ] ) ; } } function query ( l , r ) { if ( ( sum [ r + 1 ] - sum [ l ] ) % 3 == 0 ) { document . write ( " " + " " ) ; } else { document . write ( " " + " " ) ; } } let n = " " ; prepareSum ( n ) ; query ( 0 , 1 ) ; query ( 1 , 2 ) ; query ( 3 , 6 ) ; query ( 0 , 10 ) ;
Print all n | n , sum -- > value of inputs out -- > output array index -- > index of next digit to be filled in output array ; Base case ; If number becomes N - digit ; if sum of its digits is equal to given sum , print it ; Traverse through every digit . Note that here we ' re ▁ considering ▁ leading ▁ 0' s as digits ; append current digit to number ; recurse for next digit with reduced sum ; This is mainly a wrapper over findNDigitNumsUtil . It explicitly handles leading digit ; output array to store N - digit numbers ; fill 1 st position by every digit from 1 to 9 and calls findNDigitNumsUtil ( ) for remaining positions ; driver program to test above function
function findNDigitNumsUtil ( n , sum , out , index ) { if ( index > n sum < 0 ) return ; if ( index == n ) { if ( sum == 0 ) { out [ index ] = ' \0 ' ; for ( let i = 0 ; i < out . length ; i ++ ) document . write ( out [ i ] ) ; document . write ( " " ) ; } return ; } for ( let i = 0 ; i <= 9 ; i ++ ) { out [ index ] = String . fromCharCode ( i + ' ' . charCodeAt ( 0 ) ) ; findNDigitNumsUtil ( n , sum - i , out , index + 1 ) ; } } function findNDigitNums ( n , sum ) { let out = new Array ( n + 1 ) ; for ( let i = 0 ; i < n + 1 ; i ++ ) { out [ i ] = false ; } for ( let i = 1 ; i <= 9 ; i ++ ) { out [ 0 ] = String . fromCharCode ( i + ' ' . charCodeAt ( 0 ) ) ; findNDigitNumsUtil ( n , sum - i , out , 1 ) ; } } let n = 2 , sum = 3 ; findNDigitNums ( n , sum ) ;
Program for Armstrong Numbers |
function armstrong ( n ) { let number = new String ( n ) n = number . length let output = 0 for ( let i of number ) output = output + parseInt ( i ) ** n if ( output == parseInt ( number ) ) return ( " " + " " ) else return ( " " + " " ) } document . write ( armstrong ( 153 ) ) document . write ( armstrong ( 120 ) )
Palindromic Primes | A function that returns true only if num contains one digit ; comparison operation is faster than division operation . So using following instead of " return ▁ num ▁ / ▁ 10 ▁ = = ▁ 0 ; " ; A recursive function to find out whether num is palindrome or not . Initially , dupNum contains address of a copy of num . ; Base case ( needed for recursion termination ) : This statement / mainly compares the first digit with the last digit ; This is the key line in this method . Note that all recursive / calls have a separate copy of num , but they all share same copy of dupNum . We divide num while moving up the recursion tree ; The following statements are executed when we move up the recursion call tree ; At this point , if num % 10 contains ith digit from beginning , then ( dupNum ) % 10 contains ith digit from end ; The main function that uses recursive function isPalUtil ( ) to find out whether num is palindrome or not ; If num is negative , make it positive ; Create a separate copy of num , so that modifications made to address dupNum don 't  change the input number.  var dupNum = num; dupNum = num ; Function to generate all primes and checking whether number is palindromic or not ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Prvar all palindromic prime numbers ; checking whether the given number is prime palindromic or not ; Driver function
function oneDigit ( num ) { return ( num >= 0 && num < 10 ) ; } function isPalUtil ( num , dupNum ) { if ( oneDigit ( num ) ) return ( num == ( dupNum ) % 10 ) ; if ( ! isPalUtil ( parseInt ( num / 10 ) , dupNum ) ) return false ; dupNum = parseInt ( dupNum / 10 ) ; return ( num % 10 == ( dupNum ) % 10 ) ; } function isPal ( num ) { if ( num < 0 ) num = - num ; return isPalUtil ( num , dupNum ) ; } function printPalPrimesLessThanN ( n ) { var prime = Array . from ( { length : n + 1 } , ( _ , i ) => true ) ; for ( p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] ) { for ( i = p * 2 ; i <= n ; i += p ) { prime [ i ] = false ; } } } for ( p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] && isPal ( p ) ) { document . write ( p + " " ) ; } } } var n = 100 ; document . write ( ' ' + n + ' ' ) ; printPalPrimesLessThanN ( n ) ;
Almost Prime Numbers | A function to count all prime factors of a given number ; Count the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , count i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; A function to print the first n numbers that are k - almost primes . ; Print this number if it is k - prime ; Increment count of k - primes printed so far ; Driver Code
function countPrimeFactors ( n ) { let count = 0 ; while ( n % 2 == 0 ) { n = n / 2 ; count ++ ; } for ( let i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { n = n / i ; count ++ ; } } if ( n > 2 ) count ++ ; return ( count ) ; } function printKAlmostPrimes ( k , n ) { for ( let i = 1 , num = 2 ; i <= n ; num ++ ) { if ( countPrimeFactors ( num ) == k ) { document . write ( num + " " ) ; i ++ ; } } return ; } let n = 10 , k = 2 ; document . write ( " " + n + " " + k + " " + " " ) ; printKAlmostPrimes ( k , n ) ;
Program to add two fractions | Function to return gcd of a and b ; Function to convert the obtained fraction into it 's simplest form ; Finding gcd of both terms ; Converting both terms into simpler terms by dividing them by common factor ; Function to add two fractions ; Finding gcd of den1 and den2 ; Denominator of final fraction obtained finding LCM of den1 and den2 LCM * GCD = a * b ; Changing the fractions to have same denominator Numerator of the final fraction obtained ; Calling function to convert final fraction into it 's   simplest form ; Driver Code
const gcd = ( a , b ) => { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } const lowest = ( den3 , num3 ) => { let common_factor = gcd ( num3 , den3 ) ; den3 = parseInt ( den3 / common_factor ) ; num3 = parseInt ( num3 / common_factor ) ; document . write ( ` ${ num3 } ${ den3 } ` ) } const addFraction = ( num1 , den1 , num2 , den2 ) => { let den3 = gcd ( den1 , den2 ) ; den3 = ( den1 * den2 ) / den3 ; let num3 = ( ( num1 ) * ( den3 / den1 ) + ( num2 ) * ( den3 / den2 ) ) ; lowest ( den3 , num3 ) ; } let num1 = 1 ; let den1 = 500 ; let num2 = 2 ; let den2 = 1500 ; document . write ( ` ${ num1 } ${ den1 } ${ num2 } ${ den2 } ` ) ; addFraction ( num1 , den1 , num2 , den2 ) ;
The Lazy Caterer 's Problem | This function returns the maximum number of pieces that can be made form pancake using n cuts ; Use the formula ; Driver Code
function findPieces ( n ) { return ( n * ( n + 1 ) ) / 2 + 1 ; } document . write ( findPieces ( 1 ) + " " ) ; document . write ( findPieces ( 2 ) + " " ) ; document . write ( findPieces ( 3 ) + " " ) ; document . write ( findPieces ( 50 ) ) ;
Count digits in a factorial | Set 2 | Returns the number of digits present in n ! Since the result can be large long long is used as return type ; factorial of - ve number doesn 't exists ; base case ; Use Kamenetsky formula to calculate the number of digits ; Driver Code
function findDigits ( n ) { if ( n < 0 ) return 0 ; if ( n <= 1 ) return 1 ; let x = ( ( n * Math . log10 ( n / Math . E ) + Math . log10 ( 2 * Math . PI * n ) / 2.0 ) ) ; return Math . floor ( x ) + 1 ; } document . write ( findDigits ( 1 ) + " " ) ; document . write ( findDigits ( 50000000 ) + " " ) ; document . write ( findDigits ( 1000000000 ) + " " ) ; document . write ( findDigits ( 120 ) + " " ) ;
Count digits in a factorial | Set 1 | This function receives an integer n , and returns the number of digits present in n ! ; factorial exists only for n >= 0 ; base case ; else iterate through n and calculate the value ; Driver code
function findDigits ( n ) { if ( n < 0 ) return 0 ; if ( n <= 1 ) return 1 ; let digits = 0 ; for ( let i = 2 ; i <= n ; i ++ ) digits += Math . log10 ( i ) ; return Math . floor ( digits ) + 1 ; } document . write ( findDigits ( 1 ) + " " ) ; document . write ( findDigits ( 5 ) + " " ) ; document . write ( findDigits ( 10 ) + " " ) ; document . write ( findDigits ( 120 ) + " " ) ;
Find number of subarrays with even sum | Javascript program to count number of sub - arrays whose sum is even using brute force Time Complexity - O ( N ^ 2 ) Space Complexity - O ( 1 ) ; Find sum of all subarrays and increment result if sum is even ; Driver Code
function countEvenSum ( arr , n ) { let result = 0 ; for ( let i = 0 ; i <= n - 1 ; i ++ ) { let sum = 0 ; for ( let j = i ; j <= n - 1 ; j ++ ) { sum = sum + arr [ j ] ; if ( sum % 2 == 0 ) result ++ ; } } return ( result ) ; } let arr = [ 1 , 2 , 2 , 3 , 4 , 1 ] ; let n = arr . length ; document . write ( " " + " " ) ; document . write ( countEvenSum ( arr , n ) ) ;
Find number of subarrays with even sum | Javascript program to count number of sub - arrays with even sum using an efficient algorithm Time Complexity - O ( N ) Space Complexity - O ( 1 ) ; 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 even 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 ; Use handshake lemma to count even subarrays ( Note that an even cam be formed by two even or two odd ) ; Driver code
function countEvenSum ( arr , n ) { let temp = [ 1 , 0 ] ; let result = 0 , sum = 0 ; for ( let i = 0 ; i <= n - 1 ; i ++ ) { sum = ( ( sum + arr [ i ] ) % 2 + 2 ) % 2 ; temp [ sum ] ++ ; } result = result + ( temp [ 0 ] * ( temp [ 0 ] - 1 ) / 2 ) ; result = result + ( temp [ 1 ] * ( temp [ 1 ] - 1 ) / 2 ) ; return ( result ) ; } let arr = [ 1 , 2 , 2 , 3 , 4 , 1 ] ; let n = arr . length ; document . write ( " " + " " + countEvenSum ( arr , n ) ) ;
Sum of Fibonacci Numbers | Computes value of first fibonacci numbers ; Initialize result ; Add remaining terms ; Driver Code
function calculateSum ( n ) { let fibo = [ ] ; if ( n <= 0 ) return 0 ; fibo [ 0 ] = 0 ; fibo [ 1 ] = 1 ; let sum = fibo [ 0 ] + fibo [ 1 ] ; for ( let i = 2 ; i <= n ; i ++ ) { fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] ; sum += fibo [ i ] ; } return sum ; } let n = 4 ; document . write ( ` ${ calculateSum ( n ) } ` ) ;
Find all combinations that add upto given number | arr - array to store the combination index - next location in array num - given number reducedNum - reduced number ; Base condition ; If combination is found , print it ; Find the previous number stored in arr [ ] . It helps in maintaining increasing order ; note loop starts from previous number i . e . at array location index - 1 ; next element of array is k ; call recursively with reduced number ; Function to find out all combinations of positive numbers that add upto given number . It uses findCombinationsUtil ( ) ; array to store the combinations It can contain max n elements ; find all combinations ; Driver Code
function findCombinationsUtil ( arr , index , num , reducedNum ) { if ( reducedNum < 0 ) return ; if ( reducedNum == 0 ) { for ( let i = 0 ; i < index ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ; return ; } let prev = ( index == 0 ) ? 1 : arr [ index - 1 ] ; for ( let k = prev ; k <= num ; k ++ ) { arr [ index ] = k ; findCombinationsUtil ( arr , index + 1 , num , reducedNum - k ) ; } } function findCombinations ( n ) { let arr = [ ] ; findCombinationsUtil ( arr , 0 , n , n ) ; } let n = 5 ; findCombinations ( n ) ;
Find Square Root under Modulo p | Set 2 ( Shanks Tonelli algorithm ) | JavaScript program to implement Shanks Tonelli algorithm for finding Modular Square Roots ; utility function to find pow ( base , exponent ) % modulus ; utility function to find gcd ; Returns k such that b ^ k = 1 ( mod p ) ; Initializing k with first odd prime number ; function return p - 1 ( = x argument ) as x * 2 ^ e , where x will be odd sending e as reference because updation is needed in actual e ; Main function for finding the modular square root ; a and p should be coprime for finding the modular square root ; If below expression return ( p - 1 ) then modular square root is not possible ; expressing p - 1 , in terms of s * 2 ^ e , where s is odd number ; finding smallest q such that q ^ ( ( p - 1 ) / 2 ) ( mod p ) = p - 1 ; q - 1 is in place of ( - 1 % p ) ; Initializing variable x , b and g ; keep looping until b become 1 or m becomes 0 ; finding m such that b ^ ( 2 ^ m ) = 1 ; updating value of x , g and b according to algorithm ; Driver Code ; p should be prime
let z = 0 ; function pow1 ( base1 , exponent , modulus ) { let result = 1 ; base1 = base1 % modulus ; while ( exponent > 0 ) { if ( exponent % 2 == 1 ) result = ( result * base1 ) % modulus ; exponent = exponent >> 1 ; base1 = ( base1 * base1 ) % modulus ; } return result ; } function gcd ( a , b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } function order ( p , b ) { if ( gcd ( p , b ) != 1 ) { document . write ( " " + " " + " " ) ; return - 1 ; } let k = 3 ; while ( true ) { if ( pow1 ( b , k , p ) == 1 ) return k ; k ++ ; } } function convertx2e ( x ) { z = 0 ; while ( x % 2 == 0 ) { x /= 2 ; z ++ ; } return x ; } function STonelli ( n , p ) { if ( gcd ( n , p ) != 1 ) { System . out . prletln ( " " ) ; return - 1 ; } if ( pow1 ( n , ( p - 1 ) / 2 , p ) == ( p - 1 ) ) { document . write ( " " + " " ) ; return - 1 ; } let s , e ; s = convertx2e ( p - 1 ) ; e = z ; let q ; for ( q = 2 ; ; q ++ ) { if ( pow1 ( q , ( p - 1 ) / 2 , p ) == ( p - 1 ) ) break ; } let x = pow1 ( n , ( s + 1 ) / 2 , p ) ; let b = pow1 ( n , s , p ) ; let g = pow1 ( q , s , p ) ; let r = e ; while ( true ) { let m ; for ( m = 0 ; m < r ; m ++ ) { if ( order ( p , b ) == - 1 ) return - 1 ; if ( order ( p , b ) == Math . pow ( 2 , m ) ) break ; } if ( m == 0 ) return x ; x = ( x * pow1 ( g , Math . pow ( 2 , r - m - 1 ) , p ) ) % p ; g = pow1 ( g , Math . pow ( 2 , r - m ) , p ) ; b = ( b * g ) % p ; if ( b == 1 ) return x ; r = m ; } } let n = 2 ; let p = 113 ; let x = STonelli ( n , p ) ; if ( x == - 1 ) document . write ( " " + " " ) ; else document . write ( " " + n + " " + p + " " + x + " " ) ;
Check if a number is a power of another number | javascript program to check given number y is power of x { ; logarithm function to calculate value ; Note : this is var ; compare to the result1 or result2 both are equal ; Driver Code
function isPower ( x , y ) { var res1 = parseInt ( Math . log ( y ) ) / parseInt ( Math . log ( x ) ) ; var res2 = Math . log ( y ) / Math . log ( x ) ; return ( res1 == res2 ) ; } if ( isPower ( 27 , 729 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Program to find the Roots of Quadratic equation | Prints roots of quadratic equation ax * 2 + bx + x ; If a is 0 , then equation is not quadratic , but linear ; else d < 0 ; Driver Code ; Function call
function findRoots ( a , b , c ) { if ( a == 0 ) { document . write ( " " ) ; return ; } let d = b * b - 4 * a * c ; let sqrt_val = Math . sqrt ( Math . abs ( d ) ) ; if ( d > 0 ) { document . write ( " " + " " ) ; document . write ( ( - b + sqrt_val ) / ( 2 * a ) + " " + ( - b - sqrt_val ) / ( 2 * a ) ) ; } else if ( d == 0 ) { document . write ( " " + " " ) ; document . write ( - b / ( 2 * a ) + " " + - b / ( 2 * a ) ) ; } { document . write ( " " ) ; document . write ( - b / ( 2 * a ) + " " + sqrt_val + " " + - b / ( 2 * a ) + " " + sqrt_val ) ; } } let a = 1 , b = - 7 , c = 12 ; findRoots ( a , b , c ) ;
Check perfect square using addition / subtraction | This function returns true if n is perfect square , else false ; sum is sum of all odd numbers . i is used one by one hold odd numbers ; Driver Code
function isPerfectSquare ( n ) { for ( let sum = 0 , i = 1 ; sum < n ; i += 2 ) { sum += i ; if ( sum == n ) return true ; } return false ; } if ( isPerfectSquare ( 35 ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; if ( isPerfectSquare ( 49 ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;
Count ' d ' digit positive integers with 0 as a digit | Returns count of ' d ' digit integers have 0 as a digit ; Driver Code
function findCount ( d ) { return 9 * ( ( Math . pow ( 10 , d - 1 ) ) - ( Math . pow ( 9 , d - 1 ) ) ) ; } let d = 1 ; document . write ( findCount ( d ) + " " ) ; d = 2 ; document . write ( findCount ( d ) + " " ) ; d = 4 ; document . write ( findCount ( d ) + " " ) ;
Dyck path | Returns count Dyck paths in n x n grid ; Compute value of 2 nCn ; return 2 nCn / ( n + 1 ) ; Driver Code
function countDyckPaths ( n ) { let res = 1 ; for ( let i = 0 ; i < n ; ++ i ) { res *= ( 2 * n - i ) ; res /= ( i + 1 ) ; } return res / ( n + 1 ) ; } let n = 4 ; document . write ( " " + countDyckPaths ( n ) ) ;
Triangular Numbers | Returns true if ' num ' is triangular , else false ; Base case ; A Triangular number must be sum of first n natural numbers ; Driver code
function isTriangular ( num ) { if ( num < 0 ) return false ; let sum = 0 ; for ( let n = 1 ; sum <= num ; n ++ ) { sum = sum + n ; if ( sum == num ) return true ; } return false ; } let n = 55 ; if ( isTriangular ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Triangular Numbers | Returns true if num is triangular ; Considering the equation n * ( n + 1 ) / 2 = num The equation is : a ( n ^ 2 ) + bn + c = 0 "; ; Find roots of equation ; checking if root1 is natural ; checking if root2 is natural ; Driver code
function isTriangular ( num ) { if ( num < 0 ) return false ; var c = ( - 2 * num ) ; var b = 1 , a = 1 ; var d = ( b * b ) - ( 4 * a * c ) ; if ( d < 0 ) return false ; var root1 = ( - b + Math . sqrt ( d ) ) / ( 2 * a ) ; var root2 = ( - b - Math . sqrt ( d ) ) / ( 2 * a ) ; if ( root1 > 0 && Math . floor ( root1 ) == root1 ) return true ; if ( root2 > 0 && Math . floor ( root2 ) == root2 ) return true ; return false ; } var num = 55 ; if ( isTriangular ( num ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;