codeparrot/xlcost-text-to-code huggingface dataset:
Find two distinct numbers such that their LCM lies in given range | Function to find two distinct numbers X and Y s . t . their LCM lies between L and R and X , Y are minimum possible ; Check if 2 * L lies in range L , R ; Print the answer ; Given value of ranges ; Function call
function answer ( L , R ) { if ( 2 * L <= R ) document . write ( L + " " + 2 * L + " " ) ; else document . write ( - 1 ) ; } let L = 3 , R = 8 ; answer ( L , R ) ;
Maximum count of pairwise co | Function to find the gcd of two numbers ; Function to of pairwise co - prime and common divisors of two numbers ; Initialize answer with 1 , to include 1 in the count ; Count of primes of gcd ( N , M ) ; Finding prime factors of gcd ; Increment count if it is divisible by i ; Return the total count ; Function Call for each pair to calculate the count of pairwise co - prime divisors ; Given array of pairs ; Function Call
function gcd ( x , y ) { if ( x % y == 0 ) return y ; else return gcd ( y , x % y ) ; } function countPairwiseCoprime ( N , M ) { let answer = 1 ; let g = gcd ( N , M ) ; let temp = g ; for ( let i = 2 ; i * i <= g ; i ++ ) { if ( temp % i == 0 ) { answer ++ ; while ( temp % i == 0 ) temp /= i ; } } if ( temp != 1 ) answer ++ ; return answer ; } function countCoprimePair ( arr , N ) { for ( let i = 0 ; i < N ; i ++ ) { document . write ( countPairwiseCoprime ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) + " " ) ; } } let arr = [ [ 12 , 18 ] , [ 420 , 660 ] ] ; let N = arr . length ; countCoprimePair ( arr , N ) ;
Product of absolute difference of every pair in given Array | Function to return the product of abs diff of all pairs ( x , y ) ; To store product ; Iterate all possible pairs ; Find the product ; Return product ; Given array arr [ ] ; Function Call
function getProduct ( a , n ) { var p = 1 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = i + 1 ; j < n ; j ++ ) { p *= Math . abs ( a [ i ] - a [ j ] ) ; } } return p ; } var arr = [ 1 , 2 , 3 , 4 ] ; var N = arr . length ; document . write ( getProduct ( arr , N ) ) ;
Check whether a number can be represented as difference of two consecutive cubes | Function to print the two consecutive numbers whose difference is N ; Iterate in the range [ 0 , 10 ^ 5 ] ; Function to check if N is a perfect cube ; Find floating point value of square root of x . ; If square root is an integer ; Function to check whether a number can be represented as difference of two consecutive cubes ; Check if 12 * N - 3 is a perfect square or not ; Given Number N
function print ( N ) { for ( let i = 0 ; i < 100000 ; i ++ ) { if ( parseInt ( Math . pow ( i + 1 , 3 ) , 10 ) - parseInt ( Math . pow ( i , 3 ) , 10 ) == N ) { document . write ( i + " " + ( i + 1 ) ) ; return ; } } } function isPerfectSquare ( x ) { let sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ; } function diffCube ( N ) { return isPerfectSquare ( 12 * N - 3 ) ; } let N = 19 ; if ( diffCube ( N ) != 0 ) { document . write ( " " + " " ) ; print ( N ) ; } else { document . write ( " " ) ; }
Sum of bit differences for numbers from 0 to N | Set 2 | Recursive function to find sum of different bits between consecutive numbers from 0 to N ; Base case ; Calculate the Nth term ; Given number ; Function call
function totalCountDifference ( n ) { if ( n == 1 ) return 1 ; return n + totalCountDifference ( Math . floor ( n / 2 ) ) ; } let N = 5 ; document . write ( totalCountDifference ( N ) ) ;
Sum of elements of a Geometric Progression ( GP ) in a 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 + 1 ; let d = arr [ 1 ] / arr [ 0 ] ; let ans = arr [ left - 1 ] ; if ( d == 1 ) ans = ans * d * k ; else ans = ans * ( Math . pow ( d , k ) - 1 / ( d - 1 ) ) ; return ans ; } let arr = [ 2 , 4 , 8 , 16 , 32 , 64 , 128 , 256 ] ; let queries = 3 ; let q = [ [ 2 , 4 ] , [ 2 , 6 ] , [ 5 , 8 ] ] ; let n = arr . length ; for ( let i = 0 ; i < queries ; i ++ ) document . write ( findSum ( arr , n , q [ i ] [ 0 ] , q [ i ] [ 1 ] ) ) ;
Find the first and last M digits from K | Function to find a ^ b modulo M ; Function to find the first and last M digits from N ^ K ; Calculate Last M digits ; Calculate First M digits ; Extract the number after decimal ; Find 10 ^ y ; Move the Decimal Point M - 1 digits forward ; Print the result ; Driver Code
function modPower ( a , b , M ) { var res = 1 ; while ( b ) { if ( b & 1 ) res = res * a % M ; a = a * a % M ; b >>= 1 ; } return res ; } function findFirstAndLastM ( N , K , M ) { var lastM = modPower ( N , K , Math . pow ( 10 , M ) ) ; var firstM ; var y = K * Math . log10 ( N * 1.0 ) ; y = y - parseInt ( y ) ; var temp = Math . pow ( 10.0 , y ) ; firstM = temp * Math . pow ( 10 , M - 1 ) ; document . write ( parseInt ( firstM ) + " " + parseInt ( lastM ) ) ; } var N = 12 , K = 12 , M = 4 ; findFirstAndLastM ( N , K , M ) ;
Minimize increment / decrement of Array elements to make each modulo K equal | Function to find the minimum operations required to make the modulo of each element of the array equal to each other ; Variable to store minimum operation required ; To store operation required to make all modulo equal ; Iterating through all possible modulo value ; Iterating through all different modulo obtained so far ; Calculating oprn required to make all modulos equal to x ; Checking the operations that will cost less ; Check operation that will cost less ; Update the minimum number of operations ; Returning the answer ; Function to store different modulos ; Set to store all different modulo ; Map to store count of all different modulo obtained ; Storing all different modulo count ; Insert into the set ; Increment count Increment count ; Function call to return value of min oprn required ; Driver Code
function Find_min ( diff_mod , count_mod , k ) { let min_oprn = Number . MAX_VALUE ; let oprn = 0 ; for ( let x = 0 ; x < k ; x ++ ) { oprn = 0 ; for ( let w of diff_mod . values ( ) ) { if ( w != x ) { if ( w == 0 ) { oprn += Math . min ( x , k - x ) * count_mod . get ( w ) ; } else { oprn += Math . min ( Math . abs ( x - w ) , k + x - w ) * count_mod . get ( w ) ; } } } if ( oprn < min_oprn ) min_oprn = oprn ; } return min_oprn ; } function Cal_min ( arr , n , k ) { let diff_mod = new Set ( ) ; let count_mod = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { diff_mod . add ( arr [ i ] % k ) ; if ( count_mod . has ( ( arr [ i ] % k ) ) ) count_mod . set ( arr [ i ] % k , count_mod . get ( arr [ i ] % k ) + 1 ) ; else count_mod . set ( arr [ i ] % k , 1 ) ; } return Find_min ( diff_mod , count_mod , k ) ; } let arr = [ 2 , 35 , 48 , 23 , 52 ] ; let n = arr . length ; let k = 3 ; document . write ( Cal_min ( arr , n , k ) ) ;
Minimize Steps required to obtain Sorted Order of an Array | Function to find GCD of two numbers ; Function to calculate the LCM of array elements ; Initialize result ; Function to find minimum steps required to obtain sorted sequence ; Inititalize dat [ ] array for Direct Address Table . ; Calculating steps required for each element to reach its sorted position ; Calculate LCM of the array ; Driver Code
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function findlcm ( arr , n ) { let ans = 1 ; for ( let i = 1 ; i <= n ; i ++ ) ans = ( ( ( arr [ i ] * ans ) ) / ( gcd ( arr [ i ] , ans ) ) ) ; return ans ; } function minimumSteps ( arr , n ) { let i ; let dat = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; for ( i = 1 ; i <= n ; i ++ ) dat [ arr [ i - 1 ] ] = i ; let b = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; let j = 0 , c ; for ( i = 1 ; i <= n ; i ++ ) { c = 1 ; j = dat [ i ] ; while ( j != i ) { c ++ ; j = dat [ j ] ; } b [ i ] = c ; } document . write ( findlcm ( b , n ) ) ; } let arr = [ 5 , 1 , 4 , 3 , 2 , 7 , 6 ] ; let N = arr . length ; minimumSteps ( arr , N ) ;
Largest Ratio Contiguous subarray | Function to return maximum of two double values ; Check if a is greater than b then return a ; Function that returns the Ratio of max Ratio subarray ; Variable to store the maximum ratio ; Compute the product while traversing for subarrays ; Calculate the ratio ; Update max ratio ; Print the answer ; Driver code
function maximum ( a , b ) { if ( a > b ) return a ; return b ; } function maxSubarrayRatio ( arr , n ) { var maxRatio = - 1000000000 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = i ; j < n ; j ++ ) { var ratio = arr [ i ] ; for ( var k = i + 1 ; k <= j ; k ++ ) { ratio = ratio / arr [ k ] ; } maxRatio = maximum ( maxRatio , ratio ) ; } } return maxRatio ; } var arr = [ 2 , 2 , 4 , - 0.2 , - 1 ] ; var n = arr . length ; document . write ( maxSubarrayRatio ( arr , n ) ) ;
Even Perfect Number | Function to check for perfect number ; Find a number close to 2 ^ q - 1 ; Calculate q - 1 ; Condition of perfect number ; Check whether q is prime or not ; Check whether 2 ^ q - 1 is a prime number or not ; Function to check for prime number ; Check whether it is equal to 2 or 3 ; Check if it can be divided by 2 and 3 then it is not prime number ; Check whether the given number be divide by other prime numbers ; Driver Code
function check ( num ) { let root = Math . floor ( Math . sqrt ( num ) ) ; let pow = Math . floor ( Math . log ( root ) / Math . log ( 2 ) ) ; if ( num == Math . floor ( Math . pow ( 2 , pow ) * ( Math . pow ( 2 , pow + 1 ) - 1 ) ) ) { if ( isPrime ( pow + 1 ) ) { if ( isPrime ( Math . floor ( Math . pow ( 2 , pow + 1 ) ) - 1 ) ) document . write ( " " ) ; else document . write ( " " ) ; } else document . write ( " " ) ; } else document . write ( " " ) ; } function isPrime ( n ) { if ( n <= 1 ) return false ; else if ( n == 2 n == 3 ) return true ; else { if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( let i = 5 ; i <= Math . floor ( Math . sqrt ( n ) ) ; i += 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; } return true ; } } let num = 6 ; check ( num ) ;
Twin Pythagorean triplets in an array | Function to check if there exist a twin pythagorean triplet in the given array ; Loop to check if there is a Pythagorean triplet in the array ; Check if there is consecutive triple ; Calculate square of array elements ; Given array arr [ ] ; Function Call
function isTriplet ( ar , n ) { for ( var i = 0 ; i < n ; i ++ ) { for ( var j = i + 1 ; j < n ; j ++ ) { for ( var k = j + 1 ; k < n ; k ++ ) { if ( Math . abs ( ar [ i ] - ar [ j ] ) == 1 || Math . abs ( ar [ j ] - ar [ k ] ) == 1 || Math . abs ( ar [ i ] - ar [ k ] ) == 1 ) { var x = ar [ i ] * ar [ i ] , y = ar [ j ] * ar [ j ] , z = ar [ k ] * ar [ k ] ; if ( x == y + z y == x + z z == x + y ) return true ; } } } } return false ; } var arr = [ 3 , 1 , 4 , 6 , 5 ] ; var ar_size = arr . length ; isTriplet ( arr , ar_size ) ? document . write ( " " ) : document . write ( " " ) ;
Pair with min absolute difference and whose product is N + 1 or N + 2 | Function to print pair ( a , b ) such that a * b = N + 1 or N + 2 ; Loop to iterate over the desired possible values ; Check for condition 1 ; Check for condition 2 ; Given Number ; Function Call
function closestDivisors ( n ) { for ( var i = parseInt ( Math . sqrt ( n + 2 ) ) ; i > 0 ; i -- ) { if ( ( n + 1 ) % i == 0 ) { document . write ( i + " " + parseInt ( ( n + 1 ) / i ) ) ; break ; } if ( ( n + 2 ) % i == 0 ) { document . write ( i + " " + parseInt ( ( n + 2 ) / i ) ) ; break ; } } } N = 123 ; closestDivisors ( N ) ;
Is it possible to reach N and M from 1 and 0 respectively as per given condition | Function that find given x and y is possible or not ; Check if x is less than 2 and y is not equal to 0 ; Perform subtraction ; Check if y is divisible by 2 and greater than equal to 0 ; Given X and Y ; Function Call
function is_possible ( x , y ) { if ( x < 2 && y != 0 ) return false ; y = y - x + 1 ; if ( y % 2 == 0 && y >= 0 ) return true ; else return false ; } let x = 5 , y = 2 ; if ( is_possible ( x , y ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count of integers up to N which are non divisors and non coprime with N | Function to return the count of letegers less than N satisfying given conditions ; Stores Euler counts ; Store Divisor counts ; Based on Sieve of Eratosthenes ; Update phi values of all multiples of i ; Update count of divisors ; Return the final count ; Driver code
function count ( n ) { let phi = [ ] ; let divs = [ ] ; for ( let i = 1 ; i <= n ; i ++ ) { phi [ i ] = 0 ; divs [ i ] = 0 ; } for ( let i = 1 ; i <= n ; i ++ ) { phi [ i ] += i ; for ( let j = i * 2 ; j <= n ; j += i ) phi [ j ] -= phi [ i ] ; for ( let j = i ; j <= n ; j += i ) divs [ j ] ++ ; } return ( n - phi [ n ] - divs [ n ] + 1 ) ; } let N = 42 ; document . write ( count ( N ) ) ;
Sum of all differences between Maximum and Minimum of increasing Subarrays | Function to calculate and return the sum of differences of maximum and minimum of strictly increasing subarrays ; Stores the sum ; Traverse the array ; If last element of the increasing sub - array is found ; Update sum ; If the last element of the array is reached ; Update sum ; Return the sum ; Driver code
function sum_of_differences ( arr , N ) { let sum = 0 ; let i , j , flag ; for ( i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] < arr [ i + 1 ] ) { flag = 0 ; for ( j = i + 1 ; j < N - 1 ; j ++ ) { if ( arr [ j ] >= arr [ j + 1 ] ) { sum += ( arr [ j ] - arr [ i ] ) ; i = j ; flag = 1 ; break ; } } if ( flag == 0 && arr [ i ] < arr [ N - 1 ] ) { sum += ( arr [ N - 1 ] - arr [ i ] ) ; break ; } } } return sum ; } let arr = [ 6 , 1 , 2 , 5 , 3 , 4 ] ; let N = arr . length ; document . write ( sum_of_differences ( arr , N ) ) ;
Construct a Maximum Binary Tree from two given Binary Trees | A binary tree node has data , pointer to left child and a pointer to right child ; Helper method that allocates a new node with the given data and NULL left and right pointers . ; Given a binary tree , print its nodes in inorder ; first recur on left child ; then print the data of node ; now recur on right child ; Method to find the maximum binary tree from two binary trees ; First Binary Tree 3 / \ 2 6 / 20 ; Second Binary Tree 5 / \ 1 8 \ \ 2 8
class Node { constructor ( data ) { this . data = data ; this . left = null ; this . right = null ; } } function newNode ( data ) { return new Node ( data , null , null ) ; } function inorder ( node ) { if ( node == null ) return ; inorder ( node . left ) ; document . write ( node . data + " " ) ; inorder ( node . right ) ; } function MaximumBinaryTree ( t1 , t2 ) { if ( t1 == null ) return t2 ; if ( t2 == null ) return t1 ; t1 . data = Math . max ( t1 . data , t2 . data ) ; t1 . left = MaximumBinaryTree ( t1 . left , t2 . left ) ; t1 . right = MaximumBinaryTree ( t1 . right , t2 . right ) ; return t1 ; } var root1 = newNode ( 3 ) ; root1 . left = newNode ( 2 ) ; root1 . right = newNode ( 6 ) ; root1 . left . left = newNode ( 20 ) ; var root2 = newNode ( 5 ) ; root2 . left = newNode ( 1 ) ; root2 . right = newNode ( 8 ) ; root2 . left . right = newNode ( 2 ) ; root2 . right . right = newNode ( 8 ) ; var root3 = MaximumBinaryTree ( root1 , root2 ) ; inorder ( root3 ) ;
Minimum size binary string required such that probability of deleting two 1 's at random is 1/X | Function returns the minimum size of the String ; From formula ; Left limit of r ; Right limit of r ; Smallest integer in the valid range ; Driver Code
function MinimumString ( x ) { let b = 1 ; let left_lim = Math . sqrt ( x ) + 1.0 ; let right_lim = Math . sqrt ( x ) + 2.0 ; let r = 0 ; for ( let i = Math . floor ( left_lim ) ; i <= Math . floor ( right_lim ) ; i ++ ) { if ( i > left_lim && i < right_lim ) { r = i ; break ; } } return b + r ; } let X = 2 ; document . write ( MinimumString ( X ) ) ;
Minimum number of squares whose sum equals to a given number N | Set | Function that returns true if N is a perfect square ; Function that returns true check if N is sum of three squares ; Factor out the powers of 4 ; N is NOT of the form 4 ^ a * ( 8 b + 7 ) ; Function that finds the minimum number of square whose sum is N ; If N is perfect square ; If N is sum of 2 perfect squares ; If N is sum of 3 perfect squares ; Otherwise , N is the sum of 4 perfect squares ; Given number ; Function call
function isPerfectSquare ( N ) { let floorSqrt = Math . floor ( Math . sqrt ( N ) ) ; return ( N == floorSqrt * floorSqrt ) ; } function legendreFunction ( N ) { while ( N % 4 == 0 ) N = Math . floor ( N / 4 ) ; if ( N % 8 != 7 ) return true ; else return false ; } function minSquares ( N ) { if ( isPerfectSquare ( N ) ) return 1 ; for ( let i = 1 ; i * i < N ; i ++ ) { if ( isPerfectSquare ( N - i * i ) ) return 2 ; } if ( legendreFunction ( N ) ) return 3 ; return 4 ; } let N = 123 ; document . write ( minSquares ( N ) ) ;
Check if N leaves only distinct remainders on division by all values up to K | Function to check and return if all remainders are distinct ; Stores the remainder ; Calculate the remainder ; If remainder already occurred ; Insert leto the set ; Driver code
function is_distinct ( n , k ) { let s = new Set ( ) ; for ( let i = 1 ; i <= k ; i ++ ) { let tmp = n % i ; if ( s . has ( tmp ) ) { return false ; } s . add ( tmp ) ; } return true ; } let N = 5 , K = 3 ; if ( is_distinct ( N , K ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count of multiplicative partitions of N | Function to return number of ways of factoring N with all factors greater than 1 ; Variable to store number of ways of factoring n with all factors greater than 1 ; Driver code ; 2 is the minimum factor of number other than 1. So calling recursive function to find number of ways of factoring N with all factors greater than 1
function getDivisors ( min , n ) { var total = 0 ; for ( var i = min ; i < n ; ++ i ) { if ( n % i == 0 && n / i >= i ) { ++ total ; if ( n / i > i ) total += getDivisors ( i , n / i ) ; } } return total ; } var n = 30 ; document . write ( 1 + getDivisors ( 2 , n ) ) ;
Find last 2 survivors in N persons standing in a circle after killing next to immediate neighbour | Node for a Linked List ; Function to find the last 2 survivors ; Total is the count of alive people ; Initiating the list of n people ; Total != 2 is terminating condition because at last only two - person will remain alive ; Del represent next person to be deleted or killed ; Last two person to survive ( in any order ) ; Driver code
class Node { constructor ( val ) { this . val = val ; this . next = null ; } } ; function getLastTwoPerson ( n ) { var total = n ; var head = new Node ( 1 ) ; var temp = head ; for ( var i = 2 ; i <= n ; i ++ ) { temp . next = new Node ( i ) ; temp = temp . next ; } temp . next = head ; temp = head ; var del ; while ( total != 2 ) { del = temp . next . next ; temp . next . next = temp . next . next . next ; temp = temp . next ; total -= 1 ; } document . write ( temp . val + " " + temp . next . val ) ; } var n = 2 ; getLastTwoPerson ( n ) ;
Philaland Coin | TCS Mockvita 2020 | Function to find the minimum number of denomminations required ; Driver Code ; Function Call
function findMinDenomin ( n ) { return ( Math . floor ( Math . log ( n ) / Math . log ( 2 ) ) + 1 ) ; } let n = 10 ; document . write ( findMinDenomin ( n ) ) ;
Content of a Polynomial | Function to find the content of the polynomial ; Loop to iterate over the elements of the array ; __gcd ( a , b ) is a inbuilt function for Greatest Common Divisor ; Driver Code ; Function call
function findContent ( arr , n ) { var content = arr [ 0 ] ; for ( var i = 1 ; i < n ; i ++ ) { content = __gcd ( content , arr [ i ] ) ; } return content ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } var n = 3 ; var arr = [ 9 , 6 , 12 ] ; document . write ( findContent ( arr , n ) ) ;
Check if the given array is same as its inverse permutation | Function to check if the inverse permutation of the given array is same as the original array ; Stores the inverse permutation ; Generate the inverse permutation ; Check if the inverse permutation is same as the given array ; Driver Code
function inverseEqual ( arr , n ) { var brr = Array ( n ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { var present_index = arr [ i ] - 1 ; brr [ present_index ] = i + 1 ; } for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != brr [ i ] ) { document . write ( " " ) ; return ; } } document . write ( " " ) ; } var n = 4 ; var arr = [ 1 , 4 , 3 , 2 ] ; inverseEqual ( arr , n ) ;
Check if the given array is same as its inverse permutation | Function to check if the inverse permutation of the given array is same as the original array ; Check the if inverse permutation is not same ; Driver Code ; Function Call
function inverseEqual ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) if ( arr [ arr [ i ] - 1 ] != i + 1 ) return false ; return true ; } let n = 4 ; let arr = [ 1 , 4 , 3 , 2 ] ; document . write ( inverseEqual ( arr , n ) ? " " : " " ) ;
Linear Congruence method for generating Pseudo Random Numbers | Function to generate random numbers ; Initialize the seed state ; Traverse to generate required numbers of random numbers ; Follow the linear congruential method ; Seed value ; Modulus parameter ; Multiplier term ; Increment term ; Number of Random numbers to be generated ; To store random numbers ; Function Call ; Print the generated random numbers
function linearCongruentialMethod ( Xo , m , a , c , randomNums , noOfRandomNums ) { randomNums [ 0 ] = Xo ; for ( let i = 1 ; i < noOfRandomNums ; i ++ ) { randomNums [ i ] = ( ( randomNums [ i - 1 ] * a ) + c ) % m ; } } let Xo = 5 ; let m = 7 ; let a = 3 ; let c = 3 ; let noOfRandomNums = 10 ; let randomNums = new Array ( noOfRandomNums ) . fill ( 0 ) ; linearCongruentialMethod ( Xo , m , a , c , randomNums , noOfRandomNums ) ; for ( let i = 0 ; i < noOfRandomNums ; i ++ ) { document . write ( randomNums [ i ] + " " ) ; }
Multiplicative Congruence method for generating Pseudo Random Numbers | Function to generate random numbers ; Initialize the seed state ; Traverse to generate required numbers of random numbers ; Follow the multiplicative congruential method ; Seed value ; Modulus parameter ; Multiplier term ; Number of Random numbers to be generated ; To store random numbers ; Function Call ; Prlet the generated random numbers
function multiplicativeCongruentialMethod ( Xo , m , a , randomNums , noOfRandomNums ) { randomNums [ 0 ] = Xo ; for ( let i = 1 ; i < noOfRandomNums ; i ++ ) { randomNums [ i ] = ( randomNums [ i - 1 ] * a ) % m ; } } let Xo = 3 ; let m = 15 ; let a = 7 ; let noOfRandomNums = 10 ; let randomNums = new Array ( noOfRandomNums ) . fill ( 0 ) ; multiplicativeCongruentialMethod ( Xo , m , a , randomNums , noOfRandomNums ) ; for ( let i = 0 ; i < noOfRandomNums ; i ++ ) { document . write ( randomNums [ i ] + " " ) ; }
Product of divisors of a number from a given list of its prime factors | Javascript Program to implement the above approach ; Function to calculate ( a ^ b ) % m ; Function to calculate and return the product of divisors ; Stores the frequencies of prime divisors ; Iterate over the prime divisors ; Update the product ; Update the count of divisors ; Driver Code
var MOD = 1000000007 ; function power ( a , b , m ) { a %= m ; var res = 1 ; while ( b > 0 ) { if ( b & 1 ) res = ( ( res % m ) * ( a % m ) ) % m ; a = ( ( a % m ) * ( a % m ) ) % m ; b >>= 1 ; } return res % m ; } function productOfDivisors ( p , n ) { var prime = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( prime . has ( p [ i ] ) ) prime . set ( p [ i ] , prime . get ( p [ i ] ) + 1 ) else prime . set ( p [ i ] , 1 ) } var product = 1 , d = 1 ; prime . forEach ( ( value , key ) => { var val = power ( key , ( value ) * ( value + 1 ) / 2 , MOD ) ; product = ( power ( product , value + 1 , MOD ) * power ( val , d , MOD ) ) % MOD ; d = ( d * ( value + 1 ) ) % ( MOD - 1 ) ; } ) ; return product ; } var arr = [ 11 , 11 ] ; var n = arr . length ; document . write ( productOfDivisors ( arr , n ) ) ;