codeparrot/xlcost-text-to-code huggingface dataset:
Count all perfect divisors of a number | Utility function to check perfect square number ; Returns count all perfect divisors of n ; Initialize result ; Consider every number that can be a divisor of n ; If i is a divisor ; Driver Code
function isPerfectSquare ( n ) { let sq = Math . sqrt ( n ) ; return ( n == sq * sq ) ; } function countPerfectDivisors ( n ) { let count = 0 ; for ( let i = 1 ; i * i <= n ; ++ i ) { if ( n % i == 0 ) { if ( isPerfectSquare ( i ) ) ++ count ; if ( n / i != i && isPerfectSquare ( n / i ) ) ++ count ; } } return count ; } let n = 16 ; document . write ( " " + " " + n ) ; document . write ( " " + countPerfectDivisors ( n ) + " " ) ; n = 12 ; document . write ( " " + " " + n ) ; document . write ( " " + countPerfectDivisors ( n ) ) ;
Nearest element with at | Javascript program to print nearest element with at least one common prime factor . ; Loop covers the every element of arr [ ] ; Loop that covers from 0 to i - 1 and i + 1 to n - 1 indexes simultaneously ; print position of closest element ; Recursive function to return gcd of a and b ; Driver Code
function nearestGcd ( arr , n ) { for ( let i = 0 ; i < n ; ++ i ) { let closest = - 1 ; for ( let j = i - 1 , k = i + 1 ; j > 0 k <= n ; -- j , ++ k ) { if ( j >= 0 && __gcd ( arr [ i ] , arr [ j ] ) > 1 ) { closest = j + 1 ; break ; } if ( k < n && __gcd ( arr [ i ] , arr [ k ] ) > 1 ) { closest = k + 1 ; break ; } } document . write ( closest + " " ) ; } } function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } let arr = [ 2 , 9 , 4 , 3 , 13 ] ; let n = arr . length ; nearestGcd ( arr , n ) ;
Largest subsequence having GCD greater than 1 | Efficient Javascript program to find length of the largest subsequence with GCD greater than 1. ; prime [ ] for storing smallest prime divisor of element count [ ] for storing the number of times a particular divisor occurs in a subsequence ; Simple sieve to find smallest prime factors of numbers smaller than MAX ; Prime number will have same divisor ; Returns length of the largest subsequence with GCD more than 1. ; Fetch total unique prime divisor of element ; Increment count [ ] of Every unique divisor we get till now ; Find maximum frequency of divisor ; Pre - compute smallest divisor of all numbers
let MAX = 100001 ; let prime = new Array ( MAX + 1 ) ; let countdiv = new Array ( MAX + 1 ) ; for ( let i = 0 ; i < MAX + 1 ; i ++ ) { prime [ i ] = 0 ; countdiv [ i ] = 0 ; } function SieveOfEratosthenes ( ) { for ( let i = 2 ; i * i <= MAX ; ++ i ) { if ( prime [ i ] == 0 ) for ( let j = i * 2 ; j <= MAX ; j += i ) prime [ j ] = i ; } for ( let i = 1 ; i < MAX ; ++ i ) if ( prime [ i ] == 0 ) prime [ i ] = i ; } function largestGCDSubsequence ( arr , n ) { let ans = 0 ; for ( let i = 0 ; i < n ; ++ i ) { let element = arr [ i ] ; while ( element > 1 ) { let div = prime [ element ] ; ++ countdiv [ div ] ; ans = Math . max ( ans , countdiv [ div ] ) ; while ( element % div == 0 ) element /= div ; } } return ans ; } SieveOfEratosthenes ( ) ; let arr = [ 10 , 15 , 7 , 25 , 9 , 35 ] ; let size = arr . length ; document . write ( largestGCDSubsequence ( arr , size ) ) ;
Count of Binary Digit numbers smaller than N | method returns count of binary digit numbers smaller than N ; queue to store all intermediate binary digit numbers ; binary digits start with 1 ; loop until we have element in queue ; push next binary digit numbers only if current popped element is N ; uncomment below line to print actual number in sorted order ; Driver code
function countOfBinaryNumberLessThanN ( N ) { var q = [ ] ; q . push ( 1 ) ; var cnt = 0 ; var t ; while ( q . length > 0 ) { t = q . pop ( ) ; if ( t <= N ) { cnt ++ ; q . push ( t * 10 ) ; q . push ( t * 10 + 1 ) ; } } return cnt ; } var N = 200 ; document . write ( countOfBinaryNumberLessThanN ( N ) + " " ) ;
Sum of product of x and y such that floor ( n / x ) = y | Return the sum of product x * y . ; Iterating x from 1 to n ; Finding y = n / x . ; Adding product of x and y to answer . ; Driver Code
function sumofproduct ( n ) { var ans = 0 ; for ( x = 1 ; x <= n ; x ++ ) { var y = parseInt ( n / x ) ; ans += ( y * x ) ; } return ans ; } var n = 10 ; document . write ( sumofproduct ( n ) ) ;
Program for Perrin numbers | Optimized Javascript program for n 'th perrin number ; Driver code
function per ( n ) { let a = 3 ; let b = 0 ; let c = 2 ; let i ; let m ; if ( n == 0 ) return a ; if ( n == 1 ) return b ; if ( n == 2 ) return c ; while ( n > 2 ) { m = a + b ; a = b ; b = c ; c = m ; n -- ; } return m ; } n = 9 ; document . write ( per ( n ) ) ;
Find the first natural number whose factorial is divisible by x | function for calculating factorial ; function for check Special_Factorial_Number ; call fact function and the Modulo with k and check if condition is TRUE then return i ; taking input
function fact ( n ) { let num = 1 ; for ( let i = 1 ; i <= n ; i ++ ) { num = num * i ; } return num ; } function Special_Factorial_Number ( k ) { for ( let i = 1 ; i <= k ; i ++ ) { if ( fact ( i ) % k == 0 ) { return i ; } } return 0 ; } let k = 16 ; document . write ( Special_Factorial_Number ( k ) ) ;
Program for Chocolate and Wrapper Puzzle | Returns maximum number of chocolates we can eat with given money , price of chocolate and number of wrapprices required to get a chocolate . ; Corner case ; First find number of chocolates that can be purchased with the given amount ; Now just add number of chocolates with the chocolates gained by wrapprices ; Total money ; Cost of each candy ; No of wrappers needs to be ; Exchanged for one chocolate .
function countMaxChoco ( money , price , wrap ) { if ( money < price ) return 0 ; let choc = parseInt ( money / price , 10 ) ; choc = choc + parseInt ( ( choc - 1 ) / ( wrap - 1 ) , 10 ) ; return choc ; } let money = 15 ; let price = 1 ; let wrap = 3 ; document . write ( countMaxChoco ( money , price , wrap ) ) ;
Check if possible to move from given coordinate to desired coordinate | javascript program to check if it is possible to reach ( a , b ) from ( x , y ) . Returns GCD of i and j ; Returns true if it is possible to go to ( a , b ) from ( x , y ) ; Find absolute values of all as sign doesn 't matter. ; If gcd is equal then it is possible to reach . Else not possible . ; Converting coordinate into positive integer
function gcd ( i , j ) { if ( i == j ) return i ; if ( i > j ) return gcd ( i - j , j ) ; return gcd ( i , j - i ) ; } function ispossible ( x , y , a , b ) { x = Math . abs ( x ) ; y = Math . abs ( y ) ; a = Math . abs ( a ) ; b = Math . abs ( b ) ; return ( gcd ( x , y ) == gcd ( a , b ) ) ; } var x = 35 , y = 15 ; var a = 20 , b = 25 ; if ( ispossible ( x , y , a , b ) ) document . write ( " " ) ; else document . write ( " " ) ;
Maximum sum of distinct numbers with LCM as N | Method returns maximum sum f distinct number whose LCM is N ; find all divisors which divides ' N ' ; if ' i ' is divisor of ' N ' ; if both divisors are same then add it only once else add both ; Driver code to test above methods
function getMaximumSumWithLCMN ( N ) { let sum = 0 ; let LIM = Math . sqrt ( N ) ; for ( let i = 1 ; i <= LIM ; i ++ ) { if ( N % i == 0 ) { if ( i == ( N / i ) ) sum += i ; else sum += ( i + N / i ) ; } } return sum ; } let N = 12 ; document . write ( getMaximumSumWithLCMN ( N ) + " " ) ;
LCM of First n Natural Numbers | to calculate hcf ; to calculate lcm ; lcm ( a , b ) = ( a * b ) / hcf ( a , b ) ; assign a = lcm of n , n - 1 ; b = b - 1 ; base case ; Function call pass n , n - 1 in function to find LCM of first n natural number
function hcf ( a , b ) { if ( b == 0 ) return a ; return hcf ( b , a % b ) ; } function findlcm ( a , b ) { if ( b == 1 ) return a ; a = ( a * b ) / hcf ( a , b ) ; b -= 1 ; return findlcm ( a , b ) ; } document . write ( n ) ; else document . write ( findlcm ( n , n - 1 ) ) ;
Smallest number divisible by first n numbers | Function for gcd ; Function returns the lcm of first n numbers ; function call
function gcd ( a , b ) { if ( a % b != 0 ) return gcd ( b , a % b ) ; else return b ; } function lcm ( n ) { let ans = 1 ; for ( let i = 1 ; i <= n ; i ++ ) ans = ( ans * i ) / ( gcd ( ans , i ) ) ; return ans ; } let n = 20 ; document . write ( lcm ( n ) ) ;
Trapezoidal Rule for Approximate Value of Definite Integral | A sample function whose definite integral 's approximate value  is computed using Trapezoidal rule ; Declaring the function f ( x ) = 1 / ( 1 + x * x ) ; Function to evaluate the value of integral ; Grid spacing ; Computing sum of first and last terms in above formula ; Adding middle terms in above formula ; h / 2 indicates ( b - a ) / 2 n . Multiplying h / 2 with s . ; Range of definite integral ; Number of grids . Higher value means more accuracy
function y ( x ) { return 1 / ( 1 + x * x ) ; } function trapezoidal ( a , b , n ) { let h = ( b - a ) / n ; let s = y ( a ) + y ( b ) ; for ( let i = 1 ; i < n ; i ++ ) s += 2 * y ( a + i * h ) ; return ( h / 2 ) * s ; } let x0 = 0 ; let xn = 1 ; let n = 6 ; document . write ( " " + Math . round ( trapezoidal ( x0 , xn , n ) * 10000.0 ) / 10000.0 ) ;
Breaking an Integer to get Maximum Product | The main function that returns the max possible product ; n equals to 2 or 3 must be handled explicitly ; Keep removing parts of size 3 while n is greater than 4 ; Keep multiplying 3 to res ; The last part multiplied by previous parts ; Driver code
function maxProd ( n ) { if ( n == 2 n == 3 ) return ( n - 1 ) ; let res = 1 ; while ( n > 4 ) { n -= 3 ; res *= 3 ; } return ( n * res ) ; } document . write ( " " + maxProd ( 45 ) ) ;
Finding number of digits in n 'th Fibonacci number | This function returns the number of digits in nth Fibonacci number after ceiling it Formula used ( n * log ( phi ) - ( log 5 ) / 2 ) ; using phi = 1.6180339887498948 ; Driver Code
function numberOfDigits ( n ) { if ( n == 1 ) return 1 ; let d = ( n * Math . log10 ( 1.6180339887498948 ) ) - ( ( Math . log10 ( 5 ) ) / 2 ) ; return Math . ceil ( d ) ; } let i ; for ( let i = 1 ; i <= 10 ; i ++ ) document . write ( ` ${ i } ${ numberOfDigits ( i ) } ` ) ;
Number of elements with odd factors in given range | Function to count odd squares ; Driver Code
function countOddSquares ( n , m ) { return Math . pow ( m , 0.5 ) - Math . pow ( n - 1 , 0.5 ) ; } let n = 5 , m = 100 ; document . write ( " " + countOddSquares ( n , m ) ) ;
Check if a number exists having exactly N factors and K prime factors | Function to compute the number of factors of the given number ; Vector to store the prime factors ; While there are no two multiples in the number , divide it by 2 ; If the size is already greater than K , then return true ; Computing the remaining divisors of the number ; If n is divisible by i , then it is a divisor ; If the size is already greater than K , then return true ; If the size is already greater than K , then return true ; If none of the above conditions satisfies , then return false ; Function to check if it is possible to make a number having total N factors and K prime factors ; If total divisors are less than the number of prime divisors , then print No ; Find the number of factors of n ; Driver Code
function factors ( n , k ) { let v = [ ] ; while ( n % 2 == 0 ) { v . push ( 2 ) ; n = parseInt ( n / 2 ) ; } if ( v . length >= k ) return true ; for ( let i = 3 ; i * i <= n ; i += 2 ) { while ( n % i == 0 ) { n = parseInt ( n / i ) ; v . push ( i ) ; } if ( v . length >= k ) return true ; } if ( n > 2 ) v . push ( n ) ; if ( v . length >= k ) return true ; return false ; } function operation ( n , k ) { let answered = false ; if ( n < k ) { answered = true ; document . write ( " " ) ; } let ok = factors ( n , k ) ; if ( ! ok && ! answered ) { answered = true ; document . write ( " " ) ; } if ( ok && ! answered ) document . write ( " " ) ; } let n = 4 ; let k = 2 ; operation ( n , k ) ;
No of Factors of n ! | Sieve of Eratosthenes to mark all prime number in array prime as 1 ; Initialize all numbers as prime ; Mark composites ; Returns the highest exponent of p in n ! ; Returns the no of factors in n ! ; ans stores the no of factors in n ! ; Find all primes upto n ; Multiply exponent ( of primes ) added with 1 ; if p is a prime then p is also a prime factor of n ! ; Driver code
function sieve ( n , prime ) { for ( let i = 1 ; i <= n ; i ++ ) prime [ i ] = 1 ; prime [ 1 ] = 0 ; for ( let i = 2 ; i * i <= n ; i ++ ) { if ( prime [ i ] == 1 ) { for ( let j = i * i ; j <= n ; j += i ) prime [ j ] = 0 ; } } } function expFactor ( n , p ) { let x = p ; let exponent = 0 ; while ( ( n / x ) > 0 ) { exponent += Math . floor ( n / x ) ; x *= p ; } return exponent ; } function countFactors ( n ) { let ans = 1 ; let prime = new Array ( n + 1 ) ; for ( let i = 0 ; i < n + 1 ; i ++ ) { prime [ i ] = 0 ; } sieve ( n , prime ) ; for ( let p = 1 ; p <= n ; p ++ ) { if ( prime [ p ] == 1 ) ans *= ( expFactor ( n , p ) + 1 ) ; } return ans ; } let n = 16 ; document . write ( " " + n + " " + countFactors ( n ) ) ;
LCM of given array elements | Function to return gcd of a and b ; recursive implementation ; lcm ( a , b ) = ( a * b / gcd ( a , b ) ) ; Driver code
function __gcd ( a , b ) { if ( a == 0 ) return b ; return __gcd ( b % a , a ) ; } function LcmOfArray ( arr , idx ) { if ( idx == arr . length - 1 ) { return arr [ idx ] ; } let a = arr [ idx ] ; let b = LcmOfArray ( arr , idx + 1 ) ; return ( a * b / __gcd ( a , b ) ) ; let arr = [ 1 , 2 , 8 , 3 ] ; document . write ( LcmOfArray ( arr , 0 ) + " " ) ; arr = [ 2 , 7 , 3 , 9 , 4 ] ; document . write ( LcmOfArray ( arr , 0 ) ) ;
Check if a number is a power of another number | Returns true if y is a power of x ; The only power of 1 is 1 itself ; Repeatedly compute power of x ; Check if power of x becomes y ; check the result for true / false and print .
function isPower ( x , y ) { if ( x == 1 ) return ( y == 1 ) ; let pow = 1 ; while ( pow < y ) pow = pow * x ; return ( pow == y ) ; } document . write ( ( isPower ( 10 , 1 ) ? 1 : 0 ) + " " ) ; document . write ( ( isPower ( 1 , 20 ) ? 1 : 0 ) + " " ) ; document . write ( ( isPower ( 2 , 128 ) ? 1 : 0 ) + " " ) ; document . write ( ( isPower ( 2 , 30 ) ? 1 : 0 ) + " " ) ;
Number of perfect squares between two given numbers | Function to count squares ; Driver code
function countSquares ( a , b ) { return ( Math . floor ( Math . sqrt ( b ) ) - Math . ceil ( Math . sqrt ( a ) ) + 1 ) ; } let a = 9 ; let b = 25 ; document . write ( " " , countSquares ( a , b ) ) ;
Count positive integers with 0 as a digit and maximum ' d ' digits | function to calculate the count of natural numbers upto a given number of digits that contain atleast one zero ; Sum of two GP series ; Driver Code
function findCountUpto ( d ) { let GP1_Sum = 9 * ( ( Math . pow ( 10 , d ) - 1 ) / 9 ) ; let GP2_Sum = 9 * ( ( Math . pow ( 9 , d ) - 1 ) / 8 ) ; return GP1_Sum - GP2_Sum ; } let d = 1 ; document . write ( findCountUpto ( d ) + " " ) ; d = 2 ; document . write ( findCountUpto ( d ) + " " ) ; d = 4 ; document . write ( findCountUpto ( d ) + " " ) ;
Check if count of divisors is even or odd | Function to count the divisors ; Initialize count of divisors ; Note that this loop runs till square root ; If divisors are equal increment count by one Otherwise increment count by 2 ; Driver Code
function countDivisors ( n ) { let count = 0 ; for ( let i = 1 ; i <= Math . sqrt ( n ) + 1 ; i ++ ) { if ( n % i == 0 ) count += ( Math . floor ( n / i ) == i ) ? 1 : 2 ; } if ( count % 2 == 0 ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; } document . write ( " " ) ; countDivisors ( 10 ) ;
Gray to Binary and Binary to Gray conversion | Javascript program for above approach ; Driver code
function greyConverter ( n ) { return n ^ ( n >> 1 ) ; } let n = 3 ; document . write ( greyConverter ( n ) + " " ) ; n = 9 ; document . write ( greyConverter ( n ) ) ;
How to avoid overflow in modular multiplication ? |
' ' ' ' ' ' function multiply ( a , b , mod ) { return ( ( a % mod ) * ( b % mod ) ) % mod ; }
Compute n ! under modulo p | Returns largest power of p that divides n ! ; Initialize result ; Calculate x = n / p + n / ( p ^ 2 ) + n / ( p ^ 3 ) + ... . ; Utility function to do modular exponentiation . It returns ( x ^ y ) % 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 ; Returns n ! % p ; Use Sieve of Eratosthenes to find all primes smaller than n ; Consider all primes found by Sieve ; Find the largest power of prime ' i ' that divides n ; Multiply result with ( i ^ k ) % p ; Driver method
function largestPower ( n , p ) { let x = 0 ; while ( n > 0 ) { n = parseInt ( n / p , 10 ) ; x += n ; } return x ; } function power ( x , y , p ) { let res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) { res = ( res * x ) % p ; } x = ( x * x ) % p ; } return res ; } function modFact ( n , p ) { if ( n >= p ) { return 0 ; } let res = 1 ; let isPrime = new Array ( n + 1 ) ; for ( let i = 0 ; i < n + 1 ; i ++ ) isPrime [ i ] = true ; for ( let i = 2 ; i * i <= n ; i ++ ) { if ( isPrime [ i ] ) { for ( let j = 2 * i ; j <= n ; j += i ) { isPrime [ j ] = false ; } } } for ( let i = 2 ; i <= n ; i ++ ) { if ( isPrime [ i ] ) { let k = largestPower ( n , i ) ; res = ( res * power ( i , k , p ) ) % p ; } } return res ; } let n = 25 , p = 29 ; document . write ( modFact ( n , p ) ) ;
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 ) [ m , n ] = [ n , m ] ; return m * ( m + 1 ) * ( 2 * m + 1 ) / 6 + ( n - m ) * m * ( m + 1 ) / 2 ; } let m = 4 ; let n = 3 ; document . write ( " " + countSquares ( n , m ) ) ;
Add two numbers using ++ and / or | Returns value of x + y without using + ; If y is positive , y times add 1 to x ; If y is negative , y times subtract 1 from x ; Driver code
function add ( x , y ) { while ( y > 0 && y != 0 ) { x ++ ; y -- ; } while ( y < 0 && y != 0 ) { x -- ; y ++ ; } return x ; } document . write ( add ( 43 , 23 ) + " " ) ; document . write ( add ( 43 , - 23 ) + " " ) ;
Count factorial numbers in a given range | Function to count factorial ; Find the first factorial number ' fact ' greater than or equal to ' low ' ; Count factorial numbers in range [ low , high ] ; Return the count ; Driver Code
function countFact ( low , high ) { let fact = 1 ; let x = 1 ; while ( fact < low ) { fact = fact * x ; x ++ ; } let res = 0 ; while ( fact <= high ) { res ++ ; fact = fact * x ; x ++ ; } return res ; } document . write ( " " + countFact ( 2 , 720 ) ) ;
Find number of days between two given dates | A date has day ' d ' , month ' m ' and year ' y ' ; To store number of days in all months from January to Dec . ; This function counts number of leap years before the given date ; Check if the current year needs to be considered for the count of leap years or not ; An year is a leap year if it is a multiple of 4 , multiple of 400 and not a multiple of 100. ; This function returns number of days between two given dates ; initialize count using years and day ; Add days for months in given date ; Since every leap year is of 366 days , Add a day for every leap year ; SIMILARLY , COUNT TOTAL NUMBER OF DAYS BEFORE ' dt2' ; return difference between two counts ; Driver code ; Function call
class Date { constructor ( d , m , y ) { this . d = d ; this . m = m ; this . y = y ; } } let monthDays = [ 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ] ; function countLeapYears ( d ) { let years = d . y ; if ( d . m <= 2 ) { years -- ; } return Math . floor ( years / 4 ) - Math . floor ( years / 100 ) + Math . floor ( years / 400 ) ; } function getDifference ( dt1 , dt2 ) { let n1 = dt1 . y * 365 + dt1 . d ; for ( let i = 0 ; i < dt1 . m - 1 ; i ++ ) { n1 += monthDays [ i ] ; } n1 += countLeapYears ( dt1 ) ; let n2 = dt2 . y * 365 + dt2 . d ; for ( let i = 0 ; i < dt2 . m - 1 ; i ++ ) { n2 += monthDays [ i ] ; } n2 += countLeapYears ( dt2 ) ; return ( n2 - n1 ) ; } let dt1 = new Date ( 1 , 2 , 2000 ) ; let dt2 = new Date ( 1 , 2 , 2004 ) ; document . write ( " " + getDifference ( dt1 , dt2 ) ) ;
Find length of period in decimal value of 1 / n | Function to find length of period in 1 / n ; Find the ( n + 1 ) th remainder after decimal point in value of 1 / n ; Store ( n + 1 ) th remainder ; Count the number of remainders before next occurrence of ( n + 1 ) ' th ▁ remainder ▁ ' d ' ; Driver code
function getPeriod ( n ) { let rem = 1 ; for ( let i = 1 ; i <= n + 1 ; i ++ ) rem = ( 10 * rem ) % n ; let d = rem ; let count = 0 ; do { rem = ( 10 * rem ) % n ; count ++ ; } while ( rem != d ) ; return count ; } document . write ( getPeriod ( 3 ) + " " ) document . write ( getPeriod ( 7 ) + " " )
Replace all â €˜ 0 â €™ with â €˜ 5 â €™ in an input Integer | Returns the number to be added to the input to replace all zeroes with five ; Amount to be added ; Unit decimal place ; A number divisible by 10 , then this is a zero occurrence in the input ; Move one decimal place ; Driver code
function calculateAddedValue ( number ) { let result = 0 ; let decimalPlace = 1 ; if ( number == 0 ) { result += ( 5 * decimalPlace ) ; } while ( number > 0 ) { if ( number % 10 == 0 ) { result += ( 5 * decimalPlace ) ; } number = Math . floor ( number / 10 ) ; decimalPlace *= 10 ; } return result ; } function replace0with5 ( number ) { return number += calculateAddedValue ( number ) ; } document . write ( replace0with5 ( 1020 ) ) ;
Program to find remainder without using modulo or % operator | This function returns remainder of num / divisor without using % ( modulo ) operator ; Driver Code
function getRemainder ( num , divisor ) { let t = ( num - divisor * parseInt ( num / divisor ) ) ; return t ; } document . write ( getRemainder ( 100 , 7 ) ) ;
Efficient Program to Compute Sum of Series 1 / 1 ! + 1 / 2 ! + 1 / 3 ! + 1 / 4 ! + . . + 1 / n ! | An Efficient Function to return value of 1 / 1 ! + 1 / 2 ! + . . + 1 / n ! ; Update factorial ; Update series sum ; Driver Code
function sum ( n ) { let sum = 0 ; let fact = 1 ; for ( let i = 1 ; i <= n ; i ++ ) { fact *= i ; sum += 1.0 / fact ; } return sum . toFixed ( 5 ) ; } let n = 5 ; document . write ( sum ( n ) ) ;
Print first k digits of 1 / n where n is a positive integer | Function to print first k digits after dot in value of 1 / n . n is assumed to be a positive integer . ; Initialize remainder ; Run a loop k times to print k digits ; The next digit can always be obtained as doing ( 10 * rem ) / 10 ; Update remainder ; Driver program to test above function
function print ( n , k ) { let rem = 1 ; let ans = ' ' ; for ( let i = 0 ; i < k ; i ++ ) { ans += Math . floor ( ( ( 10 * rem ) / n ) ) ; rem = ( 10 * rem ) % n ; } document . write ( ans ) } let n = 7 ; let k = 3 ; print ( n , k ) ; document . write ( " " ) ; n = 21 ; k = 4 ; print ( n , k ) ;
Program to find sum of series 1 + 1 / 2 + 1 / 3 + 1 / 4 + . . + 1 / n | Function to return sum of 1 / 1 + 1 / 2 + 1 / 3 + . . + 1 / n ; Driven Program
function sum ( n ) { var i , s = 0.0 ; for ( i = 1 ; i <= n ; i ++ ) s = s + 1 / i ; return s ; } var n = 5 ; document . write ( sum ( n ) . toFixed ( 5 ) ) ;
Program to find GCD or HCF of two numbers | Recursive function to return gcd of a and b ; Driver code
function gcd ( a , b ) { if ( b == 0 ) { return a ; } return gcd ( b , a % b ) ; } let a = 98 ; let b = 56 ; document . write ( ` ${ a } ${ b } ${ gcd ( a , b ) } ` ) ;