codeparrot/xlcost-text-to-code huggingface dataset:
Find minimum GCD of all pairs in an array | JavaScript program to find the minimum GCD of any pair in the array ; Function returns the Minimum GCD of any pair ; Finding GCD of all the elements in the array . ; Driver code
function calGCD ( a , b ) { if ( ! b ) { return a ; } return calGCD ( b , a % b ) ; } function MinimumGCD ( arr , n ) { var g = 0 ; var i ; for ( i = 0 ; i < n ; i ++ ) { g = calGCD ( g , arr [ i ] ) ; } return g ; } var arr = [ 2 , 4 , 6 , 8 , 3 ] ; var N = arr . length ; document . write ( MinimumGCD ( arr , N ) ) ;
Minimum steps to reach the Nth stair in jumps of perfect power of 2 | Function to count the number of jumps required to reach Nth stairs . ; Till N becomes 0 ; Removes the set bits from the right to left ; Number of stairs ; Function Call
function stepRequired ( N ) { let cnt = 0 ; while ( N ) { N = N & ( N - 1 ) ; cnt ++ ; } return cnt ; } let N = 23 ; document . write ( stepRequired ( N ) ) ;
Count of total subarrays whose sum is a Fibonacci Numbers | Function to check whether a number is perfect square or not ; Function to check whether a number is fibonacci number or not ; If 5 * n * n + 4 or 5 * n * n - 5 is a perfect square , then the number is Fibonacci ; Function to count the subarray with sum fibonacci number ; Traverse the array arr [ ] to find the sum of each subarray ; To store the sum ; Check whether sum of subarray between [ i , j ] is fibonacci or not ; Driver Code ; Function Call
function isPerfectSquare ( x ) { var s = parseInt ( Math . sqrt ( x ) ) ; return ( s * s == x ) ; } function isFibonacci ( n ) { return isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ; } function fibonacciSubarrays ( arr , n ) { var count = 0 ; for ( var i = 0 ; i < n ; ++ i ) { var sum = 0 ; for ( var j = i ; j < n ; ++ j ) { sum += arr [ j ] ; if ( isFibonacci ( sum ) ) { ++ count ; } } } document . write ( count ) ; } var arr = [ 6 , 7 , 8 , 9 ] ; var n = arr . length ; fibonacciSubarrays ( arr , n ) ;
Program to print numbers from N to 1 in reverse order | Recursive function to print from N to 1 ; if N is less than 1 then return void function ; recursive call of the function ; Driver code
function PrintReverseOrder ( N ) { if ( N <= 0 ) { return ; } else { document . write ( N + " " ) ; PrintReverseOrder ( N - 1 ) ; } } let N = 5 ; PrintReverseOrder ( N ) ;
Find the XOR of first half and second half elements of an array | Function to find the xor of the first half elements and second half elements of an array ; xor of elements in FirstHalfXOR ; xor of elements in SecondHalfXOR ; Driver Code ; Function call
function XOROfElements ( arr , n ) { var FirstHalfXOR = 0 ; var SecondHalfXOR = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( i < parseInt ( n / 2 ) ) FirstHalfXOR ^= arr [ i ] ; else SecondHalfXOR ^= arr [ i ] ; } document . write ( FirstHalfXOR + " " + SecondHalfXOR + " " ) ; } var arr = [ 20 , 30 , 50 , 10 , 55 , 15 , 42 ] ; var N = arr . length ; XOROfElements ( arr , N ) ;
Minimum volume of cone that can be circumscribed about a sphere of radius R | Function to find the volume of the cone ; r = radius of cone h = height of cone Volume of cone = ( 1 / 3 ) * ( 3.14 ) * ( r * r ) * ( h ) we get radius of cone from the derivation is root ( 2 ) times multiple of R we get height of cone from the derivation is 4 times multiple of R ; Driver code
function Volume_of_cone ( R ) { let V = ( ( 1 / 3.0 ) * ( 3.14 ) * ( 2 * ( R * R ) ) * ( 4 * R ) ) ; return V ; } let R = 10.0 ; document . write ( Volume_of_cone ( R ) ) ;
Program to find Surface Area and Volume of Octagonal Prism | Function to find the Volume of octagonal prism ; Formula to calculate volume = ( area * h ) ; Display volume ; Function to find the surface area of octagonal prism ; Formula to calculate Surface area ; Driver Code
function find_volume ( area , h ) { let Volume = ( area * h ) ; document . write ( " " + Volume + " " ) ; } function find_Surface_area ( area , a , h ) { let Surface_area = ( 2 * area ) + ( 8 * a * h ) ; document . write ( " " + Surface_area + " " ) ; } let h = 1 ; let a = 6 ; let d = 2 ; let area = 2 * a * d ; find_Surface_area ( area , a , h ) ; find_volume ( area , h ) ;
Count of pairs upto N such whose LCM is not equal to their product for Q queries | javascript program to find the count of pairs from 1 to N such that their LCM is not equal to their product ; To store Euler 's Totient Function ; To store prefix sum table ; Compute Totients of all numbers smaller than or equal to N ; Make phi [ 1 ] = 0 since 1 cannot form any pair ; Initialise all remaining phi with i ; Compute remaining phi ; If phi [ p ] is not computed already , then number p is prime ; phi of prime number is p - 1 ; Update phi of all multiples of p ; Add the contribution of p to its multiple i by multiplying it with ( 1 - 1 / p ) ; Function to store prefix sum table ; Prefix Sum of all Euler 's Totient Values ; Total number of pairs that can be formed ; Function call to compute all phi ; Function call to store all prefix sum
var N = 100005 ; var phi = Array ( N ) . fill ( 0 ) ; var pref = Array ( N ) . fill ( 0 ) ; function precompute ( ) { phi [ 1 ] = 0 ; for ( i = 2 ; i < N ; i ++ ) phi [ i ] = i ; for ( p = 2 ; p < N ; p ++ ) { if ( phi [ p ] == p ) { phi [ p ] = p - 1 ; for ( i = 2 * p ; i < N ; i += p ) { phi [ i ] = ( phi [ i ] / p ) * ( p - 1 ) ; } } } } function prefix ( ) { for ( i = 1 ; i < N ; i ++ ) pref [ i ] = pref [ i - 1 ] + phi [ i ] ; } function find_pairs ( n ) { var total = ( n * ( n - 1 ) ) / 2 ; var ans = total - pref [ n ] ; document . write ( " " + n + " " + ans + " " ) ; } precompute ( ) ; prefix ( ) ; var q = [ 5 , 7 ] ; var n = q . length ; for ( i = 0 ; i < n ; i ++ ) { find_pairs ( q [ i ] ) ; }
Program to balance the given Chemical Equation | Function to calculate GCD ; Function to calculate b1 , b2 and b3 ; Variable declaration ; temp variable to store gcd ; Computing GCD ; Driver Code
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function balance ( x , y , p , q ) { let b1 , b2 , b3 ; if ( p % x == 0 && q % y == 0 ) { b1 = p / x ; b2 = q / y ; b3 = 1 ; } else { p = p * y ; q = q * x ; b3 = x * y ; let temp = gcd ( p , gcd ( q , b3 ) ) ; b1 = p / temp ; b2 = q / temp ; b3 = b3 / temp ; } document . write ( b1 + " " + b2 + " " + b3 + " " ) ; } let x = 2 , y = 3 , p = 4 , q = 5 ; balance ( x , y , p , q ) ;
Find the Maximum Alternate Subsequence Sum from a given array | Function to find maximum alternating subsequence sum ; Initialize sum to 0 ; Calculate the sum ; Return the final result ; Array initialization ; Length of array
function maxAlternatingSum ( arr , n ) { var max_sum = 0 ; var i = 0 ; while ( i < n ) { var current_max = arr [ i ] ; var k = i ; while ( k < n && ( ( arr [ i ] > 0 && arr [ k ] > 0 ) || ( arr [ i ] < 0 && arr [ k ] < 0 ) ) ) { current_max = Math . max ( current_max , arr [ k ] ) ; k += 1 ; } max_sum += current_max ; i = k ; } return max_sum ; } var arr = [ 1 , 2 , 3 , 4 , - 1 , - 2 ] ; var n = arr . length ; document . write ( maxAlternatingSum ( arr , n ) ) ;
Minimum length subarray containing all unique elements after Q operations | Function to find minimum size subarray of all array elements ; Updating the array after processing each query ; Making it to 0 - indexing ; Prefix sum array concept is used to obtain the count array ; Iterating over the array to get the final array ; Variable to get count of all unique elements ; Hash to maintain perviously occurred elements ; Loop to find the all unique elements ; array to maintain counter of encountered elements ; variable to store answer ; Using two poleters approach ; Increment counter if occurred once ; when all unique elements are found ; update answer with minimum size ; decrement count of elements from left ; decrement counter ; Driver code
function subarrayLength ( A , R , N , M ) { for ( let i = 0 ; i < M ; ++ i ) { let l = R [ i ] [ 0 ] , r = R [ i ] [ 1 ] + 1 ; l -- ; r -- ; A [ l ] ++ ; if ( r < N ) A [ r ] -- ; } for ( let i = 1 ; i < N ; ++ i ) { A [ i ] += A [ i - 1 ] ; } let count = 0 ; let s = new Set ( ) ; for ( let i = 0 ; i < N ; ++ i ) { if ( ! s . has ( A [ i ] ) ) count ++ ; s . add ( A [ i ] ) ; } let repeat = Array . from ( { length : count + 1 } , ( _ , i ) => 0 ) ; let ans = N ; let counter = 0 , left = 0 , right = 0 ; while ( right < N ) { let cur_element = A [ right ] ; repeat [ cur_element ] += 1 ; if ( repeat [ cur_element ] == 1 ) ++ counter ; while ( counter == count ) { ans = Math . min ( ans , right - left + 1 ) ; cur_element = A [ left ] ; repeat [ cur_element ] -= 1 ; ++ left ; if ( repeat [ cur_element ] == 0 ) -- counter ; } ++ right ; } return ans ; } let N = 8 , queries = 6 ; let Q = [ [ 1 , 4 ] , [ 3 , 4 ] , [ 4 , 5 ] , [ 5 , 5 ] , [ 7 , 8 ] , [ 8 , 8 ] ] ; let A = Array . from ( { length : N } , ( _ , i ) => 0 ) ; document . write ( subarrayLength ( A , Q , N , queries ) ) ;
Elements of Array which can be expressed as power of prime numbers | Function to mark all the exponent of prime numbers ; If number is prime then marking all of its exponent true ; Function to display all required elements ; Function to print the required numbers ; To find the largest number ; Function call to mark all the Exponential prime nos . ; Function call ; Driver code
function ModifiedSieveOfEratosthenes ( N , Expo_Prime ) { let primes = new Array ( N ) ; primes . fill ( true ) for ( let i = 2 ; i < N ; i ++ ) { if ( primes [ i ] ) { let no = i ; while ( no <= N ) { Expo_Prime [ no ] = true ; no *= i ; } for ( let j = i * i ; j < N ; j += i ) primes [ j ] = false ; } } } function Display ( arr , Expo_Prime , n ) { for ( let i = 0 ; i < n ; i ++ ) if ( Expo_Prime [ arr [ i ] ] ) document . write ( arr [ i ] + " " ) ; } function FindExpoPrime ( arr , n ) { let max = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( max < arr [ i ] ) max = arr [ i ] ; } let Expo_Prime = new Array ( max + 1 ) ; Expo_Prime . fill ( false ) ModifiedSieveOfEratosthenes ( max + 1 , Expo_Prime ) ; Display ( arr , Expo_Prime , n ) ; } let arr = [ 4 , 6 , 9 , 16 , 1 , 3 , 12 , 36 , 625 , 1000 ] ; let n = arr . length FindExpoPrime ( arr , n ) ;
Elements of Array which can be expressed as power of some integer to given exponent K | Method returns Nth power of A ; Smaller eps , denotes more accuracy ; Initializing difference between two roots by Integer . MAX_VALUE ; x ^ K denotes current value of x ; loop untill we reach desired accuracy ; calculating current value from previous value by newton 's method ; Function to check whether its k root is an integer or not ; Function to find the numbers ; Driver code
function nthRoot ( A , N ) { let xPre = 7 ; let eps = 1e-3 ; let delX = Number . MAX_VALUE ; let xK = 0 ; while ( delX > eps ) { xK = ( ( N - 1.0 ) * xPre + A / Math . pow ( xPre , N - 1 ) ) / N ; delX = Math . abs ( xK - xPre ) ; xPre = xK ; } return xK ; } function check ( no , k ) { let kth_root = nthRoot ( no , k ) ; let num = Math . floor ( kth_root ) ; if ( Math . abs ( num - kth_root ) < 1e-4 ) return true ; return false ; } function printExpo ( arr , n , k ) { for ( let i = 0 ; i < n ; i ++ ) { if ( check ( arr [ i ] , k ) ) document . write ( arr [ i ] + " " ) ; } } let K = 6 ; let arr = [ 46656 , 64 , 256 , 729 , 16 , 1000 ] ; let n = arr . length ; printExpo ( arr , n , K ) ;
Count of subsequences whose product is a difference of square of two integers | Function to count the number of contiguous subsequences whose product can be expressed as square of difference of two integers ; Iterating through the array ; Check if that number can be expressed as the square of difference of two numbers ; Variable to compute the product ; Finding the remaining subsequences ; Check if that number can be expressed as the square of difference of two numbers ; Return the number of subsequences ; Driver code
function CntcontSubs ( a , n ) { let c = 0 , d = 0 , i , sum = 1 , j ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 != 0 a [ i ] % 4 == 0 ) d ++ ; sum = a [ i ] ; for ( j = i + 1 ; j < n ; j ++ ) { sum = sum * a [ j ] ; if ( sum % 2 != 0 sum % 4 == 0 ) c ++ ; } sum = 1 ; } return c + d ; } let arr = [ 5 , 4 , 2 , 9 , 8 ] ; let n = arr . length ; document . write ( CntcontSubs ( arr , n ) ) ;
Count of subsequences whose product is a difference of square of two integers | Function to count all the contiguous subsequences whose product is expressed as the square of the difference of two integers ; Creating vectors to store the remainders and the subsequences ; Iterating through the array ; Finding the remainder when the element is divided by 4 ; Bringing all the elements in the range [ 0 , 3 ] ; If the remainder is 2 , store the index of the ; If the remainder is 2 , store the index of the ; Finding the total number of subsequences ; If there are no numbers which yield the remainder 2 ; Iterating through the vector ; If the element is 2 , find the nearest 2 or 0 and find the number of elements between them ; Returning the count ; Driver code
function CntcontSubs ( a , n ) { let prod = 1 ; let vect = [ ] ; vect . push ( [ 0 , 2 ] ) ; let two = [ ] ; let zero = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { a [ i ] = a [ i ] % 4 ; if ( a [ i ] < 0 ) a [ i ] = a [ i ] + 4 ; if ( a [ i ] == 2 ) two . push ( i + 1 ) ; if ( a [ i ] == 0 ) zero . push ( i + 1 ) ; if ( a [ i ] == 0 a [ i ] == 2 ) vect . push ( [ i + 1 , a [ i ] ] ) ; } vect . push ( [ n + 1 , 2 ] ) ; let total = Math . floor ( ( n * ( n + 1 ) ) / 2 ) ; if ( two . length == 0 ) return total ; else { let sum = 0 ; let pos1 = - 1 , pos2 = - 1 , pos3 = - 1 ; let sz = vect . length ; for ( let i = 1 ; i + 1 < sz ; i ++ ) { if ( vect [ i ] [ 1 ] == 2 ) { sum += ( vect [ i ] [ 0 ] - vect [ i - 1 ] [ 0 ] ) * ( vect [ i + 1 ] [ 0 ] - vect [ i ] [ 0 ] ) - 1 ; } } return total - sum - two . length ; } } let a = [ 5 , 4 , 2 , 9 , 8 ] ; let n = a . length ; document . write ( CntcontSubs ( a , n ) ) ;
Count of possible subarrays and subsequences using given length of Array | Function to count the subarray for the given array ; Function to count the subsequence for the given array length ; Driver Code
function countSubarray ( n ) { return ( ( n ) * ( n + 1 ) ) / 2 ; } function countSubsequence ( n ) { return Math . pow ( 2 , n ) ; } let n = 5 ; document . write ( ( countSubarray ( n ) ) + " " ) ; document . write ( ( countSubsequence ( n ) ) + " " ) ;
Program for finding the Integral of a given function using Boole 's Rule | Function to return the value of f ( x ) for the given value of x ; Function to computes the integrand of y at the given intervals of x with step size h and the initial limit a and final limit b ; Number of intervals ; Computing the step size ; Substituing a = 0 , b = 4 and h = 1 ; Driver Code
function y ( x ) { return ( 1 / ( 1 + x ) ) ; } function BooleRule ( a , b ) { let n = 4 ; let h ; h = ( ( b - a ) / n ) ; let sum = 0 ; let bl = ( 7 * y ( a ) + 32 * y ( a + h ) + 12 * y ( a + 2 * h ) + 32 * y ( a + 3 * h ) + 7 * y ( a + 4 * h ) ) * 2 * h / 45 ; sum = sum + bl ; return sum ; } document . write ( " " + BooleRule ( 0 , 4 ) . toFixed ( 4 ) ) ;
Number of subsets with same AND , OR and XOR values in an Array | Javascript program to find the number of subsets with equal bitwise AND , OR and XOR values ; Function to find the number of subsets with equal bitwise AND , OR and XOR values ; Precompute the modded powers of two for subset counting ; Loop to iterate and find the modded powers of two for subset counting ; Map to store the frequency of each element ; Loop to compute the frequency ; For every element > 0 , the number of subsets formed using this element only is equal to 2 ^ ( frequency [ element ] - 1 ) . And for 0 , we have to find all the subsets , so 2 ^ ( frequency [ element ] ) - 1 ; If element is greater than 0 ; Driver code
var mod = 1000000007 ; function countSubsets ( a , n ) { var answer = 0 ; var powerOfTwo = Array ( 100005 ) . fill ( 0 ) ; powerOfTwo [ 0 ] = 1 ; for ( var i = 1 ; i < 100005 ; i ++ ) powerOfTwo [ i ] = ( powerOfTwo [ i - 1 ] * 2 ) % mod ; var frequency = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) if ( frequency . has ( a [ i ] ) ) { frequency . set ( a [ i ] , frequency . get ( a [ i ] ) + 1 ) ; } else { frequency . set ( a [ i ] , 1 ) ; } frequency . forEach ( ( value , key ) => { if ( key != 0 ) answer = ( answer % mod + powerOfTwo [ value - 1 ] ) % mod ; else answer = ( answer % mod + powerOfTwo [ value ] - 1 + mod ) % mod ; } ) ; return answer ; } var N = 6 ; var A = [ 1 , 3 , 2 , 1 , 2 , 1 ] ; document . write ( countSubsets ( A , N ) ) ;
Sum of all Perfect numbers lying in the range [ L , R ] | Array to store the sum ; Function to check if a number is a perfect number or not ; Iterating till the square root of the number and checking if the sum of divisors is equal to the number or not ; If it is a perfect number , then return the number ; Else , return 0 ; Function to precompute the sum of perfect squares and store then in an array
var pref = Array ( 100010 ) . fill ( 0 ) ; function isPerfect ( n ) { var sum = 1 ; for ( var i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( i * i != n ) sum = sum + i + n / i ; else sum = sum + i ; } } if ( sum == n && n != 1 ) return n ; return 0 ; } function precomputation ( ) { for ( var i = 1 ; i <= 100000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + isPerfect ( i ) ; } } var L = 6 , R = 28 ; precomputation ( ) ; document . write ( pref [ R ] - pref [ L - 1 ] ) ;
Ternary number system or Base 3 numbers | Function to convert a ternary number to a decimal number ; If the number is greater than 0 , compute the decimal representation of the number ; Loop to iterate through the number ; Computing the decimal digit ; Driver code
function convertToDecimal ( N ) { document . write ( " " + N + " " ) ; if ( N != 0 ) { let decimalNumber = 0 ; let i = 0 , remainder ; while ( N != 0 ) { remainder = N % 10 ; N = parseInt ( N / 10 , 10 ) ; decimalNumber += remainder * Math . pow ( 3 , i ) ; ++ i ; } document . write ( decimalNumber + " " ) ; } else document . write ( " " + " " ) ; } let Ternary = 10202202 ; convertToDecimal ( Ternary ) ;
Check if a given pair of Numbers are Betrothed numbers or not | Function to check whether N is Perfect Square or not ; Find sqrt ; Function to check whether the given pairs of numbers is Betrothed Numbers or not ; For finding the sum of all the divisors of first number n ; For finding the sum of all the divisors of second number m ; Driver Code ; Function Call
function isPerfectSquare ( N ) { let sr = Math . sqrt ( N ) ; return ( sr - Math . floor ( sr ) ) == 0 ; } function BetrothedNumbers ( n , m ) { let Sum1 = 1 ; let Sum2 = 1 ; for ( let i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { Sum1 += i + ( isPerfectSquare ( n ) ? 0 : parseInt ( n / i ) ) ; } } for ( let i = 2 ; i <= Math . sqrt ( m ) ; i ++ ) { if ( m % i == 0 ) { Sum2 += i + ( isPerfectSquare ( m ) ? 0 : parseInt ( m / i ) ) ; } } if ( ( n + 1 == Sum2 ) && ( m + 1 == Sum1 ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let N = 9504 ; let M = 20734 ; BetrothedNumbers ( N , M ) ;
Count of numbers upto M with GCD equals to K when paired with M | Function to calculate GCD using euler totient function ; Finding the prime factors of limit to calculate it 's  euler totient function ; Calculating the euler totient function of ( m / k ) ; Function print the count of numbers whose GCD with M equals to K ; GCD of m with any integer cannot be equal to k ; 0 and m itself will be the only valid integers ; Finding the number upto which coefficient of k can come ; Driver Code
function EulerTotientFunction ( limit ) { let copy = limit ; let primes = [ ] ; for ( let i = 2 ; i * i <= limit ; i ++ ) { if ( limit % i == 0 ) { while ( limit % i == 0 ) { limit /= i ; } primes . push ( i ) ; } } if ( limit >= 2 ) { primes . push ( limit ) ; } let ans = copy ; for ( let it in primes ) { ans = ( ans / primes [ it ] ) * ( primes [ it ] - 1 ) ; } return ans ; } function CountGCD ( m , k ) { if ( m % k != 0 ) { document . write ( 0 + " " ) ; return ; } if ( m == k ) { document . write ( 2 + " " ) ; return ; } let limit = Math . floor ( m / k ) ; let ans = EulerTotientFunction ( limit ) ; document . write ( ans + " " ) ; } let M = 9 ; let K = 1 ; CountGCD ( M , K ) ;
Find the product of sum of two diagonals of a square Matrix | Function to find the product of the sum of diagonals . ; Initialize sums of diagonals ; Return the answer ; Driven code ; Function call
function product ( mat , n ) { let d1 = 0 , d2 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { d1 += mat [ i ] [ i ] ; d2 += mat [ i ] [ n - i - 1 ] ; } return d1 * d2 ; } let mat = [ [ 5 , 8 , 1 ] , [ 5 , 10 , 3 ] , [ - 6 , 17 , - 9 ] ] ; let n = mat . length ; document . write ( product ( mat , n ) ) ;
Program to calculate Percentile of a student based on rank | Program to calculate the percentile ; flat variable to store the result ; calculate and return the percentile ; Driver Code
function getPercentile ( rank , students ) { var result = ( students - rank ) / students * 100 ; return result ; } var your_rank = 805 ; var total_students = 97481 ; document . write ( getPercentile ( your_rank , total_students ) . toFixed ( 4 ) ) ;
Sum of numbers in the Kth level of a Fibonacci triangle | Function to return the nth Fibonacci number ; Function to return the required sum of the array ; Using our deduced result ; Function to return the sum of fibonacci in the Kth array ; Count of fibonacci which are in the arrays from 1 to k - 1 ; Driver code
function fib ( n ) { var phi = ( 1 + Math . sqrt ( 5 ) ) / 2 ; return parseInt ( Math . round ( Math . pow ( phi , n ) / Math . sqrt ( 5 ) ) ) ; } function calculateSum ( l , r ) { var sum = fib ( r + 2 ) - fib ( l + 1 ) ; return sum ; } function sumFibonacci ( k ) { var l = ( k * ( k - 1 ) ) / 2 ; var r = l + k ; var sum = calculateSum ( l , r - 1 ) ; return sum ; } var k = 3 ; document . write ( sumFibonacci ( k ) ) ;
Print the sequence of size N in which every term is sum of previous K terms | Function to generate the series in the form of array ; Pick a starting point ; Find the sum of all elements till count < K ; Find the value of sum at i position ; Driver Code
function sumOfPrevK ( N , K ) { let arr = new Array ( N ) ; arr [ 0 ] = 1 ; for ( let i = 1 ; i < N ; i ++ ) { let j = i - 1 , count = 0 , sum = 0 ; while ( j >= 0 && count < K ) { sum += arr [ j ] ; j -- ; count ++ ; } arr [ i ] = sum ; } for ( let i = 0 ; i < N ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } let N = 10 , K = 4 ; sumOfPrevK ( N , K ) ;
Maximum GCD of all subarrays of length at least 2 | Function to find GCD ; To store the maximum GCD ; Traverse the array ; Find GCD of the consecutive element ; If calculated GCD > maxGCD then update it ; Print the maximum GCD ; Driver Code ; Function Call
function gcd ( a , b ) { if ( b == 0 ) { return a ; } return gcd ( b , a % b ) ; } function findMaxGCD ( arr , n ) { let maxGCD = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { let val = gcd ( arr [ i ] , arr [ i + 1 ] ) ; if ( val > maxGCD ) { maxGCD = val ; } } document . write ( maxGCD + " " ) ; } let arr = [ 3 , 18 , 9 , 9 , 5 , 15 , 8 , 7 , 6 , 9 ] ; let n = arr . length ; findMaxGCD ( arr , n ) ;
Sum of elements of an AP in the given range | Function to find sum in the given range ; Find the value of k ; Find the common difference ; Find the sum ; Driver Code
function findSum ( arr , n , left , right ) { let k = right - left ; let d = arr [ 1 ] - arr [ 0 ] ; let ans = arr [ left - 1 ] * ( k + 1 ) ; ans = ans + ( d * ( k * ( k + 1 ) ) ) / 2 ; return ans ; } let arr = [ 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 ] ; let queries = 3 ; let q = [ [ 2 , 4 ] , [ 2 , 6 ] , [ 5 , 6 ] ] ; let n = arr . length ; for ( let i = 0 ; i < queries ; i ++ ) document . write ( findSum ( arr , n , q [ i ] [ 0 ] , q [ i ] [ 1 ] ) + " " ) ;