codeparrot/xlcost-text-to-code huggingface dataset:
Area of a square inscribed in a circle which is inscribed in a hexagon | Function to find the area of the square ; side of hexagon cannot be negative ; area of the square ; Driver code
function area ( a ) { if ( a < 0 ) return - 1 ; var area = Math . pow ( ( a * Math . sqrt ( 3 ) ) / ( Math . sqrt ( 2 ) ) , 2 ) ; return area ; } var a = 5 ; document . write ( area ( a ) . toFixed ( 5 ) ) ;
Program to find Nth term of series 1 , 6 , 17 , 34 , 56 , 86 , 121 , 162 , ... ... . | calculate Nth term of series ; Driver code
function nthTerm ( n ) { return 3 * Math . pow ( n , 2 ) - 4 * n + 2 ; } let N = 4 ; document . write ( nthTerm ( N ) ) ;
Sum of the first N terms of the series 2 , 10 , 30 , 68 , ... . | Function to calculate the sum ; number of terms to be included in the sum ; find the Sum
function calculateSum ( n ) { return n * ( n + 1 ) / 2 + Math . pow ( ( n * ( n + 1 ) / 2 ) , 2 ) ; } let n = 3 ; document . write ( " " + calculateSum ( n ) ) ;
Check if two arrays are permutations of each other using Mathematical Operation | Function to check if arrays are permutations of each other . ; Calculating sum and multiply of first array ; Calculating sum and multiply of second array ; If sum and mul of both arrays are equal , return true , else return false . ; Driver code
function arePermutations ( a , b , n , m ) { let sum1 = 0 , sum2 = 0 , mul1 = 1 , mul2 = 1 ; for ( let i = 0 ; i < n ; i ++ ) { sum1 += a [ i ] ; mul1 *= a [ i ] ; } for ( let i = 0 ; i < m ; i ++ ) { sum2 += b [ i ] ; mul2 *= b [ i ] ; } return ( ( sum1 == sum2 ) && ( mul1 == mul2 ) ) ; } let a = [ 1 , 3 , 2 ] ; let b = [ 3 , 1 , 2 ] ; let n = a . length ; let m = b . length ; if ( arePermutations ( a , b , n , m ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
Program to find the head start in a race | Function to find the B start to C ; When B completed it 's 100 meter  then Completed meters by C is ; Driver Code ; When A completed it 's 100 meter Then completed meters of B and C is
function Race ( B , C ) { var result = 0 ; result = ( ( C * 100 ) / B ) ; return 100 - result ; } var B = 10 , C = 28 ; B = 100 - B ; C = 100 - C ; document . write ( Race ( B , C ) + " " ) ;
Minimum time required to fill a cistern using N pipes | Function to calculate the time ; Driver Code
function Time ( arr , n , Emptypipe ) { var fill = 0 ; for ( var i = 0 ; i < n ; i ++ ) fill += 1 / arr [ i ] ; fill = fill - ( 1 / Emptypipe ) ; return 1 / fill ; } var arr = [ 12 , 14 ] ; var Emptypipe = 30 ; var n = arr . length ; document . write ( Math . floor ( Time ( arr , n , Emptypipe ) ) + " " ) ;
Check if Decimal representation of an Octal number is divisible by 7 | Function to check Divisibility ; Sum of all individual digits ; Condition ; Octal number
function check ( n ) { let sum = 0 ; while ( n != 0 ) { sum += n % 10 ; n = Math . floor ( n / 10 ) ; } if ( sum % 7 == 0 ) return 1 ; else return 0 ; } let n = 25 ; ( check ( n ) == 1 ) ? document . write ( " " ) : document . write ( " " ) ;
Sum of all the prime divisors of a number | Javascript program to find sum of prime divisors of N ; Function to check if the number is prime or not . ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; function to find sum of prime divisors of N ; Driver code
let N = 1000005 ; 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 SumOfPrimeDivisors ( n ) { let sum = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { if ( n % i == 0 ) { if ( isPrime ( i ) ) sum += i ; } } return sum ; } let n = 60 ; document . write ( " " + SumOfPrimeDivisors ( n ) ) ;
Sum of all the prime divisors of a number | function to find prime divisors of all numbers from 1 to n ; if the number is prime ; add this prime to all it 's multiples ; Driver code
function Sum ( N ) { let SumOfPrimeDivisors = new Array ( N + 1 ) ; for ( let i = 0 ; i < SumOfPrimeDivisors . length ; i ++ ) { SumOfPrimeDivisors [ i ] = 0 ; } for ( let i = 2 ; i <= N ; ++ i ) { if ( SumOfPrimeDivisors [ i ] == 0 ) { for ( let j = i ; j <= N ; j += i ) { SumOfPrimeDivisors [ j ] += i ; } } } return SumOfPrimeDivisors [ N ] ; } let N = 60 ; document . write ( " " + " " + Sum ( N ) + " " ) ;
Squares of numbers with repeated single digits | Set 1 ( 3 , 6 and 9 ) | Function to find the square of 333. . .333 , 666. . .666 and 999. . .999 ; if the number is 333. . .333 ; if the number is 666. . .666 ; if the number is 999. . .999 ; variable for hold result ; find the no of digit ; add size - 1 time a in result ; add one time b in result ; add size - 1 time c in result ; add one time d in result ; return result ; Drivers code ; find square of 33. .33 ; find square of 66. .66 ; find square of 66. .66
function find_Square_369 ( num ) { let a , b , c , d ; if ( num [ 0 ] == ' ' ) { a = ' ' ; b = ' ' ; c = ' ' ; d = ' ' ; } else if ( num [ 0 ] == ' ' ) { a = ' ' ; b = ' ' ; c = ' ' ; d = ' ' ; } else { a = ' ' ; b = ' ' ; c = ' ' ; d = ' ' ; } let result = " " ; let size = num . length ; for ( let i = 1 ; i < size ; i ++ ) result += a ; result += b ; for ( let i = 1 ; i < size ; i ++ ) result += c ; result += d ; return result ; } let num_3 , num_6 , num_9 ; num_3 = " " ; num_6 = " " ; num_9 = " " ; let result = " " ; result = find_Square_369 ( num_3 ) ; document . write ( " " + num_3 + " " + result + " " ) ; result = find_Square_369 ( num_6 ) ; document . write ( " " + num_9 + " " + result + " " ) ; result = find_Square_369 ( num_9 ) ; document . write ( " " + num_9 + " " + result + " " ) ;
Trick for modular division ( ( x1 * x2 ... . xn ) / b ) mod ( m ) |
var ans = 1 ; var mod = 1000000007 * 120 ; for ( var i = 0 ; i < 5 ; i ++ ) ans = ( ans * ( 55555 - i ) ) % mod ; ans = ans / 120 ; document . write ( " " + " " + ans ) ;
Ways to multiply n elements with an associative operation | Function to find the required factorial ; Function to find nCr ; Function to find the number of ways ; Driver code
function fact ( n ) { if ( n == 0 n == 1 ) return 1 ; let ans = 1 ; for ( let i = 1 ; i <= n ; i ++ ) ans = ans * i ; return ans ; } function nCr ( n , r ) { let Nr = n , Dr = 1 , ans = 1 ; for ( let i = 1 ; i <= r ; i ++ ) { ans = parseInt ( ( ans * Nr ) / ( Dr ) , 10 ) ; Nr -- ; Dr ++ ; } return ans ; } function solve ( n ) { let N = 2 * n - 2 ; let R = n - 1 ; return nCr ( N , R ) * fact ( n - 1 ) ; } let n = 6 ; document . write ( solve ( n ) ) ;
Pythagorean Triplet with given sum | JavaScript program to find Pythagorean Triplet of given sum . ; Considering triplets in sorted order . The value of first element in sorted triplet can be at - most n / 3. ; The value of second element must be less than equal to n / 2 ; Driver Code
function pythagoreanTriplet ( n ) { for ( let i = 1 ; i <= n / 3 ; i ++ ) { for ( let j = i + 1 ; j <= n / 2 ; j ++ ) { let k = n - i - j ; if ( i * i + j * j == k * k ) { document . write ( i + " " + j + " " + k ) ; return ; } } } document . write ( " " ) ; } let n = 12 ; pythagoreanTriplet ( n ) ;
Program to print binomial expansion series | function to calculate factorial of a number ; function to prlet the series ; calculating the value of n ! ; loop to display the series ; For calculating the value of nCr ; calculating the value of A to the power k and X to the power k ; display the series ; Driver Code
function factorial ( n ) { let f = 1 ; for ( let i = 2 ; i <= n ; i ++ ) f *= i ; return f ; } function series ( A , X , n ) { let nFact = factorial ( n ) ; for ( let i = 0 ; i < n + 1 ; i ++ ) { let niFact = factorial ( n - i ) ; let iFact = factorial ( i ) ; let aPow = Math . pow ( A , n - i ) ; let xPow = Math . pow ( X , i ) ; document . write ( ( nFact * aPow * xPow ) / ( niFact * iFact ) + " " ) ; } } let A = 3 , X = 4 , n = 5 ; series ( A , X , n ) ;
Sum of series with alternate signed squares of AP | function to calculate series sum ; Driver Code
function seiresSum ( n , a ) { let res = 0 , i ; for ( i = 0 ; i < 2 * n ; i ++ ) { if ( i % 2 == 0 ) res += a [ i ] * a [ i ] ; else res -= a [ i ] * a [ i ] ; } return res ; } let n = 2 ; let a = [ 1 , 2 , 3 , 4 ] ; document . write ( seiresSum ( n , a ) ) ;
Power of a prime number ‘ r ’ in n ! | Function to return power of a no . ' r ' in factorial of n ; Keep dividing n by powers of ' r ' and update count ; Driver code
function power ( n , r ) { let count = 0 ; for ( let i = r ; ( n / i ) >= 1 ; i = i * r ) count += n / i ; return count ; } let n = 6 , r = 3 ; document . write ( power ( n , r ) ) ;
Average of first n odd naturals numbers | Returns the Avg of first n odd numbers ; sum of first n odd number ; Average of first n odd numbers ; Driver Code
function avg_of_odd_num ( n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += ( 2 * i + 1 ) ; return sum / n ; } let n = 20 ; document . write ( avg_of_odd_num ( n ) ) ;
Average of first n odd naturals numbers | Return the average of sum of first n odd numbers ; Driver Code
function avg_of_odd_num ( n ) { return n ; } var n = 8 ; document . write ( avg_of_odd_num ( n ) ) ;
Program to print Fibonacci Triangle | Function to fill Fibonacci Numbers in f [ ] ; 1 st and 2 nd number of the series are 1 and 1 ; Add the previous 2 numbers in the series and store it ; Fill Fibonacci numbers in f [ ] using fib ( ) . We need N = n * ( n + 1 ) / 2 Fibonacci numbers to make a triangle of height n ; To store next Fibonacci Number to print ; for loop to keep track of number of lines ; For loop to keep track of numbers in each line ; Driver code
function fib ( f , N ) { f [ 1 ] = 1 ; f [ 2 ] = 1 ; for ( var i = 3 ; i <= N ; i ++ ) f [ i ] = f [ i - 1 ] + f [ i - 2 ] ; } function fiboTriangle ( n ) { var N = ( n * ( n + 1 ) ) / 2 ; var f = [ ... Array ( N + 1 ) ] ; fib ( f , N ) ; var fiboNum = 1 ; for ( var i = 1 ; i <= n ; i ++ ) { for ( var j = 1 ; j <= i ; j ++ ) document . write ( f [ fiboNum ++ ] + " " ) ; document . write ( " " ) ; } } var n = 5 ; fiboTriangle ( n ) ;
Average of odd numbers till a given odd number | Function to calculate the average of odd numbers ; count odd numbers ; store the sum of odd numbers ; driver function
function averageOdd ( n ) { if ( n % 2 == 0 ) { document . write ( " " ) ; return - 1 ; } let sum = 0 , count = 0 ; while ( n >= 1 ) { count ++ ; sum += n ; n = n - 2 ; } return sum / count ; } let n = 15 ; document . write ( averageOdd ( n ) ) ;
Average of odd numbers till a given odd number | Function to calculate the average of odd numbers ; driver function
function averageOdd ( n ) { if ( n % 2 == 0 ) { document . write ( " " ) ; return - 1 ; } return ( n + 1 ) / 2 ; } let n = 15 ; document . write ( averageOdd ( n ) ) ;
Find max of two Rational numbers | JAVASCRIPT program to find max between two Rational numbers ; Get lcm of two number 's ; Get max rational number ; Find the LCM of first . denominator and sec . denominator ; Declare nume1 and nume2 for get the value of first numerator and second numerator ; Driver Code
class Rational { constructor ( nume , deno ) { this . nume = nume ; this . deno = deno ; } } function lcm ( a , b ) { return ( a * b ) / ( __gcd ( a , b ) ) ; } function maxRational ( first , sec ) { let k = lcm ( first . deno , sec . deno ) ; let nume1 = first . nume ; let nume2 = sec . nume ; nume1 *= k / ( first . deno ) ; nume2 *= k / ( sec . deno ) ; return ( nume2 < nume1 ) ? first : sec ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } let first = new Rational ( 3 , 2 ) ; let sec = new Rational ( 3 , 4 ) ; let res = maxRational ( first , sec ) ; document . write ( res . nume + " " + res . deno ) ;
Trinomial Triangle | Function to find the trinomial triangle value . ; base case ; base case ; recursive step . ; Function to print Trinomial Triangle of height n . ; printing n rows . ; printing first half of triangle ; printing second half of triangle . ; Driver code
function TrinomialValue ( n , k ) { if ( n == 0 && k == 0 ) return 1 ; if ( k < - n k > n ) return 0 ; return TrinomialValue ( n - 1 , k - 1 ) + TrinomialValue ( n - 1 , k ) + TrinomialValue ( n - 1 , k + 1 ) ; } function printTrinomial ( n ) { for ( let i = 0 ; i < n ; i ++ ) { for ( let j = - i ; j <= 0 ; j ++ ) document . write ( TrinomialValue ( i , j ) + " " ) ; for ( let j = 1 ; j <= i ; j ++ ) document . write ( TrinomialValue ( i , j ) + " " ) ; document . write ( " " ) ; } } let n = 4 ; printTrinomial ( n ) ;
Trinomial Triangle | Javascript Program to print trinomial triangle . ; Function to find the trinomial triangle value . ; Using property of trinomial triangle . ; If value already calculated , return that . ; base case ; base case ; recursive step and storing the value . ; Function to print Trinomial Triangle of height n . ; printing n rows . ; printing first half of triangle ; printing second half of triangle . ; Driven Program
var MAX = 10 function TrinomialValue ( dp , n , k ) { if ( k < 0 ) k = - k ; if ( dp [ n ] [ k ] != 0 ) return dp [ n ] [ k ] ; if ( n == 0 && k == 0 ) return 1 ; if ( k < - n k > n ) return 0 ; return ( dp [ n ] [ k ] = TrinomialValue ( dp , n - 1 , k - 1 ) + TrinomialValue ( dp , n - 1 , k ) + TrinomialValue ( dp , n - 1 , k + 1 ) ) ; } function printTrinomial ( n ) { var dp = Array . from ( Array ( MAX ) , ( ) => Array ( MAX ) . fill ( 0 ) ) ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = - i ; j <= 0 ; j ++ ) document . write ( TrinomialValue ( dp , i , j ) + " " ) ; for ( var j = 1 ; j <= i ; j ++ ) document . write ( TrinomialValue ( dp , i , j ) + " " ) ; document . write ( " " ) ; } } var n = 4 ; printTrinomial ( n ) ;
Sum of largest prime factor of each number less than equal to n | function to find sum of largest prime factor of each number less than equal to n ; Create an integer array " prime [ 0 . . n ] " and initialize all entries of it as 0. A value in prime [ i ] will finally be 0 if ' i ' is a prime , else it will contain the largest prime factor of ' i ' . ; If prime [ p ] is '0' , then it is a prime number ; Update all multiples of p ; Sum up the largest prime factor of all the numbers ; If ' p ' is a non - prime number then prime [ p ] gives its largesr prime factor ; ' p ' is a prime number ; Required sum ; Driver code
function sumOfLargePrimeFactor ( n ) { let prime = new Array ( n + 1 ) ; let sum = 0 ; let max = n / 2 ; for ( let i = 0 ; i < n + 1 ; i ++ ) prime [ i ] = 0 ; for ( let p = 2 ; p <= max ; p ++ ) { if ( prime [ p ] == 0 ) { for ( let i = p * 2 ; i <= n ; i += p ) prime [ i ] = p ; } } for ( let p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] ) sum += prime [ p ] ; else sum += p ; } return sum ; } let n = 12 ; document . write ( " " + sumOfLargePrimeFactor ( n ) ) ;
Sum of multiples of a number up to N | Function for calculating sum of multiples of a upto N ; Number of multiples ; Sum of first m natural numbers ; Sum of multiples ; Driver code
function calculate_sum ( a , N ) { m = N / a ; sum = m * ( m + 1 ) / 2 ; ans = a * sum ; return ans ; } let a = 7 ; let N = 49 ; document . write ( " " + a + " " + N + " " + calculate_sum ( a , N ) ) ;
Given a HUGE number check if it 's a power of two. | Function to check whether a number is power of 2 or not ; Driver code
function ispowerof2 ( num ) { if ( ( num & ( num - 1 ) ) == 0 ) return 1 ; return 0 ; } var num = 549755813888 ; document . write ( ispowerof2 ( num ) ) ;
Count divisors of array multiplication | To count number of factors in a number ; Initialize count with 0 ; Increment count for every factor of the given number X . ; Return number of factors ; Returns number of divisors in array multiplication ; Multipliying all elements of the given array . ; Calling function which count number of factors of the number ; driver function
function counDivisors ( X ) { let count = 0 ; for ( let i = 1 ; i <= X ; ++ i ) { if ( X % i == 0 ) { count ++ ; } } return count ; } function countDivisorsMult ( arr , n ) { let mul = 1 ; for ( let i = 0 ; i < n ; ++ i ) mul *= arr [ i ] ; return counDivisors ( mul ) ; } let arr = [ 2 , 4 , 6 ] ; let n = arr . length ; document . write ( countDivisorsMult ( arr , n ) ) ;
Count divisors of array multiplication | javascript program to count divisors in array multiplication . ; Create a boolean array " isPrime [ 0 . . n ] " and initialize all entries it as true . A value in isPrime [ i ] will finally be false if i is Not a isPrime , else true . ; If isPrime [ p ] is not changed , then it is a isPrime ; Update all multiples of p ; Print all isPrime numbers ; Returns number of divisors in array multiplication ; Find all prime numbers smaller than the largest element . ; Find counts of occurrences of each prime factor ; Compute count of all divisors using counts prime factors . ; Driver code
function SieveOfEratosthenes ( largest , prime ) { var isPrime = Array ( largest + 1 ) . fill ( true ) ; var p , i ; for ( p = 2 ; p * p <= largest ; p ++ ) { if ( isPrime [ p ] == true ) { for ( i = p * 2 ; i <= largest ; i += p ) isPrime [ i ] = false ; } } for ( p = 2 ; p <= largest ; p ++ ) if ( isPrime [ p ] ) prime . push ( p ) ; } function countDivisorsMult ( arr , n ) { var largest = Math . max . apply ( null , arr ) ; var prime = [ ] ; SieveOfEratosthenes ( largest , prime ) ; var j ; var mp = new Map ( ) ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < prime . length ; j ++ ) { while ( arr [ i ] > 1 && arr [ i ] % prime [ j ] == 0 ) { arr [ i ] /= prime [ j ] ; if ( mp . has ( prime [ j ] ) ) mp . set ( prime [ j ] , mp . get ( prime [ j ] ) + 1 ) ; else mp . set ( prime [ j ] , 1 ) ; } } if ( arr [ i ] != 1 ) { if ( mp . has ( arr [ i ] ) ) mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; else mp . set ( arr [ i ] , 1 ) ; } } var res = 1 ; for ( const [ key , value ] of mp . entries ( ) ) { res *= ( value + 1 ) ; } return res ; } var arr = [ 2 , 4 , 6 ] ; var n = arr . length ; document . write ( countDivisorsMult ( arr , n ) ) ;
K distant prime pairs in a given range | Function to generate prime numbers in the given range [ L , R ] ; Store all value in the range ; Erase 1 as its non - prime ; Perform Sieve of Eratosthenes ; Find current multiple ; Erase as it is a non - prime ; Increment multiple ; Function to print all the prime pairs in the given range that differs by K ; Generate all prime number ; Traverse the Map M ; If it . first & ( it . first + K ) is prime then print this pair ; Given range ; Given K ; Function Call
function findPrimeNos ( L , R , M ) { for ( var i = L ; i <= R ; i ++ ) { if ( M . has ( i ) ) M . set ( i , M . get ( i ) + 1 ) else M . set ( i , 1 ) } if ( M . has ( 1 ) ) { M . delete ( 1 ) ; } for ( var i = 2 ; i <= parseInt ( Math . sqrt ( R ) ) ; i ++ ) { var multiple = 2 ; while ( ( i * multiple ) <= R ) { if ( M . has ( i * multiple ) ) { M . delete ( i * multiple ) ; } multiple ++ ; } } return M ; } function getPrimePairs ( L , R , K ) { var M = new Map ( ) ; M = findPrimeNos ( L , R , M ) ; M . forEach ( ( value , key ) => { if ( M . has ( key + K ) ) { document . write ( " " + key + " " + ( key + K ) + " " ) ; } } ) ; } var L = 1 , R = 19 ; var K = 6 ; getPrimePairs ( L , R , K ) ;
Enneacontahexagon numbers | Function to find the nth Enneacontahexagon Number ; Driver code
function EnneacontahexagonNum ( n ) { return ( 94 * n * n - 92 * n ) / 2 ; } let n = 3 ; document . write ( EnneacontahexagonNum ( n ) ) ;
Find two Composite Numbers such that there difference is N | Function to find the two composite numbers ; Driver code
function find_composite_nos ( n ) { document . write ( 9 * n + " " + 8 * n ) ; } var n = 4 ; find_composite_nos ( n ) ;
Count the number of pairs ( i , j ) such that either arr [ i ] is divisible by arr [ j ] or arr [ j ] is divisible by arr [ i ] | Function to find number of unordered pairs ; Maximum element from the array ; Array to store the frequency of each element ; Stores the number of unordered pairs ; Store the frequency of each element ; Find the number of unordered pairs ; If the number j divisible by ith element is present in the array ; If the ith element of the array has frequency more than one ; Driver code
function freqPairs ( arr , n ) { let max = Math . max ( ... arr ) ; let freq = new Array ( max + 1 ) . fill ( 0 ) ; let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) freq [ arr [ i ] ] ++ ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 2 * arr [ i ] ; j <= max ; j += arr [ i ] ) { if ( freq [ j ] >= 1 ) count += freq [ j ] ; } if ( freq [ arr [ i ] ] > 1 ) { count += freq [ arr [ i ] ] - 1 ; freq [ arr [ i ] ] -- ; } } return count ; } let arr = [ 3 , 2 , 4 , 2 , 6 ] ; let n = arr . length ; document . write ( freqPairs ( arr , n ) ) ;
Find the Nth term of the series 1 + 2 + 6 + 15 + 31 + 56 + ... | calculate Nth term of given series ; Driver code
function Nth_Term ( n ) { return ( 2 * Math . pow ( n , 3 ) - 3 * Math . pow ( n , 2 ) + n + 6 ) / 6 ; } let N = 8 ; document . write ( Nth_Term ( N ) ) ;
Program to find N | Function to find n - th number in series made of 3 and 5 ; create an array of size ( n + 1 ) ; If i is odd ; main function
function prletNthElement ( n ) { let arr = Array ( n + 1 ) . fill ( 0 ) ; arr [ 1 ] = 3 ; arr [ 2 ] = 5 ; for ( i = 3 ; i <= n ; i ++ ) { if ( i % 2 != 0 ) arr [ i ] = arr [ i / 2 ] * 10 + 3 ; else arr [ i ] = arr [ ( i / 2 ) - 1 ] * 10 + 5 ; } return arr [ n ] ; } let n = 6 ; document . write ( prletNthElement ( n ) ) ;
Program to find Nth term of the series 3 , 6 , 18 , 24 , ... | function to calculate Nth term of series ; By using above formula ; get the value of N ; Calculate and print the Nth term
function nthTerm ( N ) { return parseInt ( N * ( parseInt ( N / 2 ) + ( ( N % 2 ) * 2 ) + N ) ) ; } let N = 5 ; document . write ( " " + N + " " + nthTerm ( N ) ) ;
Program to print binomial expansion series | function to print the series ; Calculating and printing first term ; Computing and printing remaining terms ; Find current term using previous terms We increment power of X by 1 , decrement power of A by 1 and compute nCi using previous term by multiplying previous term with ( n - i + 1 ) / i ; main function started
function series ( A , X , n ) { let term = Math . pow ( A , n ) ; document . write ( term + " " ) ; for ( let i = 1 ; i <= n ; i ++ ) { term = term * X * ( n - i + 1 ) / ( i * A ) ; document . write ( term + " " ) ; } } let A = 3 , X = 4 , n = 5 ; series ( A , X , n ) ;
Check if a number is divisible by 8 using bitwise operators | function to check number is div by 8 or not using bitwise operator ; Driver code
function Div_by_8 ( n ) { return ( ( ( n >> 3 ) << 3 ) == n ) ; } var n = 16 ; if ( Div_by_8 ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Average of even numbers till a given even number | Function to calculate the average of even numbers ; count even numbers ; store the sum of even numbers ; driver function
function averageEven ( n ) { if ( n % 2 != 0 ) { document . write ( " " ) ; return - 1 ; } let sum = 0 , count = 0 ; while ( n >= 2 ) { count ++ ; sum += n ; n = n - 2 ; } return sum / count ; } let n = 16 ; document . write ( averageEven ( n ) ) ;
Average of even numbers till a given even number | Function to calculate the average of even numbers ; driver function
function averageEven ( n ) { if ( n % 2 != 0 ) { document . write ( " " ) ; return - 1 ; } return ( n + 2 ) / 2 ; } let n = 16 ; document . write ( averageEven ( n ) ) ;
Largest number that divides x and is co | Recursive function to return gcd of a and b ; Everything divides 0 ; base case ; a is greater ; function to find largest coprime divisor ; Driver Code
function gcd ( a , b ) { if ( a == 0 b == 0 ) return 0 ; if ( a == b ) return a ; if ( a > b ) return gcd ( a - b , b ) ; return gcd ( a , b - a ) ; } function cpFact ( x , y ) { while ( gcd ( x , y ) != 1 ) { x = x / gcd ( x , y ) ; } return x ; } let x = 15 ; let y = 3 ; document . write ( cpFact ( x , y ) + " " ) ; x = 14 ; y = 28 ; document . write ( cpFact ( x , y ) , " " ) ; x = 7 ; y = 3 ; document . write ( cpFact ( x , y ) ) ;
Count numbers with unit digit k in given range | Returns count of numbers with k as last digit . ; Driver Code
function counLastDigitK ( low , high , k ) { let count = 0 ; for ( let i = low ; i <= high ; i ++ ) if ( i % 10 == k ) count ++ ; return count ; } let low = 3 ; let high = 35 ; let k = 3 ; document . write ( counLastDigitK ( low , high , k ) ) ;