codeparrot/xlcost-text-to-code huggingface dataset:
Maximum sum of K | Javascript program for the above approach ; Function to print the sum of subarray of length K having maximum distinct prime factors ; If K exceeds N ; Stores the count of distinct primes ; True , if index ' i ' is a prime ; Initialize the count of factors to 0 and set all indices as prime ; If i is prime ; Number is prime ; Count of factors of a prime number is 1 ; Increment CountDistinct as the factors of i ; Mark its multiple non - prime ; Compute sum of first K - length subarray ; Compute sums of remaining windows by removing first element of the previous window and adding last element of the current window ; Print the maximum sum ; Given array ; Given size of subarray ; Size of the array
let MAX = 100001 function maxSum ( arr , N , K ) { if ( N < K ) { document . write ( " " ) ; return - 1 ; } let CountDistinct = new Array ( MAX + 1 ) ; let prime = new Array ( MAX + 1 ) ; for ( let i = 0 ; i <= MAX ; i ++ ) { CountDistinct [ i ] = 0 ; prime [ i ] = true ; } for ( let i = 2 ; i <= MAX ; i ++ ) { if ( prime [ i ] == true ) { CountDistinct [ i ] = 1 ; for ( let j = i * 2 ; j <= MAX ; j += i ) { CountDistinct [ j ] ++ ; prime [ j ] = false ; } } } let Maxarr_sum = 0 , DistPrimeSum = 0 ; for ( let i = 0 ; i < K ; i ++ ) { Maxarr_sum += arr [ i ] ; DistPrimeSum += CountDistinct [ arr [ i ] ] ; } let curr_sum = DistPrimeSum ; let curr_arrSum = Maxarr_sum ; for ( let i = K ; i < N ; i ++ ) { curr_sum += CountDistinct [ arr [ i ] ] - CountDistinct [ arr [ i - K ] ] ; curr_arrSum += arr [ i ] - arr [ i - K ] ; if ( curr_sum > DistPrimeSum ) { DistPrimeSum = curr_sum ; Maxarr_sum = curr_arrSum ; } else if ( curr_sum == DistPrimeSum ) { Maxarr_sum = Math . max ( curr_arrSum , Maxarr_sum ) ; } } document . write ( Maxarr_sum ) ; } let arr = [ 1 , 4 , 2 , 10 , 3 ] ; let K = 3 ; let N = arr . length maxSum ( arr , N , K ) ;
Count Pronic numbers from a given range | Function to check if x is a Pronic Number or not ; Check for Pronic Number by multiplying consecutive numbers ; Function to count pronic numbers in the range [ A , B ] ; Initialise count ; Iterate from A to B ; If i is pronic ; Increment count ; Print count ; Driver Code ; Function call to count pronic numbers in the range [ A , B ]
function checkPronic ( x ) { for ( let i = 0 ; i <= parseInt ( Math . sqrt ( x ) ) ; i ++ ) { if ( x == i * ( i + 1 ) ) { return true ; } } return false ; } function countPronic ( A , B ) { let count = 0 ; for ( let i = A ; i <= B ; i ++ ) { if ( checkPronic ( i ) ) { count ++ ; } } document . write ( count ) ; } let A = 3 , B = 20 ; countPronic ( A , B ) ;
Count Pronic numbers from a given range | Function to count pronic numbers in the range [ A , B ] ; Check upto sqrt N ; If product of consecutive numbers are less than equal to num ; Return N - 1 ; Function to count pronic numbers in the range [ A , B ] ; Subtract the count of pronic numbers which are <= ( A - 1 ) from the count f pronic numbers which are <= B ; Driver Code ; Function call to count pronic numbers in the range [ A , B ]
function pronic ( num ) { var N = parseInt ( Math . sqrt ( num ) ) ; if ( N * ( N + 1 ) <= num ) { return N ; } return N - 1 ; } function countPronic ( A , B ) { return pronic ( B ) - pronic ( A - 1 ) ; } var A = 3 ; var B = 20 ; document . write ( countPronic ( A , B ) ) ;
Count integers from a given range with no odd divisors | Function to count integers in the range 1 to N having no odd divisors ; Traverse the array ; Stores the nearest power of two less than arr [ i ] ; Stores the count of integers with no odd divisor for each query ; Iterate until powerOfTwo is less then or equal to arr [ i ] ; Print the answer for the current element ; Given array ; Size of the array
function oddDivisors ( arr , N ) { for ( i = 0 ; i < N ; i ++ ) { var powerOfTwo = 2 ; var count = 0 ; while ( powerOfTwo <= arr [ i ] ) { count ++ ; powerOfTwo = 2 * powerOfTwo ; } document . write ( count + " " ) ; } return ; } var arr = [ 15 , 16 , 20 , 35 ] ; var N = arr . length ; oddDivisors ( arr , N ) ;
Check if all array elements can be reduced to 0 by repeatedly reducing pairs of consecutive elements by their minimum | Function to check if it is possible to convert the array ; Traverse the array range [ 1 , N - 1 ] ; If arr [ i ] < arr [ i - 1 ] ; Otherwise ; Decrement arr [ i ] by arr [ i - 1 ] ; If arr [ n - 1 ] is not equal to zero ; Otherwise ; Driver Code ; Function Call
function checkPossible ( arr , n ) { for ( i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) { document . write ( " " ) ; return ; } else { arr [ i ] -= arr [ i - 1 ] ; arr [ i - 1 ] = 0 ; } } if ( arr [ n - 1 ] == 0 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var arr = [ 2 , 3 , 3 , 4 , 2 ] ; var N = arr . length ; checkPossible ( arr , N ) ;
Nearest smaller power of 2 for every digit of a number | Function to find the nearest power of two for every digit of a given number ; Converting number to string ; Traverse the array ; Calculate log base 2 of the current digit s [ i ] ; Highest power of 2 <= s [ i ] ; ASCII conversion ; Driver Code
function highestPowerOfTwo ( num ) { var s = num . toString ( ) ; for ( var i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] === " " ) { document . write ( " " ) ; continue ; } var lg = parseInt ( Math . log2 ( s [ i ] . charCodeAt ( 0 ) - 48 ) ) ; var p = Math . pow ( 2 , lg ) ; document . write ( String . fromCharCode ( p + 48 ) ) ; } } var num = 4317 ; highestPowerOfTwo ( num ) ;
Maximum prefix sum after K reversals of a given array | Function to find the maximum prefix sum after K reversals of the array ; Stores the required sum ; If K is odd , reverse the array ; Store current prefix sum of array ; Traverse the array arr [ ] ; Add arr [ i ] to currsum ; Update maximum prefix sum till now ; Print the answer ; Driver Code ; Function Call
function maxSumAfterKReverse ( arr , K , N ) { let sum = Number . MIN_VALUE ; if ( K % 2 != 0 ) arr . reverse ( ) ; let currsum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { currsum += arr [ i ] ; sum = Math . max ( sum , currsum ) ; } document . write ( sum ) ; } let arr = [ 1 , 5 , 8 , 9 , 11 , 2 ] ; let K = 1 ; let N = arr . length ; maxSumAfterKReverse ( arr , K , N ) ;
Print all submasks of a given mask | Function to print the submasks of N ; Driver Code
function SubMasks ( N ) { for ( let S = N ; S > 0 ; S = ( S - 1 ) & N ) { document . write ( S + " " ) ; } } let N = 25 ; SubMasks ( N ) ;
Count pairs from a given range having equal Bitwise OR and XOR values | Javascript program for the above approach ; Function to calculate ( x ^ y ) % MOD ; Initialize result ; Update x if it is more than or equal to MOD ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Return ( x ^ y ) % MOD ; Function to count total pairs ; The upper bound is 2 ^ N ; Stores the count of pairs ; Generate all possible pairs ; Find XOR of both integers ; Find OR of both integers ; If both are equal ; Increment count ; Print count % MOD ; Driver Code ; Function Call
MOD = 1000000007 ; function power ( x , y ) { var res = 1 ; x = x % MOD ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % MOD ; y = y >> 1 ; x = ( x * x ) % MOD ; } return res ; } function countPairs ( N ) { var high = power ( 2 , N ) ; var count = 0 ; for ( i = 0 ; i < high ; i ++ ) { for ( j = 0 ; j < high ; j ++ ) { var X = ( i ^ j ) ; var Y = ( i j ) ; if ( X == Y ) { count ++ ; } } } document . write ( count % MOD ) ; } var N = 10 ; countPairs ( N ) ;
Count pairs from a given range having equal Bitwise OR and XOR values | Javascript program for the above approach ; Function to find the value of ( x ^ y ) % MOD ; Initialize result ; Update x if it is more than or equal to MOD ; If y is odd , multiply x with result ; y must be even now , then update y / 2 ; Return ( x ^ y ) % MOD ; Function to count total pairs ; Finding 3 ^ N % 10 ^ 9 + 7 ; Driver Code ; Function Call
var MOD = 1000000007 ; function power ( x , y ) { var res = 1 ; x = x % MOD ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % MOD ; y = y >> 1 ; x = ( x * x ) % MOD ; } return res ; } function countPairs ( N ) { document . write ( power ( 3 , N ) ) ; } var N = 10 ; countPairs ( N ) ;
Minimize swaps required to place largest and smallest array elements at first and last array indices | Function to find the minimum count of adjacent swaps to move largest and smallest element at the first and the last index of the array , respectively ; Stores the smallest array element ; Stores the smallest array element ; Stores the last occurrence of the smallest array element ; Stores the first occurrence of the largest array element ; Traverse the array arr [ ] ; If a [ i ] is less than min_element ; Update min_element ; Update min_ind ; If a [ i ] is greater than max_element ; Update max_element ; Update max_ind ; If max_ind is equal to min_ind ; Return 0 ; If max_ind is greater than min_ind ; Otherwise ; Input ; Prlet the result
function minimumMoves ( a , n ) { let min_element = Number . MAX_VALUE ; let max_element = Number . MIN_VALUE ; let min_ind = - 1 ; let max_ind = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] <= min_element ) { min_element = a [ i ] ; min_ind = i ; } if ( a [ i ] > max_element ) { max_element = a [ i ] ; max_ind = i ; } } if ( max_ind == min_ind ) { return 0 ; } else if ( max_ind > min_ind ) { return max_ind + ( n - min_ind - 2 ) ; } else { return max_ind + n - min_ind - 1 ; } } let arr = [ 35 , 46 , 17 , 23 ] ; let N = arr . length ; document . write ( minimumMoves ( arr , N ) + " " ) ;
Find the index of the smallest element to be removed to make sum of array divisible by K | Function to find index of the smallest array element required to be removed to make sum divisible by K ; Stores sum of array elements ; Stores index of the smallest element removed from the array to make sum divisible by K ; Stores the smallest element removed from the array to make sum divisible by K ; Traverse the array , arr [ ] ; Update sum ; Traverse the array arr [ ] ; Calculate remaining sum ; If sum is divisible by K ; If res == - 1 or mini is greater than arr [ i ] ; Update res and mini ; Driver Code
function findIndex ( arr , n , K ) { let sum = 0 ; let res = - 1 ; let mini = parseInt ( 1e9 ) ; for ( let i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } for ( let i = 0 ; i < n ; i ++ ) { let temp = sum - arr [ i ] ; if ( temp % K == 0 ) { if ( res == - 1 mini > arr [ i ] ) { res = i + 1 ; mini = arr [ i ] ; } } } return res ; } let arr = [ 14 , 7 , 8 , 2 , 4 ] ; let K = 7 ; let N = arr . length ; document . write ( findIndex ( arr , N , K ) ) ;
Count subarrays having a single distinct element that can be obtained from a given array | Function to count subarrays of single distinct element into which given array can be split ; Stores the count ; Stores frequency of array elements ; Traverse the array ; Traverse the map ; Increase count of subarrays by ( frequency - 1 ) ; Driver Code
function divisionalArrays ( arr , N ) { var sum = N ; 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 ) ; } } mp . forEach ( ( value , key ) => { if ( value > 1 ) { sum += value - 1 ; } } ) ; document . write ( sum ) ; } var arr = [ 1 , 1 , 3 ] ; var N = arr . length ; divisionalArrays ( arr , N ) ;
Count inversions in a sequence generated by appending given array K times | Function to count the number of inversions in K copies of given array ; Stores count of inversions in the given array ; Stores the count of pairs of distinct array elements ; Traverse the array ; Generate each pair ; Check for each pair , if the condition is satisfied or not ; If pairs consist of distinct elements ; Count inversiosn in the sequence ; Print the answer ; Given array ; Given K ; Size of the array
function totalInversions ( arr , K , N ) { let inv = 0 ; let X = 0 ; let i , j ; for ( i = 0 ; i < N ; i ++ ) { for ( j = 0 ; j < N ; j ++ ) { if ( arr [ i ] > arr [ j ] && i < j ) inv ++ ; if ( arr [ i ] > arr [ j ] ) X ++ ; } } let totalInv = X * K * ( K - 1 ) / 2 + inv * K ; document . write ( totalInv ) ; } let arr = [ 2 , 1 , 3 ] ; let K = 3 ; let N = arr . length ; totalInversions ( arr , K , N ) ;
Count ways to generate an array having distinct elements at M consecutive indices | Modular function to calculate factorial ; Stores factorial of N ; Iterate over the range [ 1 , N ] ; Update result ; Function to count ways to replace array elements having 0 s with non - zero elements such that any M consecutive elements are distinct ; Store m consecutive distinct elements such that arr [ i ] is equal to B [ i % M ] ; Stores frequency of array elements ; Traverse the array arr [ ] ; If arr [ i ] is non - zero ; If B [ i % M ] is equal to 0 ; Update B [ i % M ] ; Update frequency of arr [ i ] ; If a duplicate element found in M consecutive elements ; Handling the case of inequality ; Stores count of 0 s in B [ ] ; Traverse the array , B [ ] ; If B [ i ] is 0 ; Update cnt ; Calculate factorial ; Given M ; Given array ; Size of the array ; Function Call
function Fact ( N ) { let result = 1 ; for ( let i = 1 ; i <= N ; i ++ ) { result = ( result * i ) ; } return result ; } function numberOfWays ( M , arr , N ) { let B = new Array ( M ) . fill ( 0 ) ; let counter = new Array ( M + 1 ) . fill ( 0 ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != 0 ) { if ( B [ i % M ] == 0 ) { B [ i % M ] = arr [ i ] ; counter [ arr [ i ] ] ++ ; if ( counter [ arr [ i ] ] > 1 ) { document . write ( 0 ) ; return ; } } else if ( B [ i % M ] != arr [ i ] ) { document . write ( 0 ) ; return ; } } } let cnt = 0 ; for ( let i = 0 ; i < M ; i ++ ) { if ( B [ i ] == 0 ) { cnt ++ ; } } document . write ( Fact ( cnt ) ) ; } let M = 4 ; let arr = [ 1 , 0 , 3 , 0 , 0 ] ; let N = arr . length ; numberOfWays ( M , arr , N ) ;
Maximize sum by traversing diagonally from each cell of a given Matrix | Function to find the maximum sum ; Loop to traverse through the upper triangular matrix and update the maximum sum to ans ; Traverse through the lower triangular matrix ; Given matrix ; Given dimension
function MaximumSum ( arr , n ) { let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let x = 0 , y = i , sum = 0 ; for ( let j = i ; j < n ; j ++ ) { sum += arr [ x ++ ] [ y ++ ] ; } if ( sum > ans ) ans = sum ; } for ( let i = 1 ; i < n ; i ++ ) { let x = i , y = 0 , sum = 0 ; for ( let j = i ; j < n ; j ++ ) { sum += arr [ x ++ ] [ y ++ ] ; } if ( sum > ans ) ans = sum ; } return ans ; } let arr = [ [ 1 , 2 , 3 ] , [ 3 , 5 , 10 ] , [ 1 , 3 , 5 ] ] ; let n = arr . length ; document . write ( MaximumSum ( arr , n ) ) ;
Count array elements exceeding all previous elements as well as the next array element | Function to count array elements satisfying the given condition ; If there is only one array element ; Traverse the array ; Update the maximum element encountered so far ; Count the number of array elements strictly greater than all previous and immediately next elements ; Print the count ; Given array ; Size of the array
function numberOfIntegers ( arr , N ) { let cur_max = 0 , count = 0 ; if ( N == 1 ) { count = 1 ; } else { for ( let i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] > cur_max ) { cur_max = arr [ i ] ; if ( arr [ i ] > arr [ i + 1 ] ) { count ++ ; } } } if ( arr [ N - 1 ] > cur_max ) count ++ ; } document . write ( count ) ; } let arr = [ 1 , 2 , 0 , 7 , 2 , 0 , 2 , 0 ] ; let N = arr . length ; numberOfIntegers ( arr , N ) ;
Count ways to represent N as sum of powers of 2 | JavaScript program for above implementation ; Base Cases ; Check if 2 ^ k can be used as one of the numbers or not ; Otherwise ; Count number of ways to N using 2 ^ k - 1 ; Driver Code
function numberOfWays ( n , k ) { if ( n == 0 ) return 1 ; if ( k == 0 ) return 1 ; if ( n >= Math . pow ( 2 , k ) ) { let curr_val = Math . pow ( 2 , k ) ; return numberOfWays ( n - curr_val , k ) + numberOfWays ( n , k - 1 ) ; } else return numberOfWays ( n , k - 1 ) ; } let n = 4 ; let k = Math . log2 ( n ) ; document . write ( numberOfWays ( n , k ) + " " ) ;
Count ways to obtain triplets with positive product consisting of at most one negative element | Function to calculate possible number of triplets ; counting frequency of positive numbers in array ; If current array element is positive ; Increment frequency ; Select a triplet from freq elements such that i < j < k . ; Driver Code
function possibleTriplets ( arr , N ) { var freq = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( arr [ i ] > 0 ) { freq ++ ; } } return ( freq * 1 * ( freq - 1 ) * ( freq - 2 ) ) / 6 ; } var arr = [ 2 , 5 , - 9 , - 3 , 6 ] ; var N = arr . length ; document . write ( possibleTriplets ( arr , N ) ) ;
Maximize difference between sum of even and odd | Function to find the maximum possible difference between sum of even and odd indices ; Convert arr [ ] into 1 - based indexing ; Reverse the array ; Convert arr [ ] into 1 based index ; Reverse the array ; Stores maximum difference between sum of even and odd indexed elements ; Traverse the array ; If arr [ i ] is local maxima ; Update maxDiff ; If arr [ i ] is local minima ; Update maxDiff ; Driver Code ; Size of array ; Function Call
function maxPossibleDiff ( arr , N ) { arr . push ( - 1 ) ; arr . reverse ( ) ; arr . push ( - 1 ) ; arr . reverse ( ) ; var maxDiff = 0 ; for ( var i = 1 ; i <= N ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] && arr [ i ] > arr [ i + 1 ] ) { maxDiff += arr [ i ] ; } if ( arr [ i ] < arr [ i - 1 ] && arr [ i ] < arr [ i + 1 ] ) { maxDiff -= arr [ i ] ; } } document . write ( maxDiff ) ; } var arr = [ 3 , 2 , 1 , 4 , 5 , 2 , 1 , 7 , 8 , 9 ] ; var N = arr . length ; maxPossibleDiff ( arr , N ) ;
Reverse all elements of given circular array starting from index K | Function to print the array arr [ ] ; Print the array ; Function to reverse elements of given circular array starting from index k ; Initialize two variables as start = k and end = k - 1 ; Initialize count = N / 2 ; Loop while count > 0 ; Swap the elements at index ( start % N ) and ( end % N ) ; Update the start and end ; If end equals to - 1 set end = N - 1 ; Print the circular array ; Driver Code ; Function Call
function printArray ( arr , N ) { for ( let i = 0 ; i < N ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } function reverseCircularArray ( arr , N , K ) { let start = K , end = K - 1 ; let count = Math . floor ( N / 2 ) ; while ( count -- ) { let temp = arr [ start % N ] ; arr [ start % N ] = arr [ end % N ] ; arr [ end % N ] = temp ; start ++ ; end -- ; if ( end === - 1 ) { end = N - 1 ; } } printArray ( arr , N ) ; } let arr = [ 3 , 5 , 2 , 4 , 1 ] ; let K = 2 ; let N = arr . length ; reverseCircularArray ( arr , N , K ) ;
Generate a Matrix such that given Matrix elements are equal to Bitwise OR of all corresponding row and column elements of generated Matrix | Function to find the matrix , A satisfying the given conditions ; Store the final matrix ; Initialize all the elements of the matrix A with 1 ; Traverse the matrix B row - wise ; If B [ i ] [ j ] is equal to 0 ; Mark all the elements of ith row of A as 0 ; Mark all the elements of jth column of A as 0 ; Check if the matrix B can be made using matrix A ; Store the bitwise OR of all elements of A in ith row and jth column ; Traverse through ith row ; Traverse through jth column ; If B [ i ] [ j ] is not equal to c , print " Not ▁ Possible " ; Print the final matrix A ; Driver Code ; Function Call
function findOriginalMatrix ( B , N , M ) { var A = Array ( N ) ; for ( var i = 0 ; i < N ; i ++ ) A [ i ] = Array ( M ) . fill ( 0 ) ; for ( i = 0 ; i < N ; ++ i ) { for ( j = 0 ; j < M ; ++ j ) { A [ i ] [ j ] = 1 ; } } for ( i = 0 ; i < N ; ++ i ) { for ( j = 0 ; j < M ; ++ j ) { if ( B [ i ] [ j ] == 0 ) { for ( k = 0 ; k < M ; ++ k ) { A [ i ] [ k ] = 0 ; } for ( k = 0 ; k < N ; ++ k ) { A [ k ] [ j ] = 0 ; } } } } for ( i = 0 ; i < N ; ++ i ) { for ( j = 0 ; j < M ; ++ j ) { var c = 0 ; for ( k = 0 ; k < M ; ++ k ) { if ( c == 1 ) break ; c += A [ i ] [ k ] ; } for ( k = 0 ; k < N ; ++ k ) { if ( c == 1 ) break ; c += A [ k ] [ j ] ; } if ( c != B [ i ] [ j ] ) { document . write ( " " ) ; return ; } } } for ( i = 0 ; i < N ; ++ i ) { for ( j = 0 ; j < M ; ++ j ) { document . write ( A [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } var B = [ [ 1 , 1 , 1 ] , [ 1 , 1 , 1 ] ] ; var N = B . length ; var M = B [ 0 ] . length ; findOriginalMatrix ( B , N , M ) ;
Minimize cost to make X and Y equal by given increments | Function to find gcd of x and y ; Function to find lcm of x and y ; Function to find minimum Cost ; Subtracted initial cost of x ; Subtracted initial cost of y ; Driver Code ; Returns the minimum cost required
function gcd ( x , y ) { if ( y == 0 ) return x ; return gcd ( y , x % y ) ; } function lcm ( x , y ) { return ( x * y ) / gcd ( x , y ) ; } function minimumCost ( x , y ) { var lcm_ = lcm ( x , y ) ; var costx = ( lcm_ - x ) / x ; var costy = ( lcm_ - y ) / y ; return costx + costy ; } var x = 5 , y = 17 ; document . write ( minimumCost ( x , y ) + " " ) ;
Find GCD between the sum of two given integers raised to the power of N and their difference | javascript program for the above approach ; Function to find the value of ( a ^ n ) % d ; Stores the value of ( a ^ n ) % d ; Calculate the value of ( a ^ n ) % d ; If n is odd ; Update res ; Update a ; Update n ; Function to find the GCD of ( p ^ n + q ^ n ) and p - q mod d ; If p and q are equal ; Stores GCD of ( p ^ n + q ^ n ) and ( p - q ) mod d ; Stores the value of ( p - q ) ; Stores square root of num ; Find the divisors of num . ; If i divides num ; Stores power of ( p ^ n ) mod i ; Stores power of ( q ^ n ) mod i ; Stores power of ( p ^ n + q ^ n ) mod i ; If ( p ^ n + q ^ n ) is divisible by i ; Calculate the largest divisor . ; If i divides num , ( num / i ) also divides num . Hence , calculate temp . ; If ( p ^ n + q ^ n ) is divisible by ( num / i ) ; Calculate the largest divisor . ; Given p , q and n ; Function Call
const mod = 1000000007 ; function power ( a , n , d ) { var res = 1 ; while ( n != 0 ) { if ( ( n % 2 ) != 0 ) { res = ( ( res % d ) * ( a % d ) ) % d ; } a = ( ( a % d ) * ( a % d ) ) % d ; n /= 2 ; } return res ; } function gcd ( p , q , n ) { if ( p == q ) { return ( power ( p , n , mod ) + power ( q , n , mod ) ) % mod ; } var candidate = 1 ; var num = p - q ; var sq = parseInt ( Math . sqrt ( num ) ) ; for ( i = 1 ; i <= sq ; ++ i ) { if ( num % i == 0 ) { var X = power ( p , n , i ) ; var Y = power ( q , n , i ) ; var temp = ( X + Y ) % i ; if ( temp == 0 ) { candidate = Math . max ( candidate , i ) ; } temp = ( power ( p , n , num / i ) + power ( q , n , num / i ) ) % ( num / i ) ; if ( temp == 0 ) { candidate = Math . max ( candidate , num / i ) ; } } } return candidate % mod ; } var p , q , n ; p = 10 ; q = 6 ; n = 5 ; document . write ( gcd ( p , q , n ) ) ;
Split given isosceles triangle of height H into N equal parts | Function to divide the isosceles triangle in equal parts by making N - 1 cuts parallel to the base ; Iterate over the range [ 1 , n - 1 ] ; Given N ; Given H ; Function call
function findPolet ( n , h ) { for ( let i = 1 ; i < n ; i ++ ) document . write ( Math . sqrt ( i / ( n * 1.0 ) ) * h + " " ) ; } let n = 3 ; let h = 2 ; findPolet ( n , h ) ; ;
Count all possible values of K less than Y such that GCD ( X , Y ) = GCD ( X + K , Y ) | Function to find the gcd of a and b ; Function to find the number of Ks ; Find gcd ; Calculating value of totient function for n ; Given X and Y
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function calculateK ( x , y ) { var g = gcd ( x , y ) ; var n = y / g ; var res = n ; for ( i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { res -= ( res / i ) ; while ( n % i == 0 ) n /= i ; } } if ( n != 1 ) res -= ( res / n ) ; return res ; } var x = 3 , y = 15 ; document . write ( calculateK ( x , y ) + " " ) ;
Check if a Float value is equivalent to an Integer value | Function to check if N is equivalent to an integer ; Convert float value of N to integer ; If N is not equivalent to any integer ; driver function
function isInteger ( N ) { let X = Math . floor ( N ) ; let temp2 = N - X ; if ( temp2 > 0 ) { return false ; } return true ; } let N = 1.5 ; if ( isInteger ( N ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }