codeparrot/xlcost-text-to-code huggingface dataset:
Find the absolute difference between the nearest powers of two given integers for every array element | Function to print the array ; Traverse the array ; Function to modify array elements by absolute difference of the nearest perfect power of a and b ; Traverse the array arr [ ] ; Find the log a of arr [ i ] ; Find the power of a less than and greater than a ; Find the log b of arr [ i ] ; Find the power of b less than and greater than b ; Update arr [ i ] with absolute difference of log_a & log _b ; Print the modified array ; Driver code
function printArray ( arr , N ) { for ( let i = 0 ; i < N ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } function nearestPowerDiff ( arr , N , a , b ) { for ( let i = 0 ; i < N ; i ++ ) { let log_a = Math . floor ( Math . log ( arr [ i ] ) / Math . log ( a ) ) ; let A = Math . floor ( Math . pow ( a , log_a ) ) ; let B = Math . floor ( Math . pow ( a , log_a + 1 ) ) ; if ( ( arr [ i ] - A ) < ( B - arr [ i ] ) ) log_a = A ; else log_a = B ; let log_b = Math . floor ( Math . log ( arr [ i ] ) / Math . log ( b ) ) ; A = Math . floor ( Math . pow ( b , log_b ) ) ; B = Math . floor ( Math . pow ( b , log_b + 1 ) ) ; if ( ( arr [ i ] - A ) < ( B - arr [ i ] ) ) log_b = A ; else log_b = B ; arr [ i ] = Math . abs ( log_a - log_b ) ; } printArray ( arr , N ) ; } let arr = [ 5 , 12 , 25 ] ; let A = 2 , B = 3 ; let N = arr . length ; nearestPowerDiff ( arr , N , A , B ) ;
Count subarrays having even Bitwise OR | Function to count the number of subarrays having even Bitwise OR ; Store number of subarrays having even bitwise OR ; Store the length of the current subarray having even numbers ; Traverse the array ; If the element is even ; Increment size of the current continuous sequence of even array elements ; If arr [ i ] is odd ; If length is non zero ; Adding contribution of subarrays consisting only of even numbers ; Make length of subarray as 0 ; Add contribution of previous subarray ; Return total count of subarrays ; Driver Code ; Function Call
function bitOr ( arr , N ) { var count = 0 ; var length = 0 ; var i ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { length ++ ; } else { if ( length != 0 ) { count += Math . floor ( ( length ) * ( length + 1 ) / 2 ) ; } length = 0 ; } } count += Math . floor ( ( length ) * ( length + 1 ) / 2 ) ; return count ; } var arr = [ 1 , 5 , 4 , 2 , 6 ] var N = arr . length ; document . write ( bitOr ( arr , N ) ) ;
Find all pairs raised to power K differs by exactly N | Function to prvar pairs whose difference raised to the power K is X ; Stores the count of valid pairs ; Iterate over the range [ - 1000 , 1000 ] ; Iterate over the range [ - 1000 , 1000 ] ; If the current pair satisfies the given condition ; Increment the count by 1 ; If no such pair exists ; Driver Code
function ValidPairs ( X , K ) { var count = 0 ; for ( A = - 1000 ; A <= 1000 ; A ++ ) { for ( B = - 1000 ; B <= 1000 ; B ++ ) { if ( Math . pow ( A , K ) - Math . pow ( B , K ) == X ) { count ++ ; document . write ( A + " " + B + " " ) ; } } } if ( count == 0 ) { document . write ( " " ) ; } } var X = 33 ; var K = 5 ; ValidPairs ( X , K ) ;
Connect a graph by M edges such that the graph does not contain any cycle and Bitwise AND of connected vertices is maximum | Function to find the maximum Bitwise AND of connected components possible by connecting a graph using M edges ; Stores total number of ways to connect the graph ; Stores the maximum Bitwise AND ; Iterate over the range [ 0 , 2 ^ n ] ; Store the Bitwise AND of the connected vertices ; Store the count of the connected vertices ; Check for all the bits ; If i - th bit is set ; If the first vertex is added ; Set andans equal to arr [ i ] ; Calculate Bitwise AND of arr [ i ] with andans ; Increase the count of connected vertices ; If number of connected vertices is ( m + 1 ) , no cycle is formed ; Find the maximum Bitwise AND value possible ; Return the maximum Bitwise AND possible ; Driver Code
function maximumAND ( arr , n , m ) { let tot = 1 << n ; let mx = 0 ; for ( let bm = 0 ; bm < tot ; bm ++ ) { let andans = 0 ; let count = 0 ; for ( let i = 0 ; i < n ; ++ i ) { if ( ( ( bm >> i ) & 1 ) != 0 ) { if ( count == 0 ) { andans = arr [ i ] ; } else { andans = andans & arr [ i ] ; } count ++ ; } } if ( count == ( m + 1 ) ) { mx = Math . max ( mx , andans ) ; } } return mx ; } let arr = [ 1 , 2 , 3 , 4 ] ; let N = arr . length ; let M = 2 ; document . write ( maximumAND ( arr , N , M ) ) ;
Calculate sum of the array generated by given operations | Function to find the sum of the array formed by performing given set of operations while traversing the array ops [ ] ; If the size of array is 0 ; Stores the required sum ; Traverse the array ops [ ] ; If the character is C , remove the top element from the stack ; If the character is D , then push 2 * top element into stack ; If the character is + , add sum of top two elements from the stack ; Otherwise , push x and add it to ans ; Print the resultant sum ; Driver Code
function findTotalSum ( ops ) { if ( ops . length == 0 ) { document . write ( 0 ) ; return ; } var pts = [ ] ; var ans = 0 ; for ( var i = 0 ; i < ops . length ; i ++ ) { if ( ops [ i ] == " " ) { ans -= pts [ pts . length - 1 ] ; pts . pop ( ) ; } else if ( ops [ i ] == " " ) { pts . push ( pts [ pts . length - 1 ] * 2 ) ; ans += pts [ pts . length - 1 ] ; } else if ( ops [ i ] == " " ) { var a = pts [ pts . length - 1 ] ; pts . pop ( ) ; var b = pts [ pts . length - 1 ] ; pts . push ( a ) ; ans += ( a + b ) ; pts . push ( a + b ) ; } else { var n = parseInt ( ops [ i ] ) ; ans += n ; pts . push ( n ) ; } } document . write ( ans ) ; } var arr = [ " " , " " , " " , " " , " " ] ; findTotalSum ( arr ) ;
XOR of major diagonal elements of a 3D Matrix | Function to calculate Bitwise XOR of major diagonal elements of 3D matrix ; Stores the Bitwise XOR of the major diagonal elements ; If element is part of major diagonal ; Print the resultant Bitwise XOR ; Driver Code
function findXOR ( mat , N ) { let XOR = 0 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < N ; j ++ ) { for ( let k = 0 ; k < N ; k ++ ) { if ( ( i == j && j == k ) ) { XOR ^= mat [ i ] [ j ] [ k ] ; XOR ^= mat [ i ] [ j ] [ N - k - 1 ] ; } } } } document . write ( XOR ) ; } let mat = [ [ [ 1 , 2 ] , [ 3 , 4 ] ] , [ [ 5 , 6 ] , [ 7 , 8 ] ] ] ; let N = mat . length ; findXOR ( mat , N ) ;
XOR of major diagonal elements of a 3D Matrix | Function to calculate Bitwise XOR of major diagonal elements of 3D matrix ; Stores the Bitwise XOR of the major diagonal elements ; Print the resultant Bitwise XOR ; Driver Code
function findXOR ( mat , N ) { let XOR = 0 ; for ( let i = 0 ; i < N ; i ++ ) { XOR ^= mat [ i ] [ i ] [ i ] ; XOR ^= mat [ i ] [ i ] [ N - i - 1 ] ; } document . write ( XOR ) ; } let mat = [ [ [ 1 , 2 ] , [ 3 , 4 ] ] , [ [ 5 , 6 ] , [ 7 , 8 ] ] ] ; let N = mat . length ; findXOR ( mat , N ) ; ;
Count of subtrees possible from an N | Javascript program of the above approach ; Adjacency list to represent the graph ; Stores the count of subtrees possible from given N - ary Tree ; Utility function to count the number of subtrees possible from given N - ary Tree ; Stores the count of subtrees when cur node is the root ; Traverse the adjacency list ; Iterate over every ancestor ; Calculate product of the number of subtrees for each child node ; Update the value of ans ; Return the resultant count ; Function to count the number of subtrees in the given tree ; Initialize an adjacency matrix ; Add the edges ; Function Call to count the number of subtrees possible ; Print count of subtrees ; Driver Code
var MAX = 300004 ; var graph = Array . from ( Array ( MAX ) , ( ) => new Array ( ) ) ; var mod = 1000000007 ; var ans = 0 ; function countSubtreesUtil ( cur , par ) { var res = 1 ; for ( var i = 0 ; i < graph [ cur ] . length ; i ++ ) { var v = graph [ cur ] [ i ] ; if ( v == par ) continue ; res = ( res * ( countSubtreesUtil ( v , cur ) + 1 ) ) % mod ; } ans = ( ans + res ) % mod ; return res ; } function countSubtrees ( N , adj ) { for ( var i = 0 ; i < N - 1 ; i ++ ) { var a = adj [ i ] [ 0 ] ; var b = adj [ i ] [ 1 ] ; graph [ a ] . push ( b ) ; graph [ b ] . push ( a ) ; } countSubtreesUtil ( 1 , 1 ) ; document . write ( ans + 1 ) ; } var N = 3 ; var adj = [ [ 0 , 1 ] , [ 1 , 2 ] ] ; countSubtrees ( N , adj ) ;
Find the player who wins the game of placing alternate + and | Function to check which player wins the game ; Stores the difference between + ve and - ve array elements ; Traverse the array ; Update diff ; Checks if diff is even ; Given Input ; Function call to check which player wins the game
function checkWinner ( arr , N ) { let diff = 0 ; for ( let i = 0 ; i < N ; i ++ ) { diff -= arr [ i ] ; } if ( diff % 2 == 0 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let arr = [ 1 , 2 ] ; let N = arr . length ; checkWinner ( arr , N ) ;
Generate an array having sum of Euler Totient Function of all elements equal to N | Function to construct the array such the sum of values of Euler Totient functions of all array elements is N ; Stores the resultant array ; Find divisors in sqrt ( N ) ; If N is divisible by i ; Push the current divisor ; If N is not a perfect square ; Push the second divisor ; Prvar the resultant array ; Driver Code ; Function Call
function constructArray ( N ) { var ans = [ ] ; for ( var i = 1 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { ans . push ( i ) ; if ( N != ( i * i ) ) { ans . push ( N / i ) ; } } } document . write ( ans ) ; } var N = 12 ; constructArray ( N ) ;
Place N boys and M girls in different rows such that count of persons placed in each row is maximized | Function to calculate GCD of two numbers ; Function to count maximum persons that can be placed in a row ; Input ; Function to count maximum persons that can be placed in a row
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function maximumRowValue ( n , m ) { return gcd ( n , m ) ; } var N = 4 ; var M = 2 ; document . write ( maximumRowValue ( N , M ) ) ;
Program to generate an array having convolution of two given arrays | Javascript program for the above approach ; Function to generate a convolution array of two given arrays ; Stores the size of arrays ; Stores the final array ; Traverse the two given arrays ; Update the convolution array ; Print the convolution array c [ ] ; Driver Code
let MOD = 998244353 ; function findConvolution ( a , b ) { let n = a . length , m = b . length ; let c = [ ] ; for ( let i = 0 ; i < n + m - 1 ; ++ i ) { c [ i ] = 0 ; } for ( let i = 0 ; i < n ; ++ i ) { for ( let j = 0 ; j < m ; ++ j ) { c [ i + j ] += ( a [ i ] * b [ j ] ) % MOD ; } } for ( let k = 0 ; k < c . length ; ++ k ) { c [ k ] %= MOD ; document . write ( c [ k ] + " " ) ; } } let A = [ 1 , 2 , 3 , 4 ] ; let B = [ 5 , 6 , 7 , 8 , 9 ] ; findConvolution ( A , B ) ;
Minimum number of 1 s present in a submatrix of given dimensions in a Binary Matrix | Function to count number of 1 s present in a sub matrix from ( start_i , start_j ) to ( end_i , end_j ) ; Stores the number of 1 s present in current submatrix ; Traverse the submatrix ; If mat [ x ] [ y ] is equal to 1 ; Increase count by 1 ; Return the total count of 1 s ; Function to find the minimum number of 1 s present in a sub - matrix of size A * B or B * A ; Stores the minimum count of 1 s ; Iterate i from 0 to N ; Iterate j from 0 to M ; If a valid sub matrix of size A * B from ( i , j ) is possible ; Count the number of 1 s present in the sub matrix of size A * B from ( i , j ) ; Update minimum if count is less than the current minimum ; If a valid sub matrix of size B * A from ( i , j ) is possible ; Count the number of 1 s in the sub matrix of size B * A from ( i , j ) ; Update minimum if count is less than the current minimum ; Return minimum as the final result ; Given Input ; Function call to find the minimum number of 1 s in a submatrix of size A * B or B * A
function count1s ( start_i , start_j , end_i , end_j , mat ) { let count = 0 ; for ( let x = start_i ; x < end_i ; x ++ ) { for ( let y = start_j ; y < end_j ; y ++ ) { if ( mat [ x ] [ y ] == 1 ) count ++ ; } } return count ; } function findMinimumCount ( N , M , A , B , mat ) { let minimum = 1e9 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { if ( i + A <= N && j + B <= M ) { let count = count1s ( i , j , i + A , j + B , mat ) ; minimum = Math . min ( count , minimum ) ; } if ( i + B <= N && j + A <= M ) { let count = count1s ( i , j , i + B , j + A , mat ) ; minimum = Math . min ( count , minimum ) ; } } } return minimum ; } let A = 2 , B = 2 ; let N = 3 , M = 4 ; let mat = [ [ 1 , 0 , 1 , 0 ] , [ 0 , 1 , 0 , 1 ] , [ 1 , 0 , 1 , 0 ] ] ; document . write ( findMinimumCount ( N , M , A , B , mat ) ) ;
Program to check if a number can be expressed as an even power of 2 or not | Function to check if N can be expressed as an even power of 2 ; Iterate till x is N ; If x is even then return true ; Increment x ; If N is not a power of 2 ; Driver code
function checkEvenPower ( n ) { var x = 0 ; while ( x < n ) { var value = Math . pow ( 2 , x ) ; if ( value == n ) { if ( x % 2 == 0 ) return true ; else return false ; } x ++ ; } return false ; } var N = 4 ; document . write ( ( checkEvenPower ( N ) ? " " : " " ) ) ;
Program to check if a number can be expressed as an even power of 2 or not | Function to check if N can be expressed as an even power of 2 or not ; Iterate until low > high ; Calculate mid ; If 2 ^ mid is equal to n ; If mid is odd ; Update the value of low ; Update the value of high ; Otherwise ; Driver code
function checkEvenPower ( n ) { var low = 0 , high = n ; while ( low <= high ) { var mid = low + ( high - low ) / 2 ; var value = parseInt ( Math . pow ( 2 , mid ) ) ; if ( value == n ) { if ( mid % 2 == 1 ) return " " ; else return " " ; } else if ( value < n ) low = mid + 1 ; else high = mid - 1 ; } return " " ; } var N = 4 ; document . write ( checkEvenPower ( N ) ) ;
Program to check if a number can be expressed as an even power of 2 or not | Function to check if N can be expressed as an even power of 2 or not ; If N is not a power of 2 ; Bitwise AND operation ; Driver code
function checkEvenPower ( N ) { if ( ( N & ( N - 1 ) ) != 0 ) return false ; N = N & 0x55555555 ; return ( N > 0 ) ; } let N = 4 ; document . write ( checkEvenPower ( N ) ? 1 : 0 ) ;
Sum of quotients of division of N by powers of K not exceeding N | Function to calculate sum of quotients obtained by dividing N by powers of K <= N ; Store the required sum ; Iterate until i exceeds N ; Update sum ; Multiply i by K to obtain next power of K ; Print the result ; Given N and K
function findSum ( N , K ) { var ans = 0 ; var i = 1 ; while ( i <= N ) { ans += Math . floor ( N / i ) ; i = i * K ; } document . write ( ans ) ; } var N = 10 , K = 2 ; findSum ( N , K ) ;
Count Arithmetic Progressions having sum S and common difference equal to D | Function to count the number of APs with sum S and common difference D ; Multiply S by 2 ; Stores the count of APs ; Iterate over the factors of 2 * S ; Check if i is the factor or not ; Conditions to check if AP can be formed using factor F ; Return the total count of APs ; Driver code
function countAPs ( S , D ) { S = S * 2 ; let answer = 0 ; for ( let i = 1 ; i <= Math . sqrt ( S ) ; i ++ ) { if ( S % i == 0 ) { if ( ( ( S / i ) - D * i + D ) % 2 == 0 ) answer ++ ; if ( ( D * i - ( S / i ) + D ) % 2 == 0 ) answer ++ ; } } return answer ; } let S = 12 , D = 1 ; document . write ( countAPs ( S , D ) ) ;
Minimize insertions to make sum of every pair of consecutive array elements at most K | Function to count minimum number of insertions required to make sum of every pair of adjacent elements at most K ; Stores if it is possible to make sum of each pair of adjacent elements at most K ; Stores the count of insertions ; Stores the previous value to index i ; Traverse the array ; If arr [ i ] is greater than or equal to K ; Mark possible 0 ; If last + arr [ i ] is greater than K ; Increment res by 1 ; Assign arr [ i ] to last ; If possible to make the sum of pairs of adjacent elements at most K ; Otherwise prvar " - 1" ; Driver Code
function minimumInsertions ( arr , N , K ) { var possible = true ; var res = 0 ; var last = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= K ) { possible = false ; break ; } if ( last + arr [ i ] > K ) res ++ ; last = arr [ i ] ; } if ( possible ) { document . write ( res ) ; } else { document . write ( " " ) ; } } var arr = [ 1 , 2 , 3 , 4 , 5 ] ; var K = 6 ; var N = arr . length ; minimumInsertions ( arr , N , K ) ;
Maximum sum of K | Function to count the number of distinct elements present in the array ; Insert all elements into the Set ; Return the size of set ; Function that finds the maximum sum of K - length subarray having same unique elements as arr [ ] ; Not possible to find a subarray of size K ; Initialize Set ; Calculate sum of the distinct elements ; If the set size is same as the count of distinct elements ; Update the maximum value ; Driver code ; Stores the count of distinct elements
function distinct ( arr , n ) { var mpp = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { mpp . set ( arr [ i ] , 1 ) ; } return mpp . size ; } function maxSubSum ( arr , n , k , totalDistinct ) { if ( k > n ) return 0 ; var maxm = 0 , sum = 0 ; for ( var i = 0 ; i < n - k + 1 ; i ++ ) { sum = 0 ; var st = new Set ( ) ; for ( var j = i ; j < i + k ; j ++ ) { sum += arr [ j ] ; st . add ( arr [ j ] ) ; } if ( st . size == totalDistinct ) maxm = Math . max ( sum , maxm ) ; } return maxm ; } var arr = [ 7 , 7 , 2 , 4 , 2 , 7 , 4 , 6 , 6 , 6 ] ; var K = 6 ; var N = arr . length ; var totalDistinct = distinct ( arr , N ) ; document . write ( maxSubSum ( arr , N , K , totalDistinct ) ) ;
Maximum sum of K | Function to count the number of distinct elements present in the array ; Insert array elements into set ; Return the st size ; Function to calculate maximum sum of K - length subarray having same unique elements as arr [ ] ; Not possible to find an subarray of length K from an N - sized array , if K > N ; Traverse the array ; Update the mp ; If i >= K , then decrement arr [ i - K ] element 's one  occurence ; If frequency of any element is 0 then remove the element ; If mp size is same as the count of distinct elements of array arr [ ] then update maximum sum ; Function that finds the maximum sum of K - length subarray having same number of distinct elements as the original array ; Size of array ; Stores count of distinct elements ; Print maximum subarray sum ; Driver Code ; Function Call
function distinct ( arr , N ) { var st = new Set ( ) ; for ( var i = 0 ; i < N ; i ++ ) { st . add ( arr [ i ] ) ; } return st . size ; } function maxSubarraySumUtil ( arr , N , K , totalDistinct ) { if ( K > N ) return 0 ; var mx = 0 ; var sum = 0 ; var mp = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( mp . has ( arr [ i ] ) ) mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) else mp . set ( arr [ i ] , 1 ) sum += arr [ i ] ; if ( i >= K ) { if ( mp . has ( arr [ i - K ] ) ) mp . set ( arr [ i - K ] , mp . get ( arr [ i - K ] ) - 1 ) sum -= arr [ i - K ] ; if ( mp . has ( arr [ i - K ] ) && mp . get ( arr [ i - K ] ) == 0 ) mp . delete ( arr [ i - K ] ) ; } if ( mp . size == totalDistinct ) mx = Math . max ( mx , sum ) ; } return mx ; } function maxSubarraySum ( arr , K ) { var N = arr . length ; var totalDistinct = distinct ( arr , N ) ; document . write ( maxSubarraySumUtil ( arr , N , K , totalDistinct ) ) ; } var arr = [ 7 , 7 , 2 , 4 , 2 , 7 , 4 , 6 , 6 , 6 ] ; var K = 6 ; maxSubarraySum ( arr , K ) ;
Number of Irreflexive Relations on a Set | Javascript program for the above approach ; Function to calculate x ^ y modulo 1000000007 in O ( log y ) ; Stores the result of x ^ y ; Update x if it exceeds mod ; If x is divisible by mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the resultant value of x ^ y ; Function to count the number of irreflixive relations in a set consisting of N elements ; Return the resultant count ; Driver code
let mod = 1000000007 ; function power ( x , y ) { let res = 1 ; x = x % mod ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } function irreflexiveRelation ( N ) { return power ( 2 , N * N - N ) ; } let N = 2 ; document . write ( irreflexiveRelation ( N ) ) ;
Count Arithmetic Progressions having sum N and common difference equal to 1 | Function to count all possible AP series with common difference 1 and sum of elements equal to N ; Stores the count of AP series ; Traverse through all factors of 2 * N ; Check for the given conditions ; Increment count ; Print count - 1 ; Given value of N ; Function call to count required number of AP series
function countAPs ( N ) { let count = 0 ; for ( let i = 1 ; i * i <= 2 * N ; i ++ ) { let res = 2 * N ; if ( res % i == 0 ) { let op = res / i - i + 1 ; if ( op % 2 == 0 ) { count ++ ; } if ( i * i != res && ( i - res / i + 1 ) % 2 == 0 ) { count ++ ; } } } document . write ( count - 1 ) ; } let N = 963761198400 ; countAPs ( N ) ;
Check if concatenation of first and last digits forms a prime number or not for each array element | Stores if i is prime ( 1 ) or non - prime ( 0 ) ; Function to build sieve array ; Inititalize all the values in sieve equals to 1 ; Sieve of Eratosthenes ; If current number is prime ; Set all multiples as non - prime ; Function to check if the numbers formed by combining first and last digits generates a prime number or not ; Check if any of the numbers formed is a prime number or not ; Traverse the array of queries ; Extract the last digit ; Extract the first digit ; If any of the two numbers is prime ; Otherwise ; Driver Code ; Computes and stores primes using Sieve ; Function call to perform queries
var sieve = Array . from ( { length : 105 } , ( _ , i ) => 0 ) ; function buildSieve ( ) { for ( i = 2 ; i < 100 ; i ++ ) sieve [ i ] = 1 ; for ( i = 2 ; i < 100 ; i ++ ) { if ( sieve [ i ] == 1 ) { for ( j = i * i ; j < 100 ; j += i ) sieve [ j ] = 0 ; } } } function isAnyPrime ( first , last ) { var num1 = first * 10 + last ; var num2 = last * 10 + first ; if ( sieve [ num1 ] == 1 sieve [ num2 ] == 1 ) return true ; else return false ; } function performQueries ( q ) { for ( i = 0 ; i < q . length ; i ++ ) { var A = q [ i ] ; var last = A % 10 ; var first ; while ( A >= 10 ) A = A / 10 ; first = A ; if ( isAnyPrime ( first , last ) ) document . write ( " " ) ; else document . write ( " " ) ; } } var q = [ 30 , 66 ] ; buildSieve ( ) ; performQueries ( q ) ;
Check if a given number N has at least one odd divisor not exceeding N | Function to check whether N has at least one odd divisor not exceeding N - 1 or not ; Stores the value of N ; Reduce the given number N by dividing it by 2 ; If N is divisible by an odd divisor i ; Check if N is an odd divisor after reducing N by dividing it by 2 ; Otherwise ; Driver Code ; Function Call
function oddDivisor ( N ) { var X = N ; var i ; while ( N % 2 == 0 ) { N /= 2 ; } for ( i = 3 ; i * i <= X ; i += 2 ) { if ( N % i == 0 ) { return " " ; } } if ( N != X ) return " " ; return " " ; } var N = 10 ; document . write ( oddDivisor ( N ) ) ;