codeparrot/xlcost-text-to-code huggingface dataset:
System of Linear Equations in three variables using Cramer 's Rule | This functions finds the determinant of Matrix ; This function finds the solution of system of linear equations using cramer 's rule ; Matrix d using coeff as given in cramer 's rule ; Matrix d1 using coeff as given in cramer 's rule ; Matrix d2 using coeff as given in cramer 's rule  ; Matrix d3 using coeff as given in cramer 's rule ; Calculating Determinant of Matrices d , d1 , d2 , d3 ; Case 1 ; Coeff have a unique solution . Apply Cramer 's Rule ; let z = D3 / D ; calculating z using cramer 's rule ; Case 2 ;
function determinantOfMatrix ( mat ) { let ans ; ans = mat [ 0 ] [ 0 ] * ( mat [ 1 ] [ 1 ] * mat [ 2 ] [ 2 ] - mat [ 2 ] [ 1 ] * mat [ 1 ] [ 2 ] ) - mat [ 0 ] [ 1 ] * ( mat [ 1 ] [ 0 ] * mat [ 2 ] [ 2 ] - mat [ 1 ] [ 2 ] * mat [ 2 ] [ 0 ] ) + mat [ 0 ] [ 2 ] * ( mat [ 1 ] [ 0 ] * mat [ 2 ] [ 1 ] - mat [ 1 ] [ 1 ] * mat [ 2 ] [ 0 ] ) ; return ans ; } function findSolution ( coeff ) { let d = [ [ coeff [ 0 ] [ 0 ] , coeff [ 0 ] [ 1 ] , coeff [ 0 ] [ 2 ] ] , [ coeff [ 1 ] [ 0 ] , coeff [ 1 ] [ 1 ] , coeff [ 1 ] [ 2 ] ] , [ coeff [ 2 ] [ 0 ] , coeff [ 2 ] [ 1 ] , coeff [ 2 ] [ 2 ] ] ] ; let d1 = [ [ coeff [ 0 ] [ 3 ] , coeff [ 0 ] [ 1 ] , coeff [ 0 ] [ 2 ] ] , [ coeff [ 1 ] [ 3 ] , coeff [ 1 ] [ 1 ] , coeff [ 1 ] [ 2 ] ] , [ coeff [ 2 ] [ 3 ] , coeff [ 2 ] [ 1 ] , coeff [ 2 ] [ 2 ] ] ] ; let d2 = [ [ coeff [ 0 ] [ 0 ] , coeff [ 0 ] [ 3 ] , coeff [ 0 ] [ 2 ] ] , [ coeff [ 1 ] [ 0 ] , coeff [ 1 ] [ 3 ] , coeff [ 1 ] [ 2 ] ] , [ coeff [ 2 ] [ 0 ] , coeff [ 2 ] [ 3 ] , coeff [ 2 ] [ 2 ] ] ] ; let d3 = [ [ coeff [ 0 ] [ 0 ] , coeff [ 0 ] [ 1 ] , coeff [ 0 ] [ 3 ] ] , [ coeff [ 1 ] [ 0 ] , coeff [ 1 ] [ 1 ] , coeff [ 1 ] [ 3 ] ] , [ coeff [ 2 ] [ 0 ] , coeff [ 2 ] [ 1 ] , coeff [ 2 ] [ 3 ] ] ] ; let D = determinantOfMatrix ( d ) ; let D1 = determinantOfMatrix ( d1 ) ; let D2 = determinantOfMatrix ( d2 ) ; let D3 = determinantOfMatrix ( d3 ) ; document . write ( " " , D . toFixed ( 6 ) + " " ) ; document . write ( " " , D1 . toFixed ( 6 ) + " " ) ; document . write ( " " , D2 . toFixed ( 6 ) + " " ) ; document . write ( " " , D3 . toFixed ( 6 ) + " " ) ; if ( D != 0 ) { let x = D1 / D ; let y = D2 / D ; document . write ( " " , x . toFixed ( 6 ) + " " ) ; document . write ( " " , y . toFixed ( 6 ) + " " ) ; document . write ( " " , z . toFixed ( 6 ) + " " ) ; } else { if ( D1 == 0 && D2 == 0 && D3 == 0 ) document . write ( " " ) ; else if ( D1 != 0 D2 != 0 D3 != 0 ) document . write ( " " ) ; } } ' ' ' ' ' ' let coeff = [ [ 2 , - 1 , 3 , 9 ] , [ 1 , 1 , 1 , 6 ] , [ 1 , - 1 , 1 , 2 ] ] findSolution ( coeff ) ;
Find larger of x ^ y and y ^ x | Javascript program to print greater of x ^ y and y ^ x ; Driver Code
function printGreater ( x , y ) { let X = y * Math . log ( x ) ; let Y = x * Math . log ( y ) ; if ( Math . abs ( X - Y ) < 1e-9 ) { document . write ( " " ) ; } else if ( X > Y ) { document . write ( x + " " + y ) ; } else { document . write ( y + " " + x ) ; } } let x = 5 , y = 8 ; printGreater ( x , y ) ;
n | Function to find nth term ; Driver code
function sumOfSeries ( n ) { return n * ( n + 1 ) * ( 6 * n * n * n + 9 * n * n + n - 1 ) / 30 ; } let n = 4 ; document . write ( sumOfSeries ( n ) ) ;
Product of first N factorials | To compute ( a * b ) % MOD ; let res = 0 ; Initialize result ; If b is odd , add ' a ' to result ; Multiply ' a ' with 2 ; Divide b by 2 ; Return result ; This function computes factorials and product by using above function i . e . modular multiplication ; Initialize product and fact with 1 ; ith factorial ; product of first i factorials ; If at any iteration , product becomes divisible by MOD , simply return 0 ; ; Driver Code
function mulmod ( a , b , mod ) { a = a % mod ; while ( b > 0 ) { if ( b % 2 == 1 ) res = ( res + a ) % mod ; a = ( a * 2 ) % mod ; b = parseInt ( b / 2 , 10 ) ; } return res % mod ; } function findProduct ( N ) { let product = 1 , fact = 1 ; let MOD = ( 1e9 + 7 ) ; for ( let i = 1 ; i <= N ; i ++ ) { fact = mulmod ( fact , i , MOD ) ; product = mulmod ( product , fact , MOD ) ; if ( product == 0 ) return 0 ; } return product ; } let N = 3 ; document . write ( findProduct ( N ) + " " ) ; N = 5 ; document . write ( findProduct ( N ) ) ;
Check if sum of divisors of two numbers are same | Function to calculate sum of all proper divisors num -- > given natural number ; To store sum of divisors ; Find all divisors and add them ; Function to check if both numbers are equivalent or not ; Driver code
function divSum ( n ) { let sum = 1 ; for ( let i = 2 ; i * i <= n ; i ++ ) if ( n % i == 0 ) sum = sum + i + parseInt ( n / i , 10 ) ; return sum ; } function areEquivalent ( num1 , num2 ) { return divSum ( num1 ) == divSum ( num2 ) ; } let num1 = 559 ; let num2 = 703 ; if ( areEquivalent ( num1 , num2 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Dodecahedral number | Function to find dodecahedral number ; Formula to calculate nth dodecahedral number and return it into main function . ; Driver code ; print result
function dodecahedral_num ( n ) { return n * ( 3 * n - 1 ) * ( 3 * n - 2 ) / 2 ; } var n = 5 ; document . write ( n + " " + " " ) ; document . write ( dodecahedral_num ( n ) ) ;
Count of divisors having more set bits than quotient on dividing N | Return the count of set bit . ; check if q and d have same number of set bit . ; Binary Search to find the point at which number of set in q is less than or equal to d . ; while left index is less than right index ; finding the middle . ; check if q and d have same number of set it or not . ; Driver Code
function bit ( x ) { let ans = 0 ; while ( x > 0 ) { x = parseInt ( x / 2 , 10 ) ; ans ++ ; } return ans ; } function check ( d , x ) { if ( bit ( parseInt ( x / d , 10 ) ) <= bit ( d ) ) return true ; return false ; } function bs ( n ) { let l = 1 , r = Math . sqrt ( n ) ; while ( l < r ) { let m = parseInt ( ( l + r ) / 2 , 10 ) ; if ( check ( m , n ) ) r = m ; else l = m + 1 ; } if ( ! check ( l , n ) ) return l + 1 ; else return l ; } function countDivisor ( n ) { return n - bs ( n ) + 1 ; } let n = 5 ; document . write ( countDivisor ( n ) ) ;
Check if two people starting from different points ever meet | Javascript program to find if two people starting from different positions ever meet or not . ; If speed of a person at a position before other person is smaller , then return false . ; Making sure that x1 is greater ; Until one person crosses other ; first person taking one jump in each iteration ; second person taking one jump in each iteration ; Driver code
function swap ( a , b ) { let t = a ; a = b ; b = t ; } function everMeet ( x1 , x2 , v1 , v2 ) { if ( x1 < x2 && v1 <= v2 ) return false ; if ( x1 > x2 && v1 >= v2 ) return false ; if ( x1 < x2 ) { swap ( x1 , x2 ) ; swap ( v1 , v2 ) ; } while ( x1 >= x2 ) { if ( x1 == x2 ) return true ; x1 = x1 + v1 ; x2 = x2 + v2 ; } return false ; } let x1 = 5 , v1 = 8 , x2 = 4 , v2 = 7 ; if ( everMeet ( x1 , x2 , v1 , v2 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Divisibility by 3 where each digit is the sum of all prefix digits modulo 10 | Function to check the divisibility ; Cycle ; no of residual terms ; sum of residual terms ; if no of residue term = 0 ; if no of residue term = 1 ; if no of residue term = 2 ; if no of residue term = 3 ; sum of all digits ; divisibility check ; Driver code
function check ( k , d0 , d1 ) { let s = ( 2 * ( d0 + d1 ) ) % 10 + ( 4 * ( d0 + d1 ) ) % 10 + ( 8 * ( d0 + d1 ) ) % 10 + ( 6 * ( d0 + d1 ) ) % 10 ; let a = ( k - 3 ) % 4 ; let x ; switch ( a ) { case 0 : x = 0 ; break ; case 1 : x = ( 2 * ( d0 + d1 ) ) % 10 ; break ; case 2 : x = ( 2 * ( d0 + d1 ) ) % 10 + ( 4 * ( d0 + d1 ) ) % 10 ; break ; case 3 : x = ( 2 * ( d0 + d1 ) ) % 10 + ( 4 * ( d0 + d1 ) ) % 10 + ( 8 * ( d0 + d1 ) ) % 10 ; break ; } let sum = d0 + d1 + parseInt ( ( k - 3 ) / 4 ) * s + x ; if ( sum % 3 == 0 ) return " " ; return " " ; } let k , d0 , d1 ; k = 13 ; d0 = 8 ; d1 = 1 ; document . write ( check ( k , d0 , d1 ) + " " ) ; k = 5 ; d0 = 3 ; d1 = 4 ; document . write ( check ( k , d0 , d1 ) + " " ) ;
Find ceil of a / b without using ceil ( ) function | taking input 1 ; example of perfect division taking input 2
var a = 4 ; var b = 3 , val = 0 ; if ( ( a % b ) != 0 ) val = parseInt ( a / b ) + ( a % b ) ; else val = parseInt ( a / b ) ; document . write ( " " + " " + val + " " ) ; a = 6 ; b = 3 ; if ( ( a % b ) != 0 ) val = parseInt ( a / b ) + ( a % b ) ; else val = parseInt ( a / b ) ; document . write ( " " + " " + val ) ;
Program to print Collatz Sequence | Javascript program to print Collatz sequence ; We simply follow steps while we do not reach 1 ; If n is odd ; If even ; Print 1 at the end ; Driver code
function printCollatz ( n ) { while ( n != 1 ) { document . write ( n + " " ) ; if ( ( n & 1 ) != 0 ) n = 3 * n + 1 ; else n = parseInt ( n / 2 , 10 ) ; } document . write ( n ) ; } printCollatz ( 6 ) ;
Powers of 2 to required sum | Javascript program to find the blocks for given number . ; Convert decimal number to its binary equivalent ; Displaying the output when the bit is '1' in binary equivalent of number . ; Driver code
function block ( x ) { let v = [ ] ; document . write ( " " + x + " " ) ; while ( x > 0 ) { v . push ( x % 2 ) ; x = parseInt ( x / 2 , 10 ) ; } for ( let i = 0 ; i < v . length ; i ++ ) { if ( v [ i ] == 1 ) { document . write ( i ) ; if ( i != v . length - 1 ) document . write ( " " ) ; } } document . write ( " " ) ; } block ( 71307 ) ; block ( 1213 ) ; block ( 29 ) ; block ( 100 ) ;
Given a number N in decimal base , find number of its digits in any base ( base b ) | function to print number of digits ; Calculating log using base changing property and then taking it floor and then adding 1. ; printing output ; taking inputs ; calling the method
function findNumberOfDigits ( n , base ) { var dig = parseInt ( Math . floor ( Math . log ( n ) / Math . log ( base ) ) + 1 ) ; document . write ( " " + " " + n + " " + base + " " + dig ) ; } var n = 1446 ; var base = 7 ; findNumberOfDigits ( n , base ) ;
Nesbitt 's Inequality | Javascript code to verify Nesbitt 's Inequality ; 3 parts of the inequality sum ; Driver code
function isValidNesbitt ( a , b , c ) { let A = a / ( b + c ) ; let B = b / ( a + c ) ; let C = c / ( a + b ) ; let inequality = A + B + C ; return ( inequality >= 1.5 ) ; } let a = 1.0 , b = 2.0 , c = 3.0 ; if ( isValidNesbitt ( a , b , c ) == true ) { document . write ( " " + " " ) ; document . write ( " " + a + " " + b + " " + c ) ; } else document . write ( " " + " " ) ;
Cube Free Numbers smaller than n | Efficient Javascript program to print all cube free numbers smaller than or equal to n . ; Initialize all numbers as not cube free ; Traverse through all possible cube roots ; If i itself is cube free ; Mark all multiples of i as not cube free ; Print all cube free numbers ; Driver code
function printCubeFree ( n ) { var cubFree = Array ( n + 1 ) . fill ( false ) ; for ( i = 0 ; i <= n ; i ++ ) cubFree [ i ] = true ; for ( i = 2 ; i * i * i <= n ; i ++ ) { if ( cubFree [ i ] ) { for ( multiple = 1 ; i * i * i * multiple <= n ; multiple ++ ) { cubFree [ i * i * i * multiple ] = false ; } } } for ( i = 2 ; i <= n ; i ++ ) { if ( cubFree [ i ] == true ) document . write ( i + " " ) ; } } printCubeFree ( 20 ) ;
Heap Sort for decreasing order using min heap | To heapify a subtree rooted with node i which is an index in arr [ ] . n is size of heap ; Initialize smalles as root ; left = 2 * i + 1 ; right = 2 * i + 2 ; If left child is smaller than root ; If right child is smaller than smallest so far ; If smallest is not root ; Recursively heapify the affected sub - tree ; main function to do heap sort ; Build heap ( rearrange array ) ; One by one extract an element from heap ; Move current root to end ; call max heapify on the reduced heap ; A utility function to print array of size n ; Driver program
function heapify ( arr , n , i ) { var smallest = i ; var l = 2 * i + 1 ; var r = 2 * i + 2 ; if ( l < n && arr [ l ] < arr [ smallest ] ) smallest = l ; if ( r < n && arr [ r ] < arr [ smallest ] ) smallest = r ; if ( smallest != i ) { [ arr [ i ] , arr [ smallest ] ] = [ arr [ smallest ] , arr [ i ] ] heapify ( arr , n , smallest ) ; } } function heapSort ( arr , n ) { for ( var i = parseInt ( n / 2 - 1 ) ; i >= 0 ; i -- ) heapify ( arr , n , i ) ; for ( var i = n - 1 ; i >= 0 ; i -- ) { [ arr [ 0 ] , arr [ i ] ] = [ arr [ i ] , arr [ 0 ] ] heapify ( arr , i , 0 ) ; } } function printArray ( arr , n ) { for ( var i = 0 ; i < n ; ++ i ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ; } var arr = [ 4 , 6 , 3 , 2 , 9 ] ; var n = arr . length ; heapSort ( arr , n ) ; document . write ( " " ) ; printArray ( arr , n ) ;
Squared triangular number ( Sum of cubes ) | Returns root of n ( n + 1 ) / 2 = num if num is triangular ( or integer root exists ) . Else returns - 1. ; 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 ; Returns square root of x if it is perfect square . Else returns - 1. ; Find floating point value of square root of x . ; If square root is an integer ; Function to find if the given number is sum of the cubes of first n natural numbers ; Driver code
function isTriangular ( num ) { if ( num < 0 ) return 0 ; var c = ( - 2 * num ) ; var b = 1 , a = 1 ; var d = ( b * b ) - ( 4 * a * c ) ; if ( d < 0 ) return - 1 ; var root1 = ( - b + Math . sqrt ( d ) ) / ( 2 * a ) ; var root2 = ( - b - Math . sqrt ( d ) ) / ( 2 * a ) ; if ( parseInt ( ( root1 ) ) > 0 && parseInt ( ( Math . floor ( root1 ) ) ) == parseInt ( ( root1 ) ) ) return parseInt ( root1 ) ; if ( parseInt ( ( root2 ) ) > 0 && parseInt ( ( Math . floor ( root2 ) ) ) == parseInt ( ( root2 ) ) ) return parseInt ( ( root2 ) ) ; return - 1 ; } function isPerfectSquare ( x ) { var sr = Math . sqrt ( x ) ; if ( ( sr - Math . floor ( sr ) ) == 0 ) return parseInt ( ( Math . floor ( sr ) ) ) ; else return - 1 ; } function findS ( s ) { var sr = isPerfectSquare ( s ) ; if ( sr == - 1 ) return - 1 ; return isTriangular ( sr ) ; } var s = 9 ; var n = findS ( s ) ; if ( n == - 1 ) document . write ( " " ) ; else document . write ( n ) ;
Smallest even digits number not less than N | function to return the answer when the first odd digit is 9 ; traverse towwars the left to find the non - 8 digit ; index digit ; if digit is not 8 , then break ; if on the left side of the '9' , no 8 is found then we return by adding a 2 and 0 's ; till non - 8 digit add all numbers ; if non - 8 is even or odd than add the next even . ; add 0 to right of 9 ; function to return the smallest number with all digits even ; convert the number to string to perform operations ; find out the first odd number ; if no odd numbers are there , than n is the answer ; if the odd number is 9 , than tricky case handles it ; add all digits till first odd ; increase the odd digit by 1 ; add 0 to the right of the odd number ; Driver Code
function trickyCase ( s , index ) { let index1 = - 1 ; for ( let i = index - 1 ; i >= 0 ; i -- ) { let digit = s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; if ( digit != 8 ) { index1 = i ; break ; } } if ( index1 == - 1 ) return 2 * Math . pow ( 10 , s . length ) ; let num = 0 ; for ( let i = 0 ; i < index1 ; i ++ ) num = num * 10 + ( s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) ; if ( s [ index1 ] . charCodeAt ( ) % 2 == 0 ) num = num * 10 + ( s [ index1 ] . charCodeAt ( ) - ' ' . charCodeAt ( ) + 2 ) ; else num = num * 10 + ( s [ index1 ] . charCodeAt ( ) - ' ' . charCodeAt ( ) + 1 ) ; for ( let i = index1 + 1 ; i < s . length ; i ++ ) num = num * 10 ; return num ; } function smallestNumber ( n ) { let num = 0 ; let s = " " ; let duplicate = n ; while ( n > 0 ) { s = String . fromCharCode ( n % 10 + 48 ) + s ; n = parseInt ( n / 10 , 10 ) ; } let index = - 1 ; for ( let i = 0 ; i < s . length ; i ++ ) { let digit = s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; let val = digit & 1 ; if ( val == 1 ) { index = i ; break ; } } if ( index == - 1 ) return duplicate ; if ( s [ index ] == ' ' ) { num = trickyCase ( s , index ) ; return num ; } for ( let i = 0 ; i < index ; i ++ ) num = num * 10 + ( s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) ; num = num * 10 + ( s [ index ] . charCodeAt ( ) - ' ' . charCodeAt ( ) + 1 ) ; for ( let i = index + 1 ; i < s . length ; i ++ ) num = num * 10 ; return num ; } let N = 2397 ; document . write ( smallestNumber ( N ) ) ;
n | Simple Javascript program to find n - th number with sum of digits as 10. ; Find sum of digits in current no . ; If sum is 10 , we increment count ; If count becomes n , we return current number . ; Driver Code
function findNth ( n ) { let count = 0 ; for ( let curr = 19 ; ; curr += 9 ) { let sum = 0 ; for ( let x = curr ; x > 0 ; x = parseInt ( x / 10 ) ) sum = sum + x % 10 ; if ( sum == 10 ) count ++ ; if ( count == n ) return curr ; } return - 1 ; } document . write ( findNth ( 5 ) ) ;
Sum of pairwise products | Simple JavaScript program to find sum of given series . ; Driver Code
function findSum ( n ) { let sum = 0 ; for ( let i = 1 ; i <= n ; i ++ ) for ( let j = i ; j <= n ; j ++ ) sum = sum + i * j ; return sum ; } let n = 5 ; document . write ( findSum ( n ) ) ;
Sum of pairwise products | Javascript program to find sum of given series . ; Sum of multiples of 1 is 1 * ( 1 + 2 + . . ) ; Adding sum of multiples of numbers other than 1 , starting from 2. ; Subtract previous number from current multiple . ; For example , for 2 , we get sum as ( 2 + 3 + 4 + ... . ) * 2 ; Driver code
function findSum ( n ) { let multiTerms = n * ( n + 1 ) / 2 ; let sum = multiTerms ; for ( let i = 2 ; i <= n ; i ++ ) { multiTerms = multiTerms - ( i - 1 ) ; sum = sum + multiTerms * i ; } return sum ; } let n = 5 ; document . write ( findSum ( n ) ) ;
Sum of n digit numbers divisible by a given number | Find the Sum of having n digit and divisible by the number ; Compute the first and last term ; First number which is divisible by given number ; Last number which is divisible by given number ; Total divisible number ; Return the total sum ; Driver Code
function totalSumDivisibleByNum ( digit , number ) { let firstnum = Math . pow ( 10 , digit - 1 ) ; let lastnum = Math . pow ( 10 , digit ) ; firstnum = ( firstnum - firstnum % number ) + number ; lastnum = ( lastnum - lastnum % number ) ; let count = ( ( lastnum - firstnum ) / number + 1 ) ; return ( ( lastnum + firstnum ) * count ) / 2 ; } let n = 3 , number = 7 ; document . write ( totalSumDivisibleByNum ( n , number ) ) ;
Program for N | JavaScript Program to find nth term of Arithmetic progression ; using formula to find the Nth term t ( n ) = a ( 1 ) + ( n - 1 ) * d ; starting number ; Common difference ; N th term to be find ; Display the output
function Nth_of_AP ( a , d , N ) { return ( a + ( N - 1 ) * d ) ; } let a = 2 ; let d = 1 ; let N = 5 ; document . write ( " " + N + " " + Nth_of_AP ( a , d , N ) ) ;
Fibbinary Numbers ( No consecutive 1 s in binary ) | function to check if binary representation of an integer has consecutive 1 s ; stores the previous last bit initially as 0 ; if current last bit and previous last bit is 1 ; stores the last bit ; right shift the number ; Driver code to check above function
function checkFibinnary ( n ) { var prev_last = 0 ; while ( n != 0 ) { if ( ( n & 1 ) != 0 && prev_last != 0 ) return false ; prev_last = n & 1 ; n >>= 1 ; } return true ; } var n = 10 ; if ( checkFibinnary ( n ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
Sum of the series 5 + 55 + 555 + . . up to n terms | function which return the the sum of series ; Driver code
function sumOfSeries ( n ) { return parseInt ( ( 0.6172 * ( Math . pow ( 10 , n ) - 1 ) - 0.55 * n ) ) ; } var n = 2 ; document . write ( sumOfSeries ( n ) ) ;
Nonagonal number | Function to find nth nonagonal number . ; Formula to find nth nonagonal number . ; Driver function .
function Nonagonal ( n ) { return parseInt ( n * ( 7 * n - 5 ) / 2 ) ; } let n = 10 ; document . write ( Nonagonal ( n ) ) ;
Check if a large number is divisible by 20 | Javascript program to check if a large number is divisible by 20. ; Get number with last two digits ; Check if the number formed by last two digits is divisible by 5 and 4. ; Driver Code
function divisibleBy20 ( num ) { let lastTwoDigits = parseInt ( num . slice ( - 2 , num . length ) ) console . log ( num . slice ( - 2 , 1 ) ) return ( ( lastTwoDigits % 5 == 0 ) && ( lastTwoDigits % 4 == 0 ) ) } let num = " " ; if ( divisibleBy20 ( num ) ) document . write ( " " ) ; else document . write ( " " ) ;
Divisibility by 12 for a large number | Javascript program to check if number is divisible by 12 ; If number greater then 3 ; Find last digit ; No is odd ; Find second last digit ; Find sum of all digits ; If number is less then or equal to 100 ; Driver code
function isDvisibleBy12 ( num ) { if ( num . length >= 3 ) { let d1 = num [ num . length - 1 ] . charCodeAt ( ) ; if ( d1 % 2 != 0 ) return false ; let d2 = num [ num . length - 2 ] . charCodeAt ( ) ; let sum = 0 ; for ( let i = 0 ; i < num . length ; i ++ ) sum += num [ i ] . charCodeAt ( ) ; return ( ( sum % 3 == 0 ) && ( d2 * 10 + d1 ) % 4 == 0 ) ; } else { let number = parseInt ( num , 10 ) ; document . write ( number ) ; return ( number % 12 == 0 ) ; } } let num = " " ; if ( isDvisibleBy12 ( num ) ) document . write ( " " ) ; else document . write ( " " ) ;
Largest number that is not a perfect square | JavaScript program to find the largest non perfect square number among n numbers ; takes the sqrt of the number ; checks if it is a perfect square number ; function to find the largest non perfect square number ; stores the maximum of all non perfect square numbers ; traverse for all elements in the array ; store the maximum if not a perfect square ; Driver Code ; function call
function check ( n ) { let d = Math . sqrt ( n ) ; if ( d * d == n ) return true ; return false ; } function largestNonPerfectSquareNumber ( a , n ) { let maxi = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( ! check ( a [ i ] ) ) maxi = Math . max ( a [ i ] , maxi ) ; } return maxi ; } let a = [ 16 , 20 , 25 , 2 , 3 , 10 ] ; let n = a . length ; document . write ( largestNonPerfectSquareNumber ( a , n ) ) ;
Program to print Arithmetic Progression series | JavaScript Program to print an arithmetic progression series ; Printing AP by simply adding d to previous term . ; starting number ; Common difference ; N th term to be find
function printAP ( a , d , n ) { let curr_term ; curr_term = a ; for ( let i = 1 ; i <= n ; i ++ ) { document . write ( curr_term + " " ) ; curr_term = curr_term + d ; } } let a = 2 ; let d = 1 ; let n = 5 ; printAP ( a , d , n ) ;
Program to print non square numbers | Javascript program to print first n non - square numbers . ; Print curr_count numbers . curr_count is current gap between two square numbers . ; Skip a square number . ; Count of next non - square numbers is next even number . ; Driver code
function printNonSquare ( n ) { let curr_count = 2 , num = 2 , count = 0 ; while ( count < n ) { for ( let i = 0 ; i < curr_count && count < n ; i ++ ) { document . write ( num + " " ) ; count ++ ; num ++ ; } num ++ ; curr_count += 2 ; } } let n = 10 ; printNonSquare ( n ) ;
Count number of trailing zeros in product of array | Returns count of zeros in product of array ; Count number of 2 s in each element ; Count number of 5 s in each element ; Return the minimum ; Driver code
function countZeros ( a , n ) { let count2 = 0 , count5 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { while ( a [ i ] % 2 == 0 ) { a [ i ] = parseInt ( a [ i ] / 2 ) ; count2 ++ ; } while ( a [ i ] % 5 == 0 ) { a [ i ] = parseInt ( a [ i ] / 5 ) ; count5 ++ ; } } return ( count2 < count5 ) ? count2 : count5 ; } let a = [ 10 , 100 , 20 , 30 , 50 , 90 , 12 , 80 ] ; let n = a . length ; document . write ( countZeros ( a , n ) ) ;
Sum of square of first n even numbers | JavaScript program to find sum of square of first n even numbers . ; Driver code
function squareSum ( n ) { let sum = 0 ; for ( let i = 1 ; i <= n ; i ++ ) sum += ( 2 * i ) * ( 2 * i ) ; return sum ; } document . write ( squareSum ( 8 ) ) ;