codeparrot/xlcost-text-to-code huggingface dataset:
Convert from any base to decimal and vice versa | To return value of a char . For example , 2 is returned for '2' . 10 is returned for ' A ' , 11 for ' B ' ; Function to convert a number from given base ' b ' to decimal ; Initialize power of base ; Initialize result ; Decimal equivalent is str [ len - 1 ] * 1 + str [ len - 2 ] * base + str [ len - 3 ] * ( base ^ 2 ) + ... ; A digit in input number must be less than number 's base ; Driver code
function val ( c ) { if ( c >= ' ' . charCodeAt ( ) && c <= ' ' . charCodeAt ( ) ) return ( c - ' ' . charCodeAt ( ) ) ; else return ( c - ' ' . charCodeAt ( ) + 10 ) ; } function toDeci ( str , b_ase ) { let len = str . length ; let power = 1 ; let num = 0 ; let i ; for ( i = len - 1 ; i >= 0 ; i -- ) { if ( val ( str [ i ] . charCodeAt ( ) ) >= b_ase ) { document . write ( " " ) ; return - 1 ; } num += val ( str [ i ] . charCodeAt ( ) ) * power ; power = power * b_ase ; } return num ; } let str = " " ; let b_ase = 16 ; document . write ( " " + str + " " + b_ase + " " + toDeci ( str , b_ase ) ) ;
Gray to Binary and Binary to Gray conversion | Helper function to xor two characters ; Helper function to flip the bit ; Function to convert binary string to gray string ; MSB of gray code is same as binary code ; Compute remaining bits , next bit is computed by doing XOR of previous and current in Binary ; Concatenate XOR of previous bit with current bit ; Function to convert gray code string to binary string ; MSB of binary code is same as gray code ; Compute remaining bits ; If current bit is 0 , concatenate previous bit ; Else , concatenate invert of previous bit ; Driver code
function xor_c ( a , b ) { return ( a == b ) ? ' ' : ' ' ; } function flip ( c ) { return ( c == ' ' ) ? ' ' : ' ' ; } function binarytoGray ( binary ) { let gray = " " ; gray += binary [ 0 ] ; for ( let i = 1 ; i < binary . length ; i ++ ) { gray += xor_c ( binary [ i - 1 ] , binary [ i ] ) ; } return gray ; } function graytoBinary ( gray ) { let binary = " " ; binary += gray [ 0 ] ; for ( let i = 1 ; i < gray . length ; i ++ ) { if ( gray [ i ] == ' ' ) binary += binary [ i - 1 ] ; else binary += flip ( binary [ i - 1 ] ) ; } return binary ; } let binary = " " ; document . write ( " " + binary + " " + binarytoGray ( binary ) + " " ) ; let gray = " " ; document . write ( " " + gray + " " + graytoBinary ( gray ) ) ;
Solving f ( n ) = ( 1 ) + ( 2 * 3 ) + ( 4 * 5 * 6 ) . . . n using Recursion | * * Recursive method for finding * sum of series * * @ param calculated number of terms * till which sum of terms has been * calculated @ param current number of * terms for which sum has to be calculated . * @ param N Number of terms in the function * to be calculated @ return sum ; checking termination condition ; product of terms till current ; recursive call for adding terms next in the series ; input number of terms in the series ; invoking the method to calculate the sum
function seriesSum ( calculated , current , N ) { let i , cur = 1 ; if ( current == N + 1 ) return 0 ; for ( i = calculated ; i < calculated + current ; i ++ ) cur *= i ; return cur + seriesSum ( i , current + 1 , N ) ; } let N = 5 ; document . write ( seriesSum ( 1 , 1 , N ) ) ;
Count inversions in an array | Set 3 ( Using BIT ) | Returns sum of arr [ 0. . index ] . This function assumes that the array is preprocessed and partial sums of array elements are stored in BITree [ ] . ; Initialize result ; Traverse ancestors of BITree [ index ] ; Add current element of BITree to sum ; Move index to parent node in getSum View ; Updates a node in Binary Index Tree ( BITree ) at given index in BITree . The given value ' val ' is added to BITree [ i ] and all of its ancestors in tree . ; Traverse all ancestors and add ' val ' ; Add ' val ' to current node of BI Tree ; Update index to that of parent in update View ; Converts an array to an array with values from 1 to n and relative order of smaller and greater elements remains same . For example , { 7 , - 90 , 100 , 1 } is converted to { 3 , 1 , 4 , 2 } ; Create a copy of arrp [ ] in temp and sort the temp array in increasing order ; Traverse all array elements ; lower_bound ( ) Returns pointer to the first element greater than or equal to arr [ i ] ; Returns inversion count arr [ 0. . n - 1 ] ; Initialize result ; Convert arr [ ] to an array with values from 1 to n and relative order of smaller and greater elements remains same . For example , { 7 , - 90 , 100 , 1 } is converted to { 3 , 1 , 4 , 2 } ; Create a BIT with size equal to maxElement + 1 ( Extra one is used so that elements can be directly be used as index ) ; Traverse all elements from right . ; Get count of elements smaller than arr [ i ] ; Add current element to BIT ; Driver code
function getSum ( BITree , index ) { let sum = 0 ; while ( index > 0 ) { sum += BITree [ index ] ; index -= index & ( - index ) ; } return sum ; } function updateBIT ( BITree , n , index , val ) { while ( index <= n ) { BITree [ index ] += val ; index += index & ( - index ) ; } } function convert ( arr , n ) { let temp = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) temp [ i ] = arr [ i ] ; temp . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < n ; i ++ ) { arr [ i ] = lower_bound ( temp , 0 , n , arr [ i ] ) + 1 ; } } function lower_bound ( a , low , high , element ) { while ( low < high ) { let middle = low + Math . floor ( ( high - low ) / 2 ) ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } function getInvCount ( arr , n ) { let invcount = 0 ; convert ( arr , n ) ; let BIT = new Array ( n + 1 ) ; for ( let i = 1 ; i <= n ; i ++ ) BIT [ i ] = 0 ; for ( let i = n - 1 ; i >= 0 ; i -- ) { invcount += getSum ( BIT , arr [ i ] - 1 ) ; updateBIT ( BIT , n , arr [ i ] , 1 ) ; } return invcount ; } let arr = [ 8 , 4 , 2 , 1 ] ; let n = arr . length ; document . write ( " " + getInvCount ( arr , n ) ) ;
Compute n ! under modulo p | Returns value of n ! % p ; Driver Code
function modFact ( n , p ) { if ( n >= p ) return 0 ; let result = 1 ; for ( let i = 1 ; i <= n ; i ++ ) result = ( result * i ) % p ; return result ; } let n = 25 , p = 29 ; document . write ( modFact ( n , p ) ) ;
Chinese Remainder Theorem | Set 2 ( Inverse Modulo based Implementation ) | Returns modulo inverse of a with respect to m using extended Euclid Algorithm . Refer below post for details : https : www . geeksforgeeks . org / multiplicative - inverse - under - modulo - m / ; Apply extended Euclid Algorithm ; q is quotient ; m is remainder now , process same as euclid 's algo ; Make x1 positive ; k is size of num [ ] and rem [ ] . Returns the smallest number x such that : x % num [ 0 ] = rem [ 0 ] , x % num [ 1 ] = rem [ 1 ] , ... ... ... ... ... ... x % num [ k - 2 ] = rem [ k - 1 ] Assumption : Numbers in num [ ] are pairwise coprime ( gcd forevery pair is 1 ) ; Compute product of all numbers ; Initialize result ; Apply above formula ; Driver Code
function inv ( a , m ) { let m0 = m ; let x0 = 0 ; let x1 = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { let q = parseInt ( a / m ) ; let t = m ; m = a % m ; a = t ; t = x0 ; x0 = x1 - q * x0 ; x1 = t ; } if ( x1 < 0 ) x1 += m0 ; return x1 ; } function findMinX ( num , rem , k ) { let prod = 1 ; for ( let i = 0 ; i < k ; i ++ ) prod *= num [ i ] ; let result = 0 ; for ( let i = 0 ; i < k ; i ++ ) { pp = parseInt ( prod / num [ i ] ) ; result += rem [ i ] * inv ( pp , num [ i ] ) * pp ; } return result % prod ; } let num = new Array ( 3 , 4 , 5 ) ; let rem = new Array ( 2 , 3 , 1 ) ; let k = num . length ; document . write ( " " + findMinX ( num , rem , k ) ) ;
Chinese Remainder Theorem | Set 1 ( Introduction ) | k is size of num and rem . Returns the smallest number x such that : x % num [ 0 ] = rem [ 0 ] , x % num [ 1 ] = rem [ 1 ] , ... ... ... ... ... ... x % num [ k - 2 ] = rem [ k - 1 ] Assumption : Numbers in num are pairwise coprime ( gcd for every pair is 1 ) ; As per the Chinese remainder theorem , this loop will always break . ; Check if remainder of x % num [ j ] is rem [ j ] or not ( for all j from 0 to k - 1 ) ; If all remainders matched , we found x ; Else try next number ; Driver method
function findMinX ( num , rem , k ) { while ( true ) { var j ; for ( j = 0 ; j < k ; j ++ ) if ( x % num [ j ] != rem [ j ] ) break ; if ( j == k ) return x ; x ++ ; } } var num = [ 3 , 4 , 5 ] ; var rem = [ 2 , 3 , 1 ] ; var k = num . length ; document . write ( " " + findMinX ( num , rem , k ) ) ;
Compute nCr % p | Set 2 ( Lucas Theorem ) | Returns nCr % p . In this Lucas Theorem based program , this function is only called for n < p and r < p . ; The array C is going to store last row of pascal triangle at the end . And last entry of last row is nCr ; One by constructs remaining rows of Pascal Triangle from top to bottom ; Fill entries of current row using previous row values ; nCj = ( n - 1 ) Cj + ( n - 1 ) C ( j - 1 ) ; ; Lucas Theorem based function that returns nCr % p This function works like decimal to binary conversion recursive function . First we compute last digits of n and r in base p , then recur for remaining digits ; Base case ; Compute last digits of n and r in base p ; Compute result for last digits computed above , and for remaining digits . Multiply the two results and compute the result of multiplication in modulo p . return ( nCrModpLucas ( parseInt ( n / p ) , parseInt ( r / p ) , p ) * Last digits of n and r nCrModpDP ( ni , ri , p ) ) % p ; Remaining digits ; Driver program
function nCrModpDP ( n , r , p ) { C = Array ( r + 1 ) . fill ( 0 ) ; for ( var i = 1 ; i <= n ; i ++ ) { for ( var j = Math . min ( i , r ) ; j > 0 ; j -- ) C [ j ] = ( C [ j ] + C [ j - 1 ] ) % p ; } return C [ r ] ; } function nCrModpLucas ( n , r , p ) { if ( r == 0 ) return 1 ; var ni = n % p , ri = r % p ; } var n = 1000 , r = 900 , p = 13 ; document . write ( " " + nCrModpLucas ( n , r , p ) ) ;
Fibonacci Coding | To limit on the largest Fibonacci number to be used ; Array to store fibonacci numbers . fib [ i ] is going to store ( i + 2 ) 'th Fibonacci number ; Stores values in fib and returns index of the largest fibonacci number smaller than n . ; Fib [ 0 ] stores 2 nd Fibonacci No . ; Fib [ 1 ] stores 3 rd Fibonacci No . ; Keep Generating remaining numbers while previously generated number is smaller ; Return index of the largest fibonacci number smaller than or equal to n . Note that the above loop stopped when fib [ i - 1 ] became larger . ; Returns pointer to the char string which corresponds to code for n ; Allocate memory for codeword ; Index of the largest Fibonacci f <= n ; Mark usage of Fibonacci f ( 1 bit ) ; Subtract f from n ; Move to Fibonacci just smaller than f ; Mark all Fibonacci > n as not used ( 0 bit ) , progress backwards ; Additional '1' bit ; Return pointer to codeword ; Driver code
let N = 30 ; let fib = new Array ( N ) ; function largestFiboLessOrEqual ( n ) { fib [ 0 ] = 1 ; fib [ 1 ] = 2 ; let i ; for ( i = 2 ; fib [ i - 1 ] <= n ; i ++ ) { fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; } return ( i - 2 ) ; } function fibonacciEncoding ( n ) { let index = largestFiboLessOrEqual ( n ) ; let codeword = new Array ( index + 3 ) ; let i = index ; while ( n > 0 ) { codeword [ i ] = ' ' ; n = n - fib [ i ] ; i = i - 1 ; while ( i >= 0 && fib [ i ] > n ) { codeword [ i ] = ' ' ; i = i - 1 ; } } codeword [ index + 1 ] = ' ' ; codeword [ index + 2 ] = ' \0 ' ; let string = ( codeword ) . join ( " " ) ; return string ; } let n = 143 ; document . write ( " " + n + " " + fibonacciEncoding ( n ) ) ;
Print all Good numbers in given range | Function to check whether n is a good number and doesn ' t ▁ contain ▁ digit ▁ ' d ' ; Get last digit and initialize sum from right side ; If last digit is d , return ; Traverse remaining digits ; Current digit ; If digit is d or digit is less than or equal to sum of digits on right side ; Update sum and n ; Print Good numbers in range [ L , R ] ; Traverse all numbers in given range ; If current numbers is good , prvar it ; Driver code ; Print good numbers in [ L , R ]
function isValid ( n , d ) { var digit = n % 10 ; var sum = digit ; if ( digit == d ) return false ; n = parseInt ( n / 10 ) ; while ( n > 0 ) { digit = n % 10 ; if ( digit == d digit <= sum ) return false ; else { sum += digit ; n = parseInt ( n / 10 ) ; } } return true ; } function printGoodNumber ( L , R , d ) { for ( i = L ; i <= R ; i ++ ) { if ( isValid ( i , d ) ) document . write ( i + " " ) ; } } var L = 410 , R = 520 , d = 3 ; printGoodNumber ( L , R , d ) ;
Count number of squares in a rectangle | Returns count of all squares in a rectangle of size m x n ; If n is smaller , swap m and n ; Now n is greater dimension , apply formula ; Driver Code
function countSquares ( m , n ) { if ( n < m ) { var temp = m ; m = n ; n = temp ; } return n * ( n + 1 ) * ( 3 * m - n + 1 ) / 6 ; } var m = 4 ; var n = 3 ; document . write ( " " + countSquares ( m , n ) ) ;
Zeckendorf 's Theorem (Non | Returns the greatest Fibonacci Number smaller than or equal to n . ; Corner cases ; Find the greatest Fibonacci Number smaller than n . ; Prints Fibonacci Representation of n using greedy algorithm ; Find the greates Fibonacci Number smaller than or equal to n ; Print the found fibonacci number ; Reduce n ; Driver Code
function nearestSmallerEqFib ( n ) { if ( n == 0 n == 1 ) return n ; let f1 = 0 ; let f2 = 1 ; let f3 = 1 ; while ( f3 <= n ) { f1 = f2 ; f2 = f3 ; f3 = f1 + f2 ; } return f2 ; } function printFibRepresntation ( n ) { while ( n > 0 ) { let f = nearestSmallerEqFib ( n ) ; document . write ( f , " " ) ; n = n - f ; } } let n = 30 ; document . write ( " " + n + " " ) ; printFibRepresntation ( n ) ;
Count number of ways to divide a number in 4 parts | A Dynamic Programming based solution to count number of ways to represent n as sum of four numbers ; " parts " is number of parts left , n is the value left " nextPart " is starting point from where we start trying for next part . ; Base cases ; If this subproblem is already solved ; Count number of ways for remaining number n - i remaining parts " parts - 1" , and for all part varying from ' nextPart ' to ' n ' ; Store computed answer in table and return result ; This function mainly initializes dp table and calls countWaysUtil ( ) ; Driver program
let dp = new Array ( 5001 ) ; for ( let i = 0 ; i < 5001 ; i ++ ) { dp [ i ] = new Array ( 5001 ) ; for ( let j = 0 ; j < 5001 ; j ++ ) { dp [ i ] [ j ] = new Array ( 5 ) ; } } function countWaysUtil ( n , parts , nextPart ) { if ( parts == 0 && n == 0 ) return 1 ; if ( n <= 0 parts <= 0 ) return 0 ; if ( dp [ n ] [ nextPart ] [ parts ] != - 1 ) return dp [ n ] [ nextPart ] [ parts ] ; for ( let i = nextPart ; i <= n ; i ++ ) ans += countWaysUtil ( n - i , parts - 1 , i ) ; return ( dp [ n ] [ nextPart ] [ parts ] = ans ) ; } function countWays ( n ) { for ( let i = 0 ; i < 5001 ; i ++ ) { for ( let j = 0 ; j < 5001 ; j ++ ) { for ( let l = 0 ; l < 5 ; l ++ ) dp [ i ] [ j ] [ l ] = - 1 ; } } return countWaysUtil ( n , 4 , 1 ) ; } let n = 8 ; document . write ( countWays ( n ) ) ;
Segmented Sieve | This functions finds all primes smaller than ' limit ' using simple sieve of eratosthenes . ; Create a boolean array " mark [ 0 . . limit - 1 ] " and initialize all entries of it as true . A value in mark [ p ] will finally be false if ' p ' is Not a prime , else true . ; One by one traverse all numbers so that their multiples can be marked as composite . ; If p is not changed , then it is a prime ; Update all multiples of p ; Print all prime numbers and store them in prime
function simpleSieve ( limit ) { var mark = Array ( limit ) . fill ( true ) ; for ( p = 2 ; p * p < limit ; p ++ ) { if ( mark [ p ] == true ) { for ( i = p * p ; i < limit ; i += p ) mark [ i ] = false ; } } for ( p = 2 ; p < limit ; p ++ ) if ( mark [ p ] == true ) document . write ( p + " " ) ; }
Find the smallest twins in given range | Javascript program to find the smallest twin in given range ; Create a boolean array " prime [ 0 . . high ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Look for the smallest twin ; If p is not marked , then it is a prime ; Update all multiples of p ; Now prvar the smallest twin in range ; Driver code
function printTwins ( low , high ) { var prime = Array . from ( { length : high + 1 } , ( _ , i ) => 0 ) ; var twin = false ; for ( i = 0 ; i < prime . length ; i ++ ) { prime [ i ] = true ; } prime [ 0 ] = prime [ 1 ] = false ; for ( p = 2 ; p <= Math . floor ( Math . sqrt ( high ) ) + 1 ; p ++ ) { if ( prime [ p ] ) { for ( i = p * 2 ; i <= high ; i += p ) { prime [ i ] = false ; } } } for ( i = low ; i <= high ; i ++ ) { if ( prime [ i ] && prime [ i + 2 ] ) { var a = i + 2 ; document . write ( " " + " " + i + " " + a + " " ) ; twin = true ; break ; } } if ( twin == false ) { document . write ( " " ) ; } } printTwins ( 10 , 100 ) ;
Check if a given number is Fancy | javascript program to find if a given number is fancy or not ; To store mappings of fancy pair characters . For example 6 is paired with 9 and 9 is paired with 6. ; Find number of digits in given number ; Traverse from both ends , and compare characters one by one ; If current characters at both ends are not fancy pairs ; Driver Code
function isFancy ( num ) { var fp = new Map ( ) ; fp . set ( ' ' , ' ' ) ; fp . set ( ' ' , ' ' ) ; fp . set ( ' ' , ' ' ) ; fp . set ( ' ' , ' ' ) ; fp . set ( ' ' , ' ' ) ; var n = num . length ; var l = 0 , r = n - 1 ; while ( l <= r ) { if ( ! fp . has ( num . charAt ( l ) ) || fp . get ( num . charAt ( l ) ) != num . charAt ( r ) ) return false ; l ++ ; r -- ; } return true ; } var str = " " ; if ( isFancy ( str ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find Next Sparse Number | Javascript program to find next sparse number ; Find binary representation of x and store it in bin [ ] . bin [ 0 ] contains least significant bit ( LSB ) , next bit is in bin [ 1 ] , and so on . ; There my be extra bit in result , so add one extra bit ; Size of binary representation ; The position till which all bits are finalized ; Start from second bit ( next to LSB ) ; If current bit and its previous bit are 1 , but next bit is not 1. ; Make the next bit 1 ; Make all bits before current bit as 0 to make sure that we get the smallest next number ; Store position of the bit set so that this bit and bits before it are not changed next time . ; Find decimal equivalent of modified bin [ ] ; Driver Code
function nextSparse ( x ) { let bin = new Array ( ) ; while ( x != 0 ) { bin . push ( x & 1 ) ; x >>= 1 ; } bin . push ( 0 ) ; n = bin . length ; let last_final = 0 ; for ( let i = 1 ; i < n - 1 ; i ++ ) { if ( bin [ i ] == 1 && bin [ i - 1 ] == 1 && bin [ i + 1 ] != 1 ) { bin [ i + 1 ] = 1 ; for ( let j = i ; j >= last_final ; j -- ) bin [ j ] = 0 ; last_final = i + 1 ; } } let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) ans += bin [ i ] * ( 1 << i ) ; return ans ; } let x = 38 ; document . write ( " " + nextSparse ( x ) ) ;
Sum of bit differences among all pairs | Javascript program to compute sum of pairwise bit differences ; Initialize result ; Traverse over all bits ; count number of elements with i 'th bit set ; Add " count ▁ * ▁ ( n ▁ - ▁ count ) ▁ * ▁ 2" to the answer ; Driver code
function sumBitDifferences ( arr , n ) { let ans = 0 ; for ( let i = 0 ; i < 32 ; i ++ ) { let count = 0 ; for ( let j = 0 ; j < n ; j ++ ) if ( ( arr [ j ] & ( 1 << i ) ) ) count ++ ; ans += ( count * ( n - count ) * 2 ) ; } return ans ; } let arr = [ 1 , 3 , 5 ] ; let n = arr . length ; document . write ( sumBitDifferences ( arr , n ) ) ;
Print all non | Utility function to print array arr [ 0. . n - 1 ] ; Recursive Function to generate all non - increasing sequences with sum x arr [ ] -- > Elements of current sequence curr_sum -- > Current Sum curr_idx -- > Current index in arr [ ] ; If current sum is equal to x , then we found a sequence ; Try placing all numbers from 1 to x - curr_sum at current index ; The placed number must also be smaller than previously placed numbers and it may be equal to the previous stored value , i . e . , arr [ curr_idx - 1 ] if there exists a previous number ; Place number at curr_idx ; Recur ; Try next number ; A wrapper over generateUtil ( ) ; Array to store sequences on by one ; Driver code
function printArr ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ; } function generateUtil ( x , arr , curr_sum , curr_idx ) { if ( curr_sum == x ) { printArr ( arr , curr_idx ) ; return ; } let num = 1 ; while ( num <= x - curr_sum && ( curr_idx == 0 num <= arr [ curr_idx - 1 ] ) ) { arr [ curr_idx ] = num ; generateUtil ( x , arr , curr_sum + num , curr_idx + 1 ) ; num ++ ; } } function generate ( x ) { let arr = new Array ( x ) ; generateUtil ( x , arr , 0 , 0 ) ; } let x = 5 ; generate ( x ) ;
Perfect Number | Returns true if n is perfect ; To store sum of divisors ; Find all divisors and add them ; If sum of divisors is equal to n , then n is a perfect number ; Driver program
function isPerfect ( n ) { sum = 1 ; for ( let i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( i * i != n ) sum = sum + i + n / i ; else sum = sum + i ; } } if ( sum == n && n != 1 ) return true ; return false ; } document . write ( " " + " " ) ; for ( let n = 2 ; n < 10000 ; n ++ ) if ( isPerfect ( n ) ) document . write ( n + " " + " " ) ;
Check if a given number can be represented in given a no . of digits in any base | Returns true if ' num ' can be represented usind ' dig ' digits in ' base ' ; Base case ; If there are more than 1 digits left and number is more than base , then remove last digit by doing num / base , reduce the number of digits and recur ; return true of num can be represented in ' dig ' digits in any base from 2 to 32 ; Check for all bases one by one ; Driver code
function checkUtil ( num , dig , base ) { if ( dig == 1 && num < base ) return true ; if ( dig > 1 && num >= base ) return checkUtil ( parseInt ( num / base ) , -- dig , base ) ; return false ; } function check ( num , dig ) { for ( base = 2 ; base <= 32 ; base ++ ) if ( checkUtil ( num , dig , base ) ) return true ; return false ; } var num = 8 ; var dig = 3 ; if ( check ( num , dig ) ) document . write ( " " ) ; else document . write ( " " ) ;
How to compute mod of a big number ? | Function to compute num ( mod a ) ; Initialize result ; One by one process all digits of ' num ' ; Driver Code
function mod ( num , a ) { let res = 0 ; for ( let i = 0 ; i < num . length ; i ++ ) res = ( res * 10 + num [ i ] - ' ' ) % a ; return res ; } let num = " " ; document . write ( mod ( num , 10 ) ) ;
Modular multiplicative inverse | A naive method to find modulor multiplicative inverse of ' a ' under modulo ' m ' ; Driver Code ; Function call
function modInverse ( a , m ) { for ( let x = 1 ; x < m ; x ++ ) if ( ( ( a % m ) * ( x % m ) ) % m == 1 ) return x ; } let a = 3 ; let m = 11 ; document . write ( modInverse ( a , m ) ) ;
Modular multiplicative inverse | Returns modulo inverse of a with respect to m using extended Euclid Algorithm Assumption : a and m are coprimes , i . e . , gcd ( a , m ) = 1 ; q is quotient ; m is remainder now , process same as Euclid 's algo ; Update y and x ; Make x positive ; Driver Code ; Function call
function modInverse ( a , m ) { let m0 = m ; let y = 0 ; let x = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { let q = parseInt ( a / m ) ; let t = m ; m = a % m ; a = t ; t = y ; y = x - q * y ; x = t ; } if ( x < 0 ) x += m0 ; return x ; } let a = 3 ; let m = 11 ; document . write ( ` ${ modInverse ( a , m ) } ` ) ;
Euler 's Totient Function | Function to return gcd of a and b ; A simple method to evaluate Euler Totient Function ; Driver Code
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function phi ( n ) { let result = 1 ; for ( let i = 2 ; i < n ; i ++ ) if ( gcd ( i , n ) == 1 ) result ++ ; return result ; } for ( let n = 1 ; n <= 10 ; n ++ ) document . write ( ` ${ n } ${ phi ( n ) } ` ) ;