codeparrot/xlcost-text-to-code huggingface dataset:
Steps to reduce N to zero by subtracting its most significant digit at every step | Function to count the number of digits in a number m ; Function to count the number of steps to reach 0 ; count the total number of stesp ; iterate till we reach 0 ; count the digits in last ; decrease it by 1 ; find the number on whose division , we get the first digit ; first digit in last ; find the first number less than last where the first digit changes ; find the number of numbers with same first digit that are jumped ; count the steps ; the next number with a different first digit ; Driver code
function countdig ( m ) { if ( m == 0 ) return 0 ; else return 1 + countdig ( parseInt ( m / 10 ) ) ; } function countSteps ( x ) { var c = 0 ; var last = x ; while ( last > 0 ) { var digits = countdig ( last ) ; digits -= 1 ; var divisor = parseInt ( Math . pow ( 10 , digits ) ) ; var first = parseInt ( last / divisor ) ; var lastnumber = first * divisor ; var skipped = parseInt ( ( last - lastnumber ) / first ) ; skipped += 1 ; c += skipped ; last = last - ( first * skipped ) ; } return c ; } var n = 14 ; document . write ( countSteps ( n ) ) ;
GCD of a number raised to some power and another number | Calculates modular exponentiation , i . e . , ( x ^ y ) % p in O ( log y ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Returns GCD of a ^ n and b ; Driver code
function power ( x , y , p ) { while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function powerGCD ( a , b , n ) { var e = power ( a , n , b ) ; return gcd ( e , b ) ; } var a = 5 , b = 4 , n = 2 ; document . write ( powerGCD ( a , b , n ) ) ;
Largest number not greater than N all the digits of which are odd | Function to check if all digits of a number are odd ; iterate for all digits ; if digit is even ; all digits are odd ; function to return the largest number with all digits odd ; iterate till we find a number with all digits odd ; Driver code
function allOddDigits ( n ) { while ( n != 0 ) { if ( ( n % 10 ) % 2 == 0 ) return false ; n = parseInt ( n / 10 ) ; } return true ; } function largestNumber ( n ) { if ( parseInt ( n % 2 ) == 0 ) n -- ; for ( i = n ; i > 0 ; i -= 2 ) if ( allOddDigits ( i ) ) return i ; } var N = 23 ; document . write ( largestNumber ( N ) ) ;
Largest number not greater than N all the digits of which are odd | function to return the largest number with all digits odd ; convert the number to a string for easy operations ; find first even digit ; if no even digit , then N is the answer ; till first even digit , add all odd numbers ; decrease 1 from the even digit ; add 9 in the rest of the digits ; Driver Code
function largestNumber ( n ) { var s = " " ; var duplicate = n ; while ( n > 0 ) { s = String . fromCharCode ( n % 10 + 48 ) + s ; n = parseInt ( n / 10 ) ; } var index = - 1 ; for ( i = 0 ; i < s . length ; i ++ ) { if ( ( ( s . charAt ( i ) . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) % 2 & 1 ) == 0 ) { index = i ; break ; } } if ( index == - 1 ) return duplicate ; var num = 0 ; for ( i = 0 ; i < index ; i ++ ) num = num * 10 + ( s . charAt ( i ) . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; num = num * 10 + ( s . charAt ( index ) . charCodeAt ( 0 ) - ( ' ' ) . charCodeAt ( 0 ) - 1 ) ; for ( i = index + 1 ; i < s . length ; i ++ ) num = num * 10 + 9 ; return num ; } var N = 24578 ; document . write ( largestNumber ( N ) ) ;
Count number less than N which are product of perfect squares | Function to count number less than N which are product of any two perfect squares ; Driver Code
function countNumbers ( N ) { return parseInt ( Math . sqrt ( N ) , 10 ) - 1 ; } let N = 36 ; document . write ( countNumbers ( N ) ) ;
Count ordered pairs with product less than N | Function to return count of Ordered pairs whose product are less than N ; Initialize count to 0 ; count total pairs ; multiply by 2 to get ordered_pairs ; subtract redundant pairs ( a , b ) where a == b . ; return answer ; Driver code ; function call to print required answer
function countOrderedPairs ( N ) { var count_pairs = 0 ; for ( var i = 1 ; i <= Math . sqrt ( N - 1 ) ; ++ i ) { for ( var j = i ; j * i < N ; ++ j ) ++ count_pairs ; } count_pairs *= 2 ; count_pairs -= parseInt ( Math . sqrt ( N - 1 ) ) ; return count_pairs ; } var N = 5 ; document . write ( countOrderedPairs ( N ) ) ;
Absolute Difference of all pairwise consecutive elements in an array | Function to print pairwise absolute difference of consecutive elements ; absolute difference between consecutive numbers ; Driver Code
function pairwiseDifference ( arr , n ) { var diff ; for ( i = 0 ; i < n - 1 ; i ++ ) { diff = Math . abs ( arr [ i ] - arr [ i + 1 ] ) ; document . write ( diff + " " ) ; } } var arr = [ 4 , 10 , 15 , 5 , 6 ] ; var n = arr . length ; pairwiseDifference ( arr , n ) ;
Find the sum of all multiples of 2 and 5 below N | Function to find sum of AP series ; Number of terms ; Function to find the sum of all multiples of 2 and 5 below N ; Since , we need the sum of multiples less than N ; Driver code
function sumAP ( n , d ) { n = parseInt ( n / d ) ; return ( n ) * ( ( 1 + n ) * parseInt ( d ) / 2 ) ; } function sumMultiples ( n ) { n -- ; return sumAP ( n , 2 ) + sumAP ( n , 5 ) - sumAP ( n , 10 ) ; } n = 20 ; document . write ( sumMultiples ( n ) ) ;
Find the total marks obtained according to given marking scheme | Function that calculates marks . ; for not attempt score + 0 ; for each correct answer score + 3 ; for each wrong answer score - 1 ; calculate total marks ; Driver code
function markingScheme ( N , answerKey , studentAnswer ) { var positive = 0 , negative = 0 , notattempt = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( studentAnswer [ i ] == 0 ) notattempt ++ ; else if ( answerKey [ i ] == studentAnswer [ i ] ) positive ++ ; else if ( answerKey [ i ] != studentAnswer [ i ] ) negative ++ ; } return ( positive * 3 ) + ( negative * - 1 ) ; } var answerKey = [ 1 , 2 , 3 , 4 , 1 ] ; var studentAnswer = [ 1 , 2 , 3 , 4 , 0 ] ; var N = answerKey . length ; document . write ( markingScheme ( N , answerKey , studentAnswer ) ) ;
Find the Product of first N Prime Numbers | Javascript implementation of above solution ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Set all multiples of p to non - prime ; find the product of 1 st N prime numbers ; count of prime numbers ; product of prime numbers ; if the number is prime add it ; increase the count ; get to next number ; create the sieve ; find the value of 1 st n prime numbers
let MAX = 10000 ; let prime = new Array ( MAX + 1 ) . fill ( true ) ; function SieveOfEratosthenes ( ) { prime [ 1 ] = false ; for ( let p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } } function solve ( n ) { let count = 0 ; let num = 1 ; let prod = 1 ; while ( count < n ) { if ( prime [ num ] == true ) { prod *= num ; count ++ ; } num ++ ; } return prod ; } SieveOfEratosthenes ( ) ; let n = 5 ; document . write ( solve ( n ) ) ;
Program to find count of numbers having odd number of divisors in given range | Function to return the count of divisors of a number ; Count the powers of the current prime i which divides a ; Update the count of divisors ; Reset the count ; If the remaining a is prime then a ^ 1 will be one of its prime factors ; Function to count numbers having odd number of divisors in range [ A , B ] ; To store the count of elements having odd number of divisors ; Iterate from a to b and find the count of their divisors ; To store the count of divisors of i ; If the divisor count of i is odd ; Driver code
function divisor ( a ) { let div = 1 , count = 0 ; for ( let i = 2 ; i <= Math . sqrt ( a ) ; i ++ ) { while ( a % i == 0 ) { count ++ ; a = parseInt ( a / i , 10 ) ; } div = div * ( count + 1 ) ; count = 0 ; } if ( a > 1 ) { div = div * ( 2 ) ; } return div ; } function OddDivCount ( a , b ) { let res = 0 ; for ( let i = a ; i <= b ; ++ i ) { let divCount = divisor ( i ) ; if ( divCount % 2 == 1 ) { ++ res ; } } return res ; } let a = 1 , b = 10 ; document . write ( OddDivCount ( a , b ) ) ;
Check if there is any pair in a given range with GCD is divisible by k | Returns count of numbers in [ l r ] that are divisible by k . ; Add 1 explicitly as l is divisible by k ; l is not divisible by k ; Driver Code
function Check_is_possible ( l , r , k ) { var div_count = ( r / k ) - ( l / k ) ; if ( l % k == 0 ) { div_count ++ ; } return ( div_count > 1 ) ; } var l = 30 , r = 70 , k = 10 ; if ( Check_is_possible ( l , r , k ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Find sum of N | calculate sum of Nth group ; Driver code
function nth_group ( n ) { return n * ( 2 * Math . pow ( n , 2 ) + 1 ) ; } let N = 5 ; document . write ( nth_group ( N ) ) ;
Find if a molecule can be formed from 3 atoms using their valence numbers | Function to check if it is possible ; Driver code
function printPossible ( a , b , c ) { if ( ( a + b + c ) % 2 != 0 a + b < c ) document . write ( " " ) ; else document . write ( " " ) ; } let a = 2 , b = 4 , c = 2 ; printPossible ( a , b , c ) ;
Check if a number is a Trojan Number | Function to check if a number can be expressed as x ^ y ; Try all numbers from 2 to sqrt ( n ) as base ; Keep increasing y while power ' p ' is smaller than n . ; Function to check if a number is Strong ; count the number for each prime factor ; minimum number of prime divisors should be 2 ; Function to check if a number is Trojan Number ; Driver Code
function isPerfectPower ( n ) { if ( n == 1 ) { return true ; } for ( var x = 2 ; x <= Math . sqrt ( n ) ; x ++ ) { var y = 2 ; var p = parseInt ( Math . pow ( x , y ) ) ; while ( p <= n && p > 0 ) { if ( p == n ) { return true ; } y ++ ; p = parseInt ( Math . pow ( x , y ) ) ; } } return false ; } function isStrongNumber ( n ) { var count = new Map ( ) ; while ( n % 2 == 0 ) { n = n / 2 ; if ( count . has ( 2 ) ) { count . set ( 2 , count . get ( 2 ) + 1 ) ; } else { count . set ( 2 , 1 ) ; } } for ( var i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { n = n / i ; if ( count . has ( i ) ) { count . set ( i , count . get ( i ) + 1 ) ; } else { count . set ( i , 1 ) ; } } } if ( n > 2 ) { if ( count . has ( n ) ) { count . set ( n , count . get ( n ) + 1 ) ; } else { count . set ( n , 1 ) ; } } var flag = 0 ; const iterator = count [ Symbol . iterator ] ( ) ; let itr = iterator . next ( ) for ( let i = 0 ; i < count . size ; i ++ ) { console . log ( itr . value , itr . done ) if ( itr . value == 1 ) { flag = 1 ; break ; } itr = iterator . next ( ) } if ( flag == 1 ) { return false ; } else { return true ; } } function isTrojan ( n ) { if ( ! isPerfectPower ( n ) && isStrongNumber ( n ) ) { return true ; } else { return false ; } } var n = 108 ; if ( isTrojan ( n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Find the sum of first N terms of the series 2 Ã — 3 + 4 Ã — 4 + 6 Ã — 5 + 8 Ã — 6 + ... | calculate sum upto N term of series ; Driver code
function Sum_upto_nth_Term ( n ) { let r = n * ( n + 1 ) * ( 2 * n + 7 ) / 3 ; document . write ( r ) ; } let N = 5 ; Sum_upto_nth_Term ( N ) ;
Absolute Difference between the Sum of Non | Function to find the difference between the sum of non - primes and the sum of primes of an array . ; Find maximum value in the array ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Store the sum of primes in S1 and the sum of non primes in S2 ; the number is prime ; the number is non - prime ; Return the absolute difference ; Get the array ; Find the absolute difference
function CalculateDifference ( arr , n ) { var max_val = Number . MIN_VALUE ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > max_val ) max_val = arr [ i ] ; } var prime = Array ( max_val + 1 ) ; for ( i = 0 ; i <= max_val ; i ++ ) prime [ i ] = true ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } var S1 = 0 , S2 = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { S1 += arr [ i ] ; } else if ( arr [ i ] != 1 ) { S2 += arr [ i ] ; } } return Math . abs ( S2 - S1 ) ; } var arr = [ 1 , 3 , 5 , 10 , 15 , 7 ] ; var n = arr . length ; document . write ( CalculateDifference ( arr , n ) ) ;
Program to find sum of 1 + x / 2 ! + x ^ 2 / 3 ! + ... + x ^ n / ( n + 1 ) ! | Function to compute the series sum ; To store the value of S [ i - 1 ] ; Iterate over n to store sum in total ; Update previous with S [ i ] ; Get x and n ; Find and print the sum
function sum ( x , n ) { let total = 1.0 ; let previous = 1.0 ; for ( let i = 1 ; i <= n ; i ++ ) { previous = ( ( previous * x ) / ( i + 1 ) ) ; total = total + previous ; } return total ; } let x = 5 , n = 4 ; document . write ( " " + sum ( x , n ) ) ;
Count number of integers less than or equal to N which has exactly 9 divisors | Function to count factors in O ( N ) ; iterate and check if factor or not ; Function to count numbers having exactly 9 divisors ; check for all numbers <= N ; check if exactly 9 factors or not ; Driver Code
function numberOfDivisors ( num ) { let c = 0 ; for ( let i = 1 ; i <= num ; i ++ ) { if ( num % i == 0 ) { c += 1 ; } } return c ; } function countNumbers ( n ) { let c = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { if ( numberOfDivisors ( i ) == 9 ) c += 1 ; } return c ; } let n = 1000 ; document . write ( countNumbers ( n ) ) ;
Number of distinct integers obtained by lcm ( X , N ) / X | Function to count the number of distinct integers ontained by lcm ( x , num ) / x ; iterate to count the number of factors ; Driver Code
function numberOfDistinct ( n ) { var ans = 0 ; for ( i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { ans ++ ; if ( ( n / i ) != i ) ans ++ ; } } return ans ; } var n = 3 ; document . write ( numberOfDistinct ( n ) ) ;
Ulam Number Sequence | Javascript code to print nth Ulam number ; Array to store Ulam Number ; function to compute ulam Number ; push First 2 two term of the sequence in the array for further calculation ; loop to generate Ulam number ; traverse the array and check if i can be represented as sum of two distinct element of the array ; If count is 1 that means i can be represented as sum of two distinct terms of the sequence ; i is ulam number ; Pre compute Ulam Number sequence ; Print nth Ulam number
var MAX = 10000 ; var arr = [ ] ; function ulam ( ) { arr . push ( 1 ) ; arr . push ( 2 ) ; for ( var i = 3 ; i < MAX ; i ++ ) { var count = 0 ; for ( var j = 0 ; j < arr . length - 1 ; j ++ ) { for ( var k = j + 1 ; k < arr . length ; k ++ ) { if ( arr [ j ] + arr [ k ] == i ) { count ++ ; } if ( count > 1 ) break ; } if ( count > 1 ) break ; } if ( count == 1 ) { arr . push ( i ) ; } } } ulam ( ) ; var n = 9 ; document . write ( arr [ n - 1 ] ) ;
Find the number of rectangles of size 2 * 1 which can be placed inside a rectangle of size n * m | Function to Find the number of rectangles of size 2 * 1 can be placed inside a rectangle of size n * m ; If n is even ; If m is even ; If both are odd ; Driver Code ; Function call
function NumberOfRectangles ( n , m ) { if ( n % 2 == 0 ) return ( n / 2 ) * m ; else if ( m % 2 == 0 ) return ( m / 2 ) * n ; return ( n * m - 1 ) / 2 ; } var n = 3 , m = 3 ; document . write ( NumberOfRectangles ( n , m ) ) ;
Next greater Number than N with the same quantity of digits A and B | Recursive function to find the required number ; If the resulting number is >= n and count of a = count of b , return the number ; select minimum of two and call the function again ; Function to find the number next greater Number than N with the same quantity of digits A and B ; Driver code
function findNumUtil ( res , a , aCount , b , bCount , n ) { if ( res > 1e11 ) return 1e11 ; if ( aCount == bCount && res >= n ) return res ; return Math . min ( findNumUtil ( res * 10 + a , a , aCount + 1 , b , bCount , n ) , findNumUtil ( res * 10 + b , a , aCount , b , bCount + 1 , n ) ) ; } function findNum ( n , a , b ) { let result = 0 ; let aCount = 0 ; let bCount = 0 ; return findNumUtil ( result , a , aCount , b , bCount , n ) ; } let N = 4500 ; let A = 4 ; let B = 7 ; document . write ( findNum ( N , A , B ) ) ;
Minimum and maximum number of N chocolates after distribution among K students | Driver code
var n = 7 ; var k = 3 ; if ( n % k == 0 ) document . write ( n / k + " " + n / k ) ; else document . write ( ( n - ( n % k ) ) / k + " " + ( ( ( n - ( n % k ) ) / k ) + 1 ) ) ;
Total money to be paid after traveling the given number of hours | JavaScript implementation of the above approach ; calculating hours travelled
var m = 50 , n = 5 , x = 67 , h = 2927 ; var z = ( Math . ceil ( h / 60 * 1.0 ) ) ; if ( z <= n ) document . write ( z * m ) ; else document . write ( n * m + ( z - n ) * x ) ;
Absolute difference between sum and product of roots of a quartic equation | Function taking coefficient of each term of equation as input ; Finding sum of roots ; Finding product of roots ; Absolute difference ; Driver Code
function sumProductDifference ( a , b , c , d , e ) { var rootSum = ( - 1 * b ) / a ; var rootProduct = e / a ; return Math . abs ( rootSum - rootProduct ) ; } document . write ( sumProductDifference ( 8 , 4 , 6 , 4 , 1 ) ) ;
Number of solutions of n = x + n âŠ • x | Function to find the number of solutions of n = n xor x ; Counter to store the number of solutions found ; Driver code
function numberOfSolutions ( n ) { let c = 0 ; for ( let x = 0 ; x <= n ; ++ x ) if ( n == x + n ^ x ) ++ c ; return c ; } let n = 3 ; document . write ( numberOfSolutions ( n ) ) ;
Program to find minimum number of lectures to attend to maintain 75 % | Method to compute minimum lecture ; Formula to compute ; Driver Code
function minimumLectures ( m , n ) { let ans = 0 ; if ( n < Math . ceil ( 0.75 * m ) ) ans = Math . ceil ( ( ( 0.75 * m ) - n ) / 0.25 ) ; else ans = 0 ; return ans ; } let M = 9 , N = 1 ; document . write ( minimumLectures ( M , N ) ) ;
Count Numbers with N digits which consists of odd number of 0 's | Function to count Numbers with N digits which consists of odd number of 0 's ; Driver code
function countNumbers ( N ) { return parseInt ( ( Math . pow ( 10 , N ) - Math . pow ( 8 , N ) ) / 2 , 10 ) ; } let n = 5 ; document . write ( countNumbers ( n ) ) ;
Sum of all Primes in a given range using Sieve of Eratosthenes | Jacascript program to find sum of primes ; prefix [ i ] is going to store sum of primes till i ( including i ) . ; Function to build the prefix sum array ; Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Build prefix array ; Function to return sum of prime in range ; Driver code
MAX = 10000 ; prefix = new Array ( MAX + 1 ) ; function buildPrefix ( ) { prime = new Array ( MAX + 1 ) ; prime . fill ( true ) ; for ( var p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( var i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } prefix [ 0 ] = prefix [ 1 ] = 0 ; for ( var p = 2 ; p <= MAX ; p ++ ) { prefix [ p ] = prefix [ p - 1 ] ; if ( prime [ p ] ) prefix [ p ] += p ; } } function sumPrimeRange ( L , R ) { buildPrefix ( ) ; return prefix [ R ] - prefix [ L - 1 ] ; } var L = 10 , R = 20 ; document . write ( sumPrimeRange ( L , R ) + " " ) ;
Sum of the first N terms of the series 5 , 12 , 23 , 38. ... | Function to calculate the sum ; number of terms to be included in sum ; find the Sn
function calculateSum ( n ) { return 2 * ( n * ( n + 1 ) * ( 2 * n + 1 ) / 6 ) + n * ( n + 1 ) / 2 + 2 * ( n ) ; } let n = 3 ; document . write ( " " + calculateSum ( n ) ) ;
Program to find number of solutions in Quadratic Equation | Method to check for solutions of equations ; If the expression is greater than 0 , then 2 solutions ; If the expression is equal 0 , then 2 solutions ; Else no solutions ; Driver Code
function checkSolution ( a , b , c ) { if ( ( ( b * b ) - ( 4 * a * c ) ) > 0 ) document . write ( " " ) ; else if ( ( ( b * b ) - ( 4 * a * c ) ) == 0 ) document . write ( " " ) ; else document . write ( " " ) ; } var a = 2 , b = 5 , c = 2 ; checkSolution ( a , b , c ) ;
Program to convert KiloBytes to Bytes and Bits | Function to calculates the bits ; Calculates Bits 1 kilobytes ( s ) = 8192 bits ; Function to calculates the bytes ; Calculates Bytes 1 KB = 1024 bytes ; Driver code
function Bits ( kilobytes ) { var Bits = 0 ; Bits = kilobytes * 8192 ; return Bits ; } function Bytes ( kilobytes ) { var Bytes = 0 ; Bytes = kilobytes * 1024 ; return Bytes ; } var kilobytes = 1 ; document . write ( kilobytes + " " + Bytes ( kilobytes ) + " " + Bits ( kilobytes ) + " " ) ;
Program to find the Hidden Number | Getting the size of array ; Getting the array of size n ; Solution ; Finding sum of the array elements ; Dividing sum by size n ; Print x , if found
let n = 3 ; let a = [ 1 , 2 , 3 ] ; let i = 0 ; let sum = 0 ; for ( i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } let x = sum / n ; if ( x * n == sum ) document . write ( x ) ; else document . write ( " " ) ;
Find sum of the series ? 3 + ? 12 + ... ... ... upto N terms | Function to find the sum ; Apply AP formula ; number of terms
function findSum ( n ) { return Math . sqrt ( 3 ) * ( n * ( n + 1 ) / 2 ) ; } let n = 10 ; document . write ( findSum ( n ) . toFixed ( 4 ) ) ;
Find the sum of the series x ( x + y ) + x ^ 2 ( x ^ 2 + y ^ 2 ) + x ^ 3 ( x ^ 3 + y ^ 3 ) + ... + x ^ n ( x ^ n + y ^ n ) | Function to return required sum ; sum of first series ; sum of second series ; Driver code ; function call to print sum
function sum ( x , y , n ) { sum1 = ( Math . pow ( x , 2 ) * ( Math . pow ( x , 2 * n ) - 1 ) ) / ( Math . pow ( x , 2 ) - 1 ) ; sum2 = ( x * y * ( Math . pow ( x , n ) * Math . pow ( y , n ) - 1 ) ) / ( x * y - 1 ) ; return sum1 + sum2 ; } let x = 2 ; let y = 2 ; let n = 2 ; document . write ( sum ( x , y , n ) ) ;
Find any pair with given GCD and LCM | Function to print the pairs ; Driver Code
function printPair ( g , l ) { document . write ( g + " " + l ) ; } var g = 3 , l = 12 ; printPair ( g , l ) ;
Sum of first n terms of a given series 3 , 6 , 11 , ... . . | Function to calculate the sum ; starting number ; Common Ratio ; Common difference ; N th term to be find ; find the Sn
function calculateSum ( n ) { let a1 = 1 , a2 = 2 ; let r = 2 ; let d = 1 ; return ( n ) * ( 2 * a1 + ( n - 1 ) * d ) / 2 + a2 * ( Math . pow ( r , n ) - 1 ) / ( r - 1 ) ; } let n = 5 ; document . write ( " " + calculateSum ( n ) ) ;
Maximum of sum and product of digits until number is reduced to a single digit | Function to sum the digits until it becomes a single digit ; Function to product the digits until it becomes a single digit ; Loop to do sum while sum is not less than or equal to 9 ; Function to find the maximum among repeated sum and repeated product ; Driver code
function repeatedSum ( n ) { if ( n == 0 ) return 0 ; return ( n % 9 == 0 ) ? 9 : ( n % 9 ) ; } function repeatedProduct ( n ) { var prod = 1 ; while ( n > 0 prod > 9 ) { if ( n == 0 ) { n = prod ; prod = 1 ; } prod *= n % 10 ; n = parseInt ( n / 10 ) ; } return prod ; } function maxSumProduct ( N ) { if ( N < 10 ) return N ; return Math . max ( repeatedSum ( N ) , repeatedProduct ( N ) ) ; } var n = 631 ; document . write ( maxSumProduct ( n ) ) ;