codeparrot/xlcost-text-to-code huggingface dataset:
Check if there is any pair in a given range with GCD is divisible by k | function to count such possible numbers ; if i is divisible by k ; if count of such numbers is greater than one ; Driver code
function Check_is_possible ( l , r , k ) { var count = 0 ; for ( i = l ; i <= r ; i ++ ) { if ( i % k == 0 ) { count ++ ; } } return ( count > 1 ) ; } var l = 4 , r = 12 ; var k = 5 ; if ( Check_is_possible ( l , r , k ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Check if any permutation of N equals any power of K | function to check if N and K are anagrams ; Function to check if any permutation of N exist such that it is some power of K ; generate all power of K under 10 ^ 18 ; check if any power of K is valid ; Driver code ; function call to print required answer
function isValid ( N , K ) { let m1 = new Set ( ) ; let m2 = new Set ( ) ; while ( N > 0 ) { m1 . add ( N % 10 ) ; N = Math . floor ( N / 10 ) ; } while ( K > 0 ) { m2 . add ( K % 10 ) ; K = Math . floor ( K / 10 ) ; } let s = new Set ( [ ... m1 , ... m2 ] ) return s . size == m1 . size && s . size == m2 . size } function anyPermutation ( N , K ) { let powK = new Array ( 100 + 1 ) , Limit = ( Math . pow ( 10 , 18 ) ) ; for ( let i = 0 ; i < 101 ; i ++ ) powK [ i ] = 0 ; powK [ 0 ] = K ; let i = 1 ; while ( powK [ i - 1 ] * K < Limit ) { powK [ i ] = powK [ i - 1 ] * K ; i ++ ; } for ( let j = 0 ; j < i ; j ++ ) { if ( isValid ( N , powK [ j ] ) ) { return " " ; } } return " " ; } let N = 96889010407 , K = 7 ; document . write ( anyPermutation ( N , K ) ) ;
Sum of first N natural numbers which are divisible by 2 and 7 | Function to calculate the sum of numbers divisible by 2 or 7 ; Driver code
function sum ( N ) { var S1 , S2 , S3 ; S1 = ( ( ( N / 2 ) ) * parseInt ( 2 * 2 + parseInt ( N / 2 - 1 ) * 2 ) / 2 ) ; S2 = ( parseInt ( parseInt ( N / 7 ) ) * ( 2 * 7 + parseInt ( N / 7 - 1 ) * 7 ) / 2 ) ; S3 = ( parseInt ( parseInt ( N / 14 ) ) * ( 2 * 14 + parseInt ( N / 14 - 1 ) * 14 ) / 2 ) ; return S1 + S2 - S3 ; } var N = 20 ; document . write ( sum ( N ) ) ;
Ways to color a skewed tree such that parent and child have different colors | fast_way is recursive method to calculate power ; Driver code
function fastPow ( N , K ) { if ( K == 0 ) return 1 ; let temp = fastPow ( N , Math . floor ( K / 2 ) ) ; if ( K % 2 == 0 ) return temp * temp ; else return N * temp * temp ; } function countWays ( N , K ) { return K * fastPow ( K - 1 , N - 1 ) ; } let N = 3 , K = 3 ; document . write ( countWays ( N , K ) ) ;
Sum of nth terms of Modified Fibonacci series made by every pair of two arrays | javascript program to find sum of n - th terms of a Fibonacci like series formed using first two terms of two arrays . ; if sum of first term is required ; if sum of second term is required ; fibonacci series used to find the nth term of every series ; as every b [ i ] term appears m times and every a [ i ] term also appears m times ; m is the size of the array
function sumNth ( A , B , m , n ) { var res = 0 ; if ( n == 1 ) { for ( let i = 0 ; i < m ; i ++ ) res = res + A [ i ] ; } else if ( n == 2 ) { for ( let i = 0 ; i < m ; i ++ ) res = res + B [ i ] * m ; } else { var f = Array ( n ) . fill ( 0 ) ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; for ( let i = 2 ; i < n ; i ++ ) f [ i ] = f [ i - 1 ] + f [ i - 2 ] ; for ( i = 0 ; i < m ; i ++ ) { res = res + ( m * ( B [ i ] * f [ n - 1 ] ) ) + ( m * ( A [ i ] * f [ n - 2 ] ) ) ; } } return res ; } var A = [ 1 , 2 , 3 ] ; var B = [ 4 , 5 , 6 ] ; var n = 3 ; var m = A . length ; document . write ( sumNth ( A , B , m , n ) ) ;
Puzzle | Minimum distance for Lizard | side of cube ; understand from diagram ; understand from diagram ; minimum distance
var a = 5 ; var AC = a ; var CE = 2 * a ; var shortestDistace = Math . sqrt ( AC * AC + CE * CE ) ; document . write ( shortestDistace . toFixed ( 4 ) ) ;
Find Sum of Series 1 ^ 2 | Function to find sum of series ; If n is even ; If n is odd ; return the result ; Get n ; Find the sum ; Get n ; Find the sum
function sum_of_series ( n ) { let result = 0 ; if ( n % 2 == 0 ) { result = - ( n * ( n + 1 ) ) / 2 ; } else { result = ( n * ( n + 1 ) ) / 2 ; } return result ; } let n = 3 ; document . write ( sum_of_series ( n ) + " " ) ; n = 10 ; document . write ( sum_of_series ( n ) ) ;
Check whether the given numbers are Cousin prime or not | Function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Returns true if n1 and n2 are Cousin primes ; Check if they differ by 4 or not ; Check if both are prime number or not ; Get the 2 numbers ; Check the numbers for cousin prime
function isPrime ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( var i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } function isCousinPrime ( n1 , n2 ) { if ( Math . abs ( n1 - n2 ) != 4 ) return false ; else return ( isPrime ( n1 ) && isPrime ( n2 ) ) ; } var n1 = 7 , n2 = 11 ; if ( isCousinPrime ( n1 , n2 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Chen Prime Number | Utility function to check whether number is semiprime or not ; Increment count of prime numbers ; If number is greater than 1 , add it to the count variable as it indicates the number remain is prime number ; Return '1' if count is equal to '2' else return '0' ; Utility function to check whether the given number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to check Chen prime number ; Driver code
function isSemiprime ( num ) { var cnt = 0 ; for ( var i = 2 ; cnt < 2 && i * i <= num ; ++ i ) while ( num % i == 0 ) num /= i ++ cnt ; if ( num > 1 ) ++ cnt ; return cnt == 2 ; } function isPrime ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( var i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } function isChenPrime ( n ) { if ( isPrime ( n ) && ( isSemiprime ( n + 2 ) || isPrime ( n + 2 ) ) ) return true ; else return false ; } var n = 7 ; if ( isChenPrime ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Thabit number | Utility function to check power of two ; function to check if the given number is Thabit Number ; Add 1 to the number ; Divide the number by 3 ; Check if the given number is power of 2 ; Driver Program ; Check if number is thabit number
function isPowerOfTwo ( n ) { return ( n && ! ( n & ( n - 1 ) ) ) ; } function isThabitNumber ( n ) { n = n + 1 ; if ( n % 3 == 0 ) n = n / 3 ; else return false ; if ( isPowerOfTwo ( n ) ) return true ; else return false ; } var n = 47 ; if ( isThabitNumber ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Sum of all the prime numbers in a given range | Suppose the constraint is N <= 1000 ; Declare an array for dynamic approach ; Method to compute the array ; Declare an extra array as arr ; Iterate the loop till sqrt ( n ) Time Complexity is O ( log ( n ) X sqrt ( n ) ) ; if ith element of arr is 0 i . e . marked as prime ; mark all of it 's multiples till N as non-prime  by setting the locations to 1 ; Update the array ' dp ' with the running sum of prime numbers within the range [ 1 , N ] Time Complexity is O ( n ) ; Here , dp [ i ] is the sum of all the prime numbers within the range [ 1 , i ] ; Driver code ; Compute dp
let N = 1000 ; let dp = new Array ( N + 1 ) ; for ( let i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = 0 ; } function sieve ( ) { let arr = new Array ( N + 1 ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { arr [ i ] = 0 ; } arr [ 0 ] = 1 ; arr [ 1 ] = 1 ; for ( let i = 2 ; i <= Math . ceil ( Math . sqrt ( N ) + 1 ) ; i ++ ) if ( arr [ i ] == 0 ) for ( let j = i * i ; j <= N ; j += i ) arr [ j ] = 1 ; let runningPrimeSum = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { if ( arr [ i ] == 0 ) runningPrimeSum += i ; dp [ i ] = runningPrimeSum ; } } let l = 4 , r = 13 ; sieve ( ) ; document . write ( dp [ r ] - dp [ l - 1 ] ) ;
Smallest Integer to be inserted to have equal sums | Function to find the minimum value to be added ; Variable to store entire array sum ; Variables to store sum of subarray1 and subarray 2 ; minimum value to be added ; Traverse through the array ; Sum of both halves ; Calculate minimum number to be added ; Driver code ; Length of array
function findMinEqualSums ( a , N ) { let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { sum += a [ i ] ; } let sum1 = 0 , sum2 = 0 ; let min = Number . MAX_VALUE ; for ( let i = 0 ; i < N ; i ++ ) { sum1 += a [ i ] ; sum2 = sum - sum1 ; if ( Math . abs ( sum1 - sum2 ) < min ) { min = Math . abs ( sum1 - sum2 ) ; } if ( min == 0 ) { break ; } } return min ; } let a = [ 3 , 2 , 1 , 5 , 7 , 8 ] ; let N = a . length ; document . write ( findMinEqualSums ( a , N ) ) ;
Sum of the 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 sum of 1 st N prime numbers ; count of prime numbers ; sum 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
var MAX = 10000 ; var prime = Array . from ( { length : parseInt ( ( MAX + 1.0 ) ) } , ( _ , i ) => false ) ; function SieveOfEratosthenes ( ) { for ( i = 0 ; i <= MAX ; i ++ ) prime [ i ] = true ; prime [ 1 ] = false ; for ( p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } } function solve ( n ) { var count = 0 , num = 1 ; var sum = 0 ; while ( count < n ) { if ( prime [ num ] ) { sum += num ; count ++ ; } num ++ ; } return parseInt ( sum ) ; } SieveOfEratosthenes ( ) ; var n = 4 ; document . write ( " " + solve ( n ) ) ;
Implementation of Wilson Primality test | Function to calculate the factorial ; Function to check if the number is prime or not ; Driver code
function fact ( p ) { if ( p <= 1 ) return 1 ; return p * fact ( p - 1 ) ; } function isPrime ( p ) { if ( p == 4 ) return false ; if ( fact ( p >> 1 ) % p == 0 ) { return 0 ; } return 1 ; } document . write ( isPrime ( 127 ) ) ;
Find the total Number of Digits in ( N ! ) N | Function to find the total Number of Digits in ( N ! ) ^ N ; Finding X ; Calculating N * X ; Floor ( N * X ) + 1 equivalent to floor ( sum ) + 1 ; Driver code
function CountDigits ( n ) { if ( n == 1 ) return 1 ; var sum = 0 ; for ( i = 2 ; i <= n ; ++ i ) { sum += ( Math . log ( i ) / Math . log ( 10 ) ) ; } sum *= n ; return Math . ceil ( sum ) ; } var N = 5 ; document . write ( CountDigits ( N ) ) ;
Find the value of max ( f ( x ) ) | Javascript implementation of above approach ; Function to calculate the value ; forming the prefix sum arrays ; Taking the query ; finding the sum in the range l to r in array a ; finding the sum in the range l to r in array b ; Finding the max value of the function ; Finding the min value of the function ; Driver code
let MAX = 200006 ; let CONS = 32766 ; function calc ( a , b , lr , q , n ) { let M , m , i , j , k , l , r , suma , sumb , cc ; cc = 0 ; for ( i = 0 ; i < n - 1 ; ++ i ) { a [ i + 1 ] += a [ i ] ; b [ i + 1 ] += b [ i ] ; } while ( q != 0 ) { l = lr [ cc ++ ] ; r = lr [ cc ++ ] ; l -= 2 ; r -= 1 ; suma = a [ r ] ; sumb = b [ r ] ; if ( l >= 0 ) { suma -= a [ l ] ; sumb -= b [ l ] ; } M = Math . max ( CONS * suma + CONS * sumb , - CONS * suma - CONS * sumb ) ; M = Math . max ( M , Math . max ( CONS * suma - CONS * sumb , - CONS * suma + CONS * sumb ) ) ; m = Math . min ( CONS * suma + CONS * sumb , - CONS * suma - CONS * sumb ) ; m = Math . min ( m , Math . min ( CONS * suma - CONS * sumb , - CONS * suma + CONS * sumb ) ) ; document . write ( ( M - m ) + " " ) ; q -- ; } } let n = 5 , q = 2 ; let a = [ 0 , 7 , 3 , 4 , 5 ] ; let b = [ 0 , 3 , 1 , 2 , 3 ] ; let lr = new Array ( q * 2 ) ; lr [ 0 ] = 1 ; lr [ 1 ] = 1 ; lr [ 2 ] = 1 ; lr [ 3 ] = 3 ; calc ( a , b , lr , q , n ) ;
Program to find the Nth number of the series 2 , 10 , 24 , 44 , 70. ... . | function to return nth term of the series ; Taking n as 4 ; Prleting the nth term
function NthTerm ( n ) { let x = ( 3 * n * n ) % 1000000009 ; return ( x - n + 1000000009 ) % 1000000009 ; } let N = 4 ; document . write ( NthTerm ( N ) ) ;
Sum of first N natural numbers by taking powers of 2 as negative number | to store power of 2 ; to store presum of the power of 2 's ; function to find power of 2 ; to store power of 2 ; to store pre sum ; Function to find the sum ; first store sum of first n natural numbers . ; find the first greater number than given number then minus var of this from answer ; function call ; function call
power = Array ( 31 ) . fill ( 0 ) ; pre = Array ( 31 ) . fill ( 0 ) ; function PowerOfTwo ( ) { var x = 1 ; for ( i = 0 ; i < 31 ; i ++ ) { power [ i ] = x ; x *= 2 ; } pre [ 0 ] = 1 ; for ( i = 1 ; i < 31 ; i ++ ) pre [ i ] = pre [ i - 1 ] + power [ i ] ; } function Sum ( n ) { var ans = n * ( n + 1 ) / 2 ; for ( i = 0 ; i < 31 ; i ++ ) { if ( power [ i ] > n ) { ans -= 2 * pre [ i - 1 ] ; break ; } } return ans ; } PowerOfTwo ( ) ; var n = 4 ; document . write ( Sum ( n ) ) ;
Check if a number is Quartan Prime or not | Function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Driver Program ; Check if number is prime and of the form 16 * n + 1
function isPrime ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( var i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } var n = 17 ; if ( isPrime ( n ) && ( n % 16 == 1 ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Print a number strictly less than a given number such that all its digits are distinct . | Function to find a number less than n such that all its digits are distinct ; looping through numbers less than n ; initializing a hash array ; creating a copy of i ; initializing variables to compare lengths of digits ; counting frequency of the digits ; checking if each digit is present once ; Driver code
function findNumber ( n ) { for ( i = n - 1 ; i >= 0 ; i -- ) { var count = Array . from ( { length : 10 } , ( _ , i ) => 0 ) ; var x = i ; var count1 = 0 , count2 = 0 ; while ( x > 0 ) { count [ x % 10 ] ++ ; x = parseInt ( x / 10 ) ; count1 ++ ; } for ( j = 0 ; j < 10 ; j ++ ) { if ( count [ j ] == 1 ) count2 ++ ; } if ( count1 == count2 ) return i ; } return - 1 ; } var n = 8490 ; document . write ( findNumber ( n ) ) ;
Check if two Linked Lists are permutations of each other | A linked list node ; Function to check if two linked lists * are permutations of each other * first : reference to head of first linked list * second : reference to head of second linked list ; Variables to keep track of sum and multiplication ; Traversing through linked list and calculating sum and multiply ; Traversing through linked list and calculating sum and multiply ; Function to add a node at the beginning of Linked List ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; Driver Code ; First constructed linked list is : 12 . 35 . 1 . 10 . 34 . 1 ; Second constructed linked list is : 35 . 1 . 12 . 1 . 10 . 34
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function isPermutation ( first , second ) { var sum1 = 0 , sum2 = 0 , mul1 = 1 , mul2 = 1 ; var temp1 = first ; while ( temp1 != null ) { sum1 += temp1 . data ; mul1 *= temp1 . data ; temp1 = temp1 . next ; } var temp2 = second ; while ( temp2 != null ) { sum2 += temp2 . data ; mul2 *= temp2 . data ; temp2 = temp2 . next ; } return sum1 == sum2 && mul1 == mul2 ; } function push ( head_ref , new_data ) { var new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } var first = null ; first = push ( first , 1 ) ; first = push ( first , 34 ) ; first = push ( first , 10 ) ; first = push ( first , 1 ) ; first = push ( first , 35 ) ; first = push ( first , 12 ) ; var second = null ; second = push ( second , 35 ) ; second = push ( second , 1 ) ; second = push ( second , 12 ) ; second = push ( second , 1 ) ; second = push ( second , 10 ) ; second = push ( second , 34 ) ; if ( isPermutation ( first , second ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Find two distinct prime numbers with given product | Function to generate all prime numbers less than n ; Initialize all entries of bool array as true . A value in isPrime [ i ] will finally be false if i is Not a prime , else true isPrime [ n + 1 ] ; ; If isPrime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to print a prime pair with given product ; Generating primes using Sieve ; Traversing all numbers to find first pair ; Driver code
function SieveOfEratosthenes ( n , isPrime ) { isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( var i = 2 ; i <= n ; i ++ ) isPrime [ i ] = true ; for ( var p = 2 ; p * p <= n ; p ++ ) { if ( isPrime [ p ] == true ) { for ( var i = p * 2 ; i <= n ; i += p ) isPrime [ i ] = false ; } } } function findPrimePair ( n ) { var flag = 0 ; var isPrime = [ ] SieveOfEratosthenes ( n , isPrime ) ; for ( var i = 2 ; i < n ; i ++ ) { var x = n / i ; if ( isPrime [ i ] && isPrime [ x ] && x != i && x * i == n ) { document . write ( i + " " + x ) ; flag = 1 ; return ; } } if ( flag == 0 ) document . write ( " " ) ; } var n = 39 ; findPrimePair ( n ) ;
Program to find the common ratio of three numbers | Utility function ; Function to print a : b : c ; To print the given proportion in simplest form . ; Get the ratios ; Get ratio a : b1 ; Get ratio b2 : c ; Find the ratio a : b : c
function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } function solveProportion ( a , b1 , b2 , c ) { let A = a * b2 ; let B = b1 * b2 ; let C = b1 * c ; let gcd = __gcd ( __gcd ( A , B ) , C ) ; document . write ( A / gcd + " " + B / gcd + " " + C / gcd ) ; } let a , b1 , b2 , c ; a = 3 ; b1 = 4 ; b2 = 8 ; c = 9 ; solveProportion ( a , b1 , b2 , c ) ;
Number of divisors of a given number N which are divisible by K | Function to count number of divisors of N which are divisible by K ; Variable to store count of divisors ; Traverse from 1 to n ; increase the count if both the conditions are satisfied ; Driver code
function countDivisors ( n , k ) { var count = 0 , i ; for ( i = 1 ; i <= n ; i ++ ) { if ( n % i == 0 && i % k == 0 ) { count ++ ; } } return count ; } var n = 12 , k = 3 ; document . write ( countDivisors ( n , k ) ) ;
Calculate volume and surface area of a cone | javascript program to calculate Volume and Surface area of Cone ; Function to calculate Volume of cone ; Function to calculate Surface area of cone ; Driver Code ; Printing value of volume and surface area
const pi = 3.14159 ; function volume ( r , h ) { return ( ( 1 ) / ( 3 ) ) * pi * r * r * h ; } function surface_area ( r , s ) { return pi * r * s + pi * r * r ; } let radius = 5 ; let slant_height = 13 ; let height = 12 ; let vol , sur_area ; document . write ( " " + volume ( radius , height ) . toFixed ( 2 ) + " " ) ; document . write ( " " + surface_area ( radius , slant_height ) . toFixed ( 2 ) + " " ) ;
Program to find the Nth term of the series 0 , 14 , 40 , 78 , 124 , ... | calculate sum upto Nth term of series ; return the final sum ; Driver code
function nthTerm ( n ) { return 6 * Math . pow ( n , 2 ) - 4 * n - 2 ; } let N = 4 ; document . write ( nthTerm ( N ) ) ;
Program to find the Nth term of series 5 , 10 , 17 , 26 , 37 , 50 , 65 , 82 , ... | calculate Nth term of series ; return the final sum ; Driver code
function nthTerm ( n ) { return Math . pow ( n , 2 ) + 2 * n + 2 ; } let N = 4 ; document . write ( nthTerm ( N ) ) ;
Find nth term of a given recurrence relation | function to return required value ; Get the answer ; Return the answer ; Get the value of n ; function call to print result
function sum ( n ) { let ans = parseInt ( ( n * ( n - 1 ) ) / 2 ) ; return ans ; } let n = 5 ; document . write ( sum ( n ) ) ;
Program to find Nth term of the series 3 , 12 , 29 , 54 , 87 , ... | calculate Nth term of series ; Return Nth term ; declaration of number of terms ; Get the Nth term
function getNthTerm ( N ) { return 4 * Math . pow ( N , 2 ) - 3 * N + 2 ; } let N = 10 ; document . write ( getNthTerm ( N ) ) ;
Find sum of product of number in given series | Javascript program to find sum of product of number in given series ; function to calculate ( a ^ b ) % p ; Initialize result ; 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 ; function to return required answer ; modulo inverse of denominator ; calculating commentator part ; calculating t ! ; accumulating the final answer ; Driver code ; function call to print required sum
var MOD = 100000007 ; function power ( x , y , p ) { var res = 1 ; x = x % p ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } function sumProd ( n , t ) { var dino = power ( t + 1 , MOD - 2 , MOD ) ; var ans = 1 ; for ( var i = n + t + 1 ; i > n ; -- i ) ans = ( ans % MOD * i % MOD ) % MOD ; var tfact = 1 ; for ( var i = 1 ; i <= t ; ++ i ) tfact = ( tfact * i ) % MOD ; ans = ans * dino - tfact + MOD ; return ans % MOD ; } var n = 3 , t = 2 ; document . write ( sumProd ( n , t ) ) ;
Find the sum of series 3 , 7 , 13 , 21 , 31. ... | Function to calculate sum ; returning the final sum ; Driver Code number of terms to find the sum
function calculateSum ( n ) { return ( n * ( parseInt ( Math . pow ( n , 2 ) + 3 * n + 5 ) ) / 3 ) ; } var n = 25 ; document . write ( calculateSum ( n ) ) ;
Minimum Players required to win the game | javascript program to find minimum players required to win the game anyhow ; function to calculate ( a ^ b ) % ( 10 ^ 9 + 7 ) . ; function to find the minimum required player ; computing the nenomenator ; computing modulo inverse of denominator ; final result ; Driver code
var mod = 107 ; function power ( a , b ) { var res = 1 ; while ( b != 0 ) { if ( ( b & 1 ) != 0 ) { res *= a ; res = res % mod ; } b = parseInt ( b / 2 ) ; a *= a ; a %= mod ; } return res ; } function minPlayer ( n , k ) { var num = ( ( power ( k , n ) - 1 ) + mod ) % mod ; var den = ( power ( k - 1 , mod - 2 ) + mod ) % mod ; var ans = ( ( ( num * den ) % mod ) * k ) % mod ; return ans ; } var n = 3 , k = 3 ; document . write ( minPlayer ( n , k ) ) ;