codeparrot/xlcost-text-to-code huggingface dataset:
Check if a right | Function to check if N is a perfect square number or not ; If N is a non positive integer ; Stores square root of N ; Check for perfect square ; If N is not a perfect square number ; Function to check if given two sides of a triangle forms a right - angled triangle ; If the value of ( A * A + B * B ) is a perfect square number ; Update checkTriangle ; If the value of ( A * A - B * B ) is a perfect square number ; Update checkTriangle ; If the value of ( B * B - A * A ) is a perfect square number ; Update checkTriangle ; Driver Code ; If the given two sides of a triangle forms a right - angled triangle ; Otherwise
function checkPerfectSquare ( N ) { if ( N <= 0 ) { return 0 ; } let sq = Math . sqrt ( N ) ; if ( Math . floor ( sq ) == Math . ceil ( sq ) ) { return 1 ; } return 0 ; } function checktwoSidesareRighTriangle ( A , B ) { let checkTriangle = false ; if ( checkPerfectSquare ( A * A + B * B ) != 0 ) { checkTriangle = true ; } if ( checkPerfectSquare ( A * A - B * B ) != 0 ) { checkTriangle = true ; } if ( checkPerfectSquare ( B * B - A * A ) != 0 ) { checkTriangle = true ; } return checkTriangle ; } let A = 3 , B = 4 ; if ( checktwoSidesareRighTriangle ( A , B ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Minimize increments or decrements required to make sum and product of array elements non | Function to find the sum of array ; Return the sum ; Function that counts the minimum operations required to make the sum and product of array non - zero ; Stores count of zero elements ; Iterate over the array to count zero elements ; Sum of elements of the array ; Prvar the result ; Given array arr ; Size of array ; Function Call
function array_sum ( arr , n ) { var sum = 0 ; for ( i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } function countOperations ( arr , N ) { var count_zeros = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) count_zeros ++ ; } var sum = array_sum ( arr , N ) ; if ( count_zeros != 0 ) return count_zeros ; if ( sum == 0 ) return 1 ; return 0 ; } var arr = [ - 1 , - 1 , 0 , 0 ] ; var N = arr . length ; document . write ( countOperations ( arr , N ) ) ;
Bitwise OR of all unordered pairs from a given array | Function to find the bitwise OR of all possible pairs of the array ; Stores bitwise OR of all possible pairs of arr [ ] ; Traverse the array arr [ ] ; Update totalOR ; Return bitwise OR of all possible pairs of arr [ ] ; Driver Code
function TotalBitwiseORPair ( arr , N ) { let totalOR = 0 ; for ( let i = 0 ; i < N ; i ++ ) { totalOR |= arr [ i ] ; } return totalOR ; } let arr = [ 4 , 5 , 12 , 15 ] ; let N = arr . length ; document . write ( TotalBitwiseORPair ( arr , N ) ) ;
Count of packets placed in each box after performing given operations | Function to print final array after performing avar the operations ; Initialize variables ; Traverse through avar operations ; Operation Type ; Move left ; Move right ; Pick a packet ; Drop a packet ; Exit ; Print final array ; Given capacity ; Given array with initial values ; Array size ; Operations ; Number of operations ; Function call
function printFinalArray ( a , n , operations , p , capacity ) { var i , curr = 0 ; var picked = false ; for ( i = 0 ; i < p ; i ++ ) { var s = operations [ i ] ; var flag = false ; switch ( s ) { case 1 : if ( curr != 0 ) curr -- ; break ; case 2 : if ( curr != n - 1 ) curr ++ ; break ; case 3 : if ( picked == false && a [ curr ] != 0 ) { picked = true ; a [ curr ] -- ; } break ; case 4 : if ( picked == true && a [ curr ] != capacity ) { picked = false ; a [ curr ] ++ ; } break ; default : flag = true ; } if ( flag == true ) break ; } for ( i = 0 ; i < n ; i ++ ) { document . write ( a [ i ] + " " ) ; } } var capacity = 5 ; var a = [ 2 , 5 , 2 ] ; var N = a . length ; var operations = [ 3 , 2 , 4 , 1 , 4 , 5 ] ; var M = operations . length ; printFinalArray ( a , N , operations , M , capacity ) ;
Check if sum of count of digits of array elements is Prime or not | Function to check whether a number is prime or not ; Corner cases ; If given number is a multiple of 2 or 3 ; Function to check if sum of count of digits of all array elements is prime or not ; Initialize sum with 0 ; Traverse over the array ; Convert array element to string ; Add the count of digits to sum ; Print the result ; Drive Code ; Function call
function isPrime ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( let i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } function CheckSumPrime ( A , N ) { let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let s = new String ( A [ i ] ) ; sum += s . length ; } if ( isPrime ( sum ) ) { document . write ( " " + " " ) ; } else { document . write ( " " + " " ) ; } } let A = [ 1 , 11 , 12 ] ; let N = A . length CheckSumPrime ( A , N ) ;
Probability of obtaining Prime Numbers as product of values obtained by throwing N dices | Function to find the value of power ( X , N ) ; Stores the value of ( X ^ N ) ; Calculate the value of power ( x , N ) ; If N is odd ; Update res ; Update x ; Update N ; Function to find the probability of obtaining a prime number as the product of N thrown dices ; Stores count of favorable outcomes ; Stores count of sample space ; Print the required probability ; Driver Code
function power ( x , N ) { let res = 1 ; while ( N > 0 ) { if ( N % 2 == 1 ) { res = ( res * x ) ; } x = ( x * x ) ; N = N >> 1 ; } return res ; } function probablityPrimeprod ( N ) { let N_E = 3 * N ; let N_S = power ( 6 , N ) ; document . write ( N_E + " " + N_S ) ; } let N = 2 ; probablityPrimeprod ( N ) ;
Smaller palindromic number closest to N | Function to check whether a number is palindrome or not ; Stores reverse of N ; Stores the value of N ; Calculate reverse of N ; Update rev ; Update N ; Update N ; if N is equal to rev of N ; Function to find the closest smaller palindromic number to N ; Calculate closest smaller palindromic number to N ; Update N ; Driver Code
function checkPalindrome ( N ) { let rev = 0 ; let temp = N ; while ( N != 0 ) { rev = Math . floor ( rev * 10 + N % 10 ) ; N = Math . floor ( N / 10 ) ; } N = temp ; if ( N == rev ) { return true ; } return false ; } function closestSmallerPalindrome ( N ) { do { N -- ; } while ( N >= 0 && ! checkPalindrome ( N ) ) ; return N ; } let N = 4000 ; document . write ( closestSmallerPalindrome ( N ) ) ;
Minimize increments or decrements by 2 to convert given value to a perfect square | Function to find the minimum number of operations required to make N a perfect square ; Stores count of operations by performing decrements ; Stores value of N ; Decrement the value of temp ; Stores square root of temp ; If temp is a perfect square ; Update temp ; Store count of operations by performing increments ; Increment the value of N ; Stores sqrt of N ; If N is a perfect square ; Update temp ; Return the minimum count ; Driver Code
function MinimumOperationReq ( N ) { let cntDecr = 0 ; let temp = N ; while ( temp > 0 ) { let X = Math . floor ( Math . sqrt ( temp ) ) ; if ( X * X == temp ) { break ; } temp = temp - 2 ; cntDecr += 1 ; } let cntIncr = 0 ; while ( true ) { let X = Math . floor ( Math . sqrt ( N ) ) ; if ( X * X == N ) { break ; } N = N + 2 ; cntIncr += 1 ; } return Math . min ( cntIncr , cntDecr ) ; } let N = 15 ; document . write ( MinimumOperationReq ( N ) ) ;
First term from given Nth term of the equation F ( N ) = ( 2 * F ( N | JavaScript program to implement the above approach ; Function to find the value of power ( X , N ) % M ; Stores the value of ( X ^ N ) % M ; Calculate the value of power ( x , N ) % M ; If N is odd ; Update res ; Update x ; Update N ; Function to find modulo multiplicative inverse of X under modulo M ; Function to find the value of F ( 1 ) ; Stores power ( 2 , N - 1 ) ; Stores modulo multiplicative inverse of P_2 under modulo M ; Stores the value of F ( 1 ) ; Update res ; Driver code
var M = 100000007 ; function power ( x , N ) { var res = 1 ; while ( N > 0 ) { if ( N % 2 == 1 ) { res = ( res * x ) % M ; } x = ( x * x ) % M ; N = N >> 1 ; } return res ; } function moduloInverse ( X ) { return power ( X , M - 2 ) ; } function F_1 ( N , F_N ) { var P_2 = power ( 2 , N - 1 ) ; var modInv = moduloInverse ( P_2 ) ; var res ; res = ( ( modInv % M ) * ( F_N % M ) ) % M ; return res ; } var N = 3 ; var F_N = 6 ; document . write ( F_1 ( N , F_N ) ) ;
Queries to replace every array element by its XOR with a given value with updates | Function to insert an element into the array ; Function to update every array element a [ i ] by a [ i ] ^ x ; Function to compute the final results after the operations ; Driver Code ; Queries ; Function Call
function add ( arr , x ) { arr . push ( x ) ; } function update ( effect , x ) { effect = effect ^ x ; return effect ; } function computeResults ( arr , effect ) { for ( let i = 0 ; i < arr . length ; i ++ ) { arr [ i ] = arr [ i ] ^ effect ; document . write ( arr [ i ] + " " ) ; } } let arr = [ 0 ] ; let effect = 0 ; add ( arr , 5 ) ; effect = update ( effect , 2 ) computeResults ( arr , effect ) ;
Check if a subarray of size K exists whose elements form a number divisible by 3 | Function to find the K size subarray ; Check if the first K elements forms a number which is divisible by 3 ; Using Sliding window technique ; Calculate sum of next K size subarray ; Check if sum is divisible by 3 ; Update the indices of the subarray ; If no such subarray is found ; Print the subarray ; Given array and K ; Function Call
function findSubArray ( arr , k ) { var ans = [ ] ; var i , sum = 0 ; for ( i = 0 ; i < k ; i ++ ) { sum += arr [ i ] ; } var found = 0 ; if ( sum % 3 == 0 ) { ans = [ 0 , i - 1 ] ; found = 1 ; } for ( var j = i ; j < arr . length ; j ++ ) { if ( found == 1 ) break ; sum = sum + arr [ j ] - arr [ j - k ] ; if ( sum % 3 == 0 ) { ans = [ j - k + 1 , j ] ; found = 1 ; } } if ( found == 0 ) ans = [ - 1 , 0 ] ; if ( ans . first == - 1 ) { cout << - 1 ; } else { for ( i = ans [ 0 ] ; i <= ans [ 1 ] ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } } var arr = [ 84 , 23 , 45 , 12 , 56 , 82 ] ; var K = 3 ; findSubArray ( arr , K ) ;
Count permutations of given array that generates the same Binary Search Tree ( BST ) | Function to precompute the factorial of 1 to N ; Function to get the value of nCr ; nCr = fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; Function to count the number of ways to rearrange the array to obtain same BST ; Store the size of the array ; Base case ; Store the elements of the left subtree of BST ; Store the elements of the right subtree of BST ; Store the root node ; Push all the elements of the left subtree ; Push all the elements of the right subtree ; Store the size of leftSubTree ; Store the size of rightSubTree ; Recurrence relation ; Driver Code ; Store the size of arr ; Store the factorial up to N ; Precompute the factorial up to N
function calculateFact ( fact , N ) { fact [ 0 ] = 1 ; for ( var i = 1 ; i < N ; i ++ ) { fact [ i ] = fact [ i - 1 ] * i ; } } function nCr ( fact , N , R ) { if ( R > N ) return 0 ; var res = parseInt ( fact [ N ] / fact [ R ] ) ; res = parseInt ( res / fact [ N - R ] ) ; return res ; } function countWays ( arr , fact ) { var N = arr . length ; if ( N <= 2 ) { return 1 ; } var leftSubTree = [ ] ; var rightSubTree = [ ] ; var root = arr [ 0 ] ; for ( var i = 1 ; i < N ; i ++ ) { if ( arr [ i ] < root ) { leftSubTree . push ( arr [ i ] ) ; } else { rightSubTree . push ( arr [ i ] ) ; } } var N1 = leftSubTree . length ; var N2 = rightSubTree . length ; var countLeft = countWays ( leftSubTree , fact ) ; var countRight = countWays ( rightSubTree , fact ) ; return nCr ( fact , N - 1 , N1 ) * countLeft * countRight ; } var arr = [ ] ; arr = [ 3 , 4 , 5 , 1 , 2 ] ; var N = arr . length ; var fact = Array ( N ) ; calculateFact ( fact , N ) ; document . write ( countWays ( arr , fact ) ) ;
Sum of all ordered pair | Function to calculate the sum of all pair - products ; Stores sum of array ; Update sum of the array ; Driver Code
function sumOfProd ( arr , N ) { let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } return sum * sum ; } let arr = [ 2 , 3 , 1 , 5 , 4 ] ; let N = arr . length ; document . write ( sumOfProd ( arr , N ) ) ;
Check if a given number is one less than twice its reverse | Iterative function to reverse digits of num ; Loop to extract all digits of the number ; Function to check if N satisfies given equation ; Driver Code
function rev ( num ) { var rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = parseInt ( num / 10 ) ; } return rev_num ; } function check ( n ) { return 2 * rev ( n ) == n + 1 ; } var n = 73 ; if ( check ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if a given number is one less than twice its reverse | Function to check y is a power of x ; logarithm function to calculate value ; Compare to the result1 or result2 both are equal ; Function to check if N satisfies the equation 2 * reverse ( n ) = n + 1 ; Driver code
function isPower ( x , y ) { let res1 = Math . log ( y ) / Math . log ( x ) ; let res2 = Math . log ( y ) / Math . log ( x ) ; return ( res1 == res2 ) ; } function check ( n ) { let x = ( n + 7 ) / 8 ; if ( ( n + 7 ) % 8 == 0 && isPower ( 10 , x ) ) return true ; else return false ; } let n = 73 ; if ( check ( n ) != 0 ) document . write ( " " ) ; else document . write ( " " ) ;
Nth term of a recurrence relation generated by two given arrays | JavaScript program for the above approach ; Function to calculate Nth term of general recurrence relations ; Stores the generated sequence ; Current term is sum of previous k terms ; Print the nth term ; Given Array F [ ] and C [ ] ; Given N and K ; Function Call
let mod = 1e9 + 7 ; function NthTerm ( F , C , K , n ) { let ans = new Uint8Array ( n + 1 ) ; for ( let i = 0 ; i < K ; i ++ ) ans [ i ] = F [ i ] ; for ( let i = K ; i <= n ; i ++ ) { for ( let j = i - K ; j < i ; j ++ ) { ans [ i ] += ans [ j ] ; ans [ i ] %= mod ; } } document . write ( ans [ n ] + " " ) ; } let F = [ 0 , 1 ] ; let C = [ 1 , 1 ] ; let K = 2 ; let N = 10 ; NthTerm ( F , C , K , N ) ;
Mode of frequencies of given array elements | Function to find the mode of the frequency of the array ; Stores the frequencies of array elements ; Traverse through array elements and count frequencies ; Stores the frequencies 's of  frequencies of array elements ; Stores the minimum value ; Find the Mode in 2 nd map ; Search for this Mode ; When mode is find then return to main function . ; If mode is not found ; Driver Code
function countFreq ( arr , n ) { let mp1 = new Map ( ) ; for ( let i = 0 ; i < n ; ++ i ) { if ( mp1 . has ( arr [ i ] ) ) { mp1 . set ( arr [ i ] , mp1 . get ( arr [ i ] ) + 1 ) ; } else { mp1 . set ( arr [ i ] , 1 ) ; } } let mp2 = new Map ( ) ; for ( let [ key , value ] of mp1 . entries ( ) ) { if ( mp2 . has ( value ) ) { mp2 . set ( value , mp2 . get ( value ) + 1 ) ; } else { mp2 . set ( value , 1 ) ; } } let M = Number . MIN_VALUE ; for ( let [ key , value ] of mp2 . entries ( ) ) { M = Math . max ( M , value ) ; } for ( let [ key , value ] of mp2 . entries ( ) ) { if ( M == value ) { return key ; } } return 0 ; } let arr = [ 6 , 10 , 3 , 10 , 8 , 3 , 6 , 4 ] ; let n = arr . length ; document . write ( countFreq ( arr , n ) ) ;
Maximize count of nodes disconnected from all other nodes in a Graph | Function which returns the maximum number of isolated nodes ; Used nodes ; Remaining edges ; Count nodes used ; If given edges are non - zero ; Given N and E ; Function call
function maxDisconnected ( N , E ) { let curr = 1 ; let rem = E ; while ( rem > 0 ) { rem = rem - Math . min ( curr , rem ) ; curr ++ ; } if ( curr > 1 ) { return N - curr ; } else { return N ; } } let N = 5 , E = 1 ; document . write ( maxDisconnected ( N , E ) ) ;
Check if number is palindrome or not in base B | Function to check if N in base B is palindrome or not ; Stores the reverse of N ; Stores the value of N ; Extract all the digits of N ; Generate its reverse ; Driver Code
function checkPalindromeB ( N , B ) { var rev = 0 ; var N1 = N ; while ( N1 ) { rev = rev * B + N1 % B ; N1 = parseInt ( N1 / B ) ; } return N == rev ; } var N = 5 , B = 2 ; if ( checkPalindromeB ( N , B ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Product of all numbers up to N that are co | Function to return gcd of a and b ; Base Case ; Recursive GCD ; Function to find the product of all the numbers till N that are relatively prime to N ; Stores the resultant product ; Iterate over [ 2 , N ] ; If gcd is 1 , then find the product with result ; Return the final product ; Driver Code
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function findProduct ( N ) { var result = 1 ; for ( var i = 2 ; i < N ; i ++ ) { if ( gcd ( i , N ) == 1 ) { result *= i ; } } return result ; } var N = 5 ; document . write ( findProduct ( N ) )
Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum | Function to count maximum number of array elements equal ; Driver Code ; Function Call
function countEqual ( n ) { return parseInt ( ( n + 1 ) / 2 ) ; } var arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; var n = arr . length ; document . write ( countEqual ( n ) ) ;
Minimize count of unequal elements at corresponding indices between given arrays | Function that count of the mismatched pairs in bot the array ; Create a parent array and initialize it ; Preprocessing of the given pairs of indices ; 1 - based indexing ; If both indices doesn 't belong  to same component ; Insert the indices in same component ; Map to get the indices of array A ; If current element is not present in array B then count this as mismatched ; Get the index of the element in array B ; Check if both indices belong to same connected component if not increment the count ; Return answer ; Function that gives the connected component of each index ; Function that creates the connected components ; Find parent of a and b recursively ; Update the parent of a ; Driver Code ; Given arrays A [ ] , B [ ] ; List of indices ; Function call
function countPairs ( A , B , N , M , List ) { var count = 0 ; var par = Array ( N + 1 ) ; for ( var i = 0 ; i <= N ; i ++ ) par [ i ] = i ; for ( var i = 0 ; i < M ; i ++ ) { var index1 = find ( par , List [ i ] [ 0 ] - 1 ) ; var index2 = find ( par , List [ i ] [ 1 ] - 1 ) ; if ( index1 != index2 ) { unionn ( par , index1 , index2 ) ; } } var mp = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { mp . set ( A [ i ] , i ) ; } for ( var i = 0 ; i < N ; i ++ ) { if ( A [ i ] != B [ i ] ) { if ( ! mp . has ( B [ i ] ) ) { count ++ ; continue ; } var j = mp . get ( B [ i ] ) ; if ( find ( par , i ) != find ( par , j ) ) count ++ ; } } return count ; } function find ( par , x ) { if ( par [ x ] == x ) return x ; else return par [ x ] = find ( par , par [ x ] ) ; } function unionn ( par , a , b ) { a = find ( par , a ) ; b = find ( par , b ) ; if ( a == b ) return ; par [ a ] = b ; } var N = 5 ; var M = 4 ; var A = [ 1 , 5 , 9 , 2 , 3 ] ; var B = [ 2 , 4 , 5 , 1 , 3 ] ; var List = [ [ 1 , 4 ] , [ 2 , 3 ] , [ 3 , 5 ] , [ 2 , 5 ] ] ; document . write ( countPairs ( A , B , N , M , List ) ) ;
Maximum value of expression ( arr [ i ] + arr [ j ] * arr [ k ] ) formed from a valid Triplet | Function that generate all valid triplets and calculate the value of the valid triplets ; Generate all triplets ; Check whether the triplet is valid or not ; Update the value ; Print the maximum value ; Given array arr [ ] ; Function call
function max_valid_triplet ( A , n ) { let ans = - 1 ; for ( let i = 0 ; i < n - 2 ; i ++ ) { for ( let j = i + 1 ; j < n - 1 ; j ++ ) { for ( let k = j + 1 ; k < n ; k ++ ) { if ( A [ i ] < A [ j ] && A [ j ] < A [ k ] ) { let value = A [ i ] + A [ j ] * A [ k ] ; if ( value > ans ) { ans = value ; } } } } } document . write ( ans ) ; } let arr = [ 7 , 9 , 3 , 8 , 11 , 10 ] ; let n = arr . length ; max_valid_triplet ( arr , n ) ;
Product of proper divisors of a number for Q queries | Javascript implementation of the above approach ; Function to precompute the product of proper divisors of a number at it 's corresponding index ; Returning the pre - computed values ; Driver code
mod = 1000000007 ans = Array ( 100002 ) . fill ( 1 ) function preCompute ( ) { for ( var i = 2 ; i <= 100000 / 2 ; i ++ ) { for ( var j = 2 * i ; j <= 100000 ; j += i ) { ans [ j ] = ( ans [ j ] * i ) % mod ; } } } function productOfProperDivi ( num ) { return ans [ num ] ; } preCompute ( ) ; var queries = 5 ; var a = [ 4 , 6 , 8 , 16 , 36 ] ; for ( var i = 0 ; i < queries ; i ++ ) { document . write ( productOfProperDivi ( a [ i ] ) + " " ) ; }
Highest power of 2 that divides the LCM of first N Natural numbers . | Function to find LCM of first N natural numbers ; Initialize result ; Ans contains LCM of 1 , 2 , 3 , . . i after i 'th iteration ; Function to find the highest power of 2 which divides LCM of first n natural numbers ; Find lcm of first N natural numbers ; To store the highest required power of 2 ; Counting number of consecutive zeros from the end in the given binary String ; Driver Code
function findlcm ( n ) { let ans = 1 ; for ( let i = 1 ; i <= n ; i ++ ) ans = ( ( ( i * ans ) ) / ( __gcd ( i , ans ) ) ) ; return ans ; } function highestPower ( n ) { let lcm = findlcm ( n ) ; let ans = 0 ; for ( let i = 1 ; ; i ++ ) { let x = Math . pow ( 2 , i ) ; if ( lcm % x == 0 ) { ans = i ; } if ( x > n ) break ; } return ans ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } let n = 15 ; document . write ( highestPower ( n ) ) ;
Highest power of 2 that divides the LCM of first N Natural numbers . | Function to find the highest power of 2 which divides LCM of first n natural numbers ; Driver code
function highestPower ( n ) { return parseInt ( Math . log ( n ) / Math . log ( 2 ) ) ; } var n = 15 ; document . write ( highestPower ( n ) ) ;
Check if number can be made prime by deleting a single digit | Function to check if N is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to delete character at index i from given String str ; Deletes character at position 4 ; Function to check if a number becomes prime by deleting any digit ; Converting the number to String ; length of String ; number should not be of single digit ; Loop to find all numbers after deleting a single digit ; Deleting ith character from the String ; converting String to let ; Driver Code
function isPrime ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( let i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } function deleteIth ( str , i ) { str = str . substr ( 0 , i ) + str . substr ( i + 1 ) ; return str ; } function isPrimePossible ( N ) { let s = N . toString ( ) ; let l = s . length ; if ( l < 2 ) return false ; for ( let i = 0 ; i < l ; i ++ ) { let str = deleteIth ( s , i ) ; let num = parseInt ( str ) ; if ( isPrime ( num ) ) return true ; } return false ; } let N = 610 ; if ( isPrimePossible ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if a number has an odd count of odd divisors and even count of even divisors | Function to find the count of even and odd factors of N ; Loop runs till square root ; Condition to check if the even factors of the number N is is even and count of odd factors is odd ; Driver Code
function checkFactors ( N ) { let ev_count = 0 , od_count = 0 ; for ( let i = 1 ; i <= Math . sqrt ( N ) + 1 ; i ++ ) { if ( N % i == 0 ) { if ( i == Math . floor ( N / i ) ) { if ( i % 2 == 0 ) ev_count += 1 ; else od_count += 1 ; } else { if ( i % 2 == 0 ) ev_count += 1 ; else od_count += 1 ; if ( Math . floor ( N / i ) % 2 == 0 ) ev_count += 1 ; else od_count += 1 ; } } } if ( ev_count % 2 == 0 && od_count % 2 == 1 ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; } let N = 36 ; checkFactors ( N ) ;
Count of distinct permutations of length N having no similar adjacent characters | Function to print the number of permutations possible ; Driver Code
function countofPermutations ( N ) { return parseInt ( 3 * Math . pow ( 2 , N - 1 ) ) ; } var N = 5 ; document . write ( countofPermutations ( N ) ) ;