codeparrot/xlcost-text-to-code huggingface dataset:
Find the concentration of a solution using given Mass and Volume | Function to calculate concentration from the given mass of solute and volume of a solution ; Driver Program
function get_concentration ( mass , volume ) { if ( volume == 0 ) return - 1 ; else return ( mass / volume ) * 1000 ; } let mass , volume ; mass = 100.00 ; volume = 500.00 ; document . write ( get_concentration ( mass , volume ) ) ;
Practical Numbers | Returns true if there is a subset of set [ ] with sun equal to given sum ; The value of subset [ i ] [ j ] will be true if there is a subset of set [ 0. . j - 1 ] with sum equal to i ; If sum is 0 , then answer is true ; If sum is not 0 and set is empty , then answer is false ; Fill the subset table in bottom up manner ; Function to store divisors of N in a vector ; Find all divisors which divides ' num ' ; if ' i ' is divisor of ' n ' ; if both divisors are same then store it once else store both divisors ; Returns true if num is Practical ; vector to store all divisors of N ; to check all numbers from 1 to < N ; Driver code
function isSubsetSum ( set , n , sum ) { var subset = Array . from ( Array ( n + 1 ) , ( ) => Array ( sum + 1 ) ) ; for ( var i = 0 ; i <= n ; i ++ ) subset [ i ] [ 0 ] = true ; for ( var i = 1 ; i <= sum ; i ++ ) subset [ 0 ] [ i ] = false ; for ( var i = 1 ; i <= n ; i ++ ) { for ( var j = 1 ; j <= sum ; j ++ ) { if ( j < set [ i - 1 ] ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] ; if ( j >= set [ i - 1 ] ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] || subset [ i - 1 ] [ j - set [ i - 1 ] ] ; } } return subset [ n ] [ sum ] ; } function storeDivisors ( n , div ) { for ( var i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i == ( n / i ) ) div . push ( i ) ; else { div . push ( i ) ; div . push ( n / i ) ; } } } } function isPractical ( N ) { div = [ ] ; storeDivisors ( N , div ) ; for ( var i = 1 ; i < N ; i ++ ) { if ( ! isSubsetSum ( div , div . length , i ) ) return false ; } return true ; } var N = 18 ; isPractical ( N ) ? document . write ( " " ) : document . write ( " " ) ;
Super Niven Numbers | Checks if sums of all subsets of digits array divides the number N ; to calculate length of array arr ; There are totoal 2 ^ n subsets ; Consider all numbers from 0 to 2 ^ n - 1 ; Consider binary representation of current i to decide which elements to pick . ; check sum of picked elements . ; Function to check if a number is a super - niven number ; to stor digits of N ; Driver code
function isDivBySubsetSums ( arr , num ) { var n = arr . length ; var total = 1 << n ; for ( var i = 0 ; i < total ; i ++ ) { var sum = 0 ; for ( var j = 0 ; j < n ; j ++ ) if ( i & ( 1 << j ) ) sum += arr [ j ] ; if ( sum != 0 && num % sum != 0 ) return false ; } return true ; } function isSuperNivenNum ( n ) { var temp = n ; var digits = [ ] ; while ( n != 0 ) { var digit = n % 10 ; digits . push ( digit ) ; n = parseInt ( n / 10 ) ; } return isDivBySubsetSums ( digits , temp ) ; } var n = 500 ; if ( isSuperNivenNum ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Highly Composite Numbers | Function to count the number of divisors of the N ; sieve method for prime calculation ; Traversing through all prime numbers ; calculate number of divisor with formula total div = ( p1 + 1 ) * ( p2 + 1 ) * ... . . * ( pn + 1 ) where n = ( a1 ^ p1 ) * ( a2 ^ p2 ) . ... * ( an ^ pn ) ai being prime divisor for n and pi are their respective power in factorization ; Function to check if a number is a highly composite number ; count number of factors of N ; loop to count number of factors of every number less than N ; If any number less than N has more factors than N , then return false ; Given Number N ; Function Call
function divCount ( n ) { let hash = Array ( n + 1 ) . fill ( true ) ; for ( let p = 2 ; p * p < n ; p ++ ) if ( hash [ p ] == true ) for ( i = p * 2 ; i < n ; i += p ) hash [ i ] = false ; let total = 1 ; for ( p = 2 ; p <= n ; p ++ ) { if ( hash [ p ] ) { let count = 0 ; if ( n % p == 0 ) { while ( n % p == 0 ) { n = n / p ; count ++ ; } total = total * ( count + 1 ) ; } } } return total ; } function isHighlyCompositeNumber ( N ) { let NdivCount = divCount ( N ) ; for ( let i = 1 ; i < N ; i ++ ) { let idivCount = divCount ( i ) ; if ( idivCount >= NdivCount ) return false ; } return true ; } let N = 12 ; if ( isHighlyCompositeNumber ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Pentacontahenagon Number | Function to find the N - th Pentacontahenagon Number ; Driver Code ; Function Call
function PentacontahenagonNum ( N ) { return ( 49 * N * N - 47 * N ) / 2 ; } let N = 3 ; document . write ( " " + PentacontahenagonNum ( N ) ) ;
Saint | Function to check if a number is a Saint - Exupery number ; Considering triplets in sorted order . The value of first element in sorted triplet can be at - most n / 3. ; The value of second element must be less than equal to n / 2 ; Given Number N ; Function Call
function isSaintExuperyNum ( n ) { for ( i = 1 ; i <= n / 3 ; i ++ ) { for ( j = i + 1 ; j <= n / 2 ; j ++ ) { let k = n / i / j ; if ( i * i + j * j == k * k ) { if ( i * j * k == n ) return true ; } } } return false ; } let N = 60 ; if ( isSaintExuperyNum ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Repdigit Numbers | Function to check if a number is a Repdigit number ; To store previous digit ( Assigning initial value which is less than any digit ) ; Traverse all digits from right to left and check if any digit is smaller than previous . ; Driver code
function isRepdigit ( num , b ) { let prev = - 1 ; while ( num != 0 ) { let digit = num % b ; num = parseInt ( num / b ) ; if ( prev != - 1 && digit != prev ) return false ; prev = digit ; } return true ; } let num = 2000 , base1 = 7 ; if ( isRepdigit ( num , base1 ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Enlightened Numbers | Function to check if N is a Composite Number ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to return concatenation of distinct prime factors of a given number n ; Handle prime factor 2 explicitly so that can optimally handle other prime factors . ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Function to check if a number is is an enlightened number ; Number should not be prime ; Converting N to string ; Function call ; Driver code
function isComposite ( n ) { if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; for ( var i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; return false ; } function concatenatePrimeFactors ( n ) { var concatenate ; if ( n % 2 == 0 ) { concatenate += " " ; while ( n % 2 == 0 ) n = parseInt ( n / 2 ) ; } for ( var i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { concatenate += i ; while ( n % i == 0 ) n = parseInt ( n / i ) ; } } if ( n > 2 ) concatenate += n ; return concatenate ; } function isEnlightened ( N ) { if ( ! isComposite ( N ) ) return false ; var num = ( N . toString ( ) ) ; var prefixConc = concatenatePrimeFactors ( N ) ; return ( prefixConc ) ; } var n = 250 ; if ( isEnlightened ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Second Pentagonal numbers | Function to find N - th term in the series ; Driver code
function findNthTerm ( n ) { document . write ( n * ( 3 * n + 1 ) / 2 ) ; } N = 4 ; findNthTerm ( N ) ;
Idoneal Numbers | Function to check if number is an Idoneal numbers ; iterate for all triples pairs ( a , b , c ) ; if the condition is satisfied ; Driver Code ; Function Call
function isIdoneal ( n ) { for ( var a = 1 ; a <= n ; a ++ ) { for ( var b = a + 1 ; b <= n ; b ++ ) { for ( var c = b + 1 ; c <= n ; c ++ ) { if ( a * b + b * c + c * a == n ) return false ; } } } return true ; } var N = 10 ; if ( isIdoneal ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Lynch | Function to check the divisibility of the number by its digit . ; If the digit divides the number then return true else return false . ; Function to check if all digits of n divide it or not ; Taking the digit of the number into digit var . ; Function to check if N has all distinct digits ; Create an array of size 10 and initialize all elements as false . This array is used to check if a digit is already seen or not . ; Traverse through all digits of given number ; Find the last digit ; If digit is already seen , return false ; Mark this digit as seen ; REmove the last digit from number ; Function to check Lynch - Bell numbers ; Given Number N ; Function Call
function checkDivisibility ( n , digit ) { return ( digit != 0 && n % digit == 0 ) ; } function isAllDigitsDivide ( n ) { let temp = n ; while ( temp > 0 ) { let digit = temp % 10 ; if ( ! ( checkDivisibility ( n , digit ) ) ) return false ; temp = parseInt ( temp / 10 ) ; } return true ; } function isAllDigitsDistinct ( n ) { let arr = Array ( 10 ) . fill ( 0 ) ; while ( n > 0 ) { let digit = n % 10 ; if ( arr [ digit ] ) return false ; arr [ digit ] = true ; n = parseInt ( n / 10 ) ; } return true ; } function isLynchBell ( n ) { return isAllDigitsDivide ( n ) && isAllDigitsDistinct ( n ) ; } let N = 12 ; if ( isLynchBell ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Repunit numbers | Function to check if a number contains all the digits 0 , 1 , . . , ( b - 1 ) an equal number of times ; to store number of digits of n in base B ; to count frequency of digit 1 ; condition to check three or more 1 's  and number of ones is equal to number  of digits of n in base B ; taking inputs ; function to check
function isRepunitNum ( n , b ) { let length = 0 ; let countOne = 0 ; while ( n != 0 ) { let r = n % b ; length ++ ; if ( r == 1 ) countOne ++ ; n = parseInt ( n / b ) ; } return countOne >= 3 && countOne == length ; } let n = 31 ; let base = 2 ; if ( isRepunitNum ( n , base ) ) document . write ( " " ) ; else document . write ( " " ) ;
Gapful Numbers | Find the first digit ; Find total number of digits - 1 ; Find first digit ; Return first digit ; Find the last digit ; Return the last digit ; A function to check Gapful numbers ; Return true if n is gapful number ; Given number ; Function call
function firstDigit ( n ) { let digits = parseInt ( ( Math . log ( n ) / Math . log ( 10 ) ) ) ; n = parseInt ( ( n / Math . pow ( 10 , digits ) ) ) ; return n ; } function lastDigit ( n ) { return ( n % 10 ) ; } function isGapful ( n ) { let first_dig = firstDigit ( n ) ; let last_dig = lastDigit ( n ) ; let concatenation = first_dig * 10 + last_dig ; return ( n % concatenation == 0 ) ; } let n = 108 ; if ( isGapful ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if all objects of type A and B can be placed on N shelves | Function to return if allocation is possible or not ; Stores the shelves needed for items of type - A and type - B ; Find number of shelves needed for items of type - A ; Fill A / K shelves fully by the items of type - A ; Otherwise ; Fill A / L shelves fully and add remaining to an extra shelf ; Find number of shelves needed for items of type - B ; Fill B / L shelves fully by the items of type - B ; Fill B / L shelves fully and add remaining to an an extra shelf ; Total shelves needed ; If required shelves exceed N ; Driver code
function isPossible ( A , B , N , K , L ) { let needa , needb ; if ( A % K == 0 ) needa = Math . floor ( A / K ) ; else needa = Math . floor ( A / K ) + 1 ; if ( B % L == 0 ) needb = Math . floor ( B / L ) ; else needb = Math . floor ( B / L ) + 1 ; let total = needa + needb ; if ( total > N ) return false ; else return true ; } let A = 3 , B = 3 , N = 3 ; let K = 4 , M = 2 ; if ( isPossible ( A , B , N , K , M ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;
Minimise N such that sum of count of all factors upto N is greater than or equal to X | JavaScript program for the above approach ; Array to store smallest prime factors of each no . ; Function to calculate smallest prime factor of N . ; marking spf [ j ] if it is not previously marked ; Array to store the count of factor for N ; Prefix array which contains the count of factors from 1 to N ; Function to count total factors from 1 to N ; Store total factors of i ; Stores total factors from 1 to i ; Function to search lowest X such that the given condition is satisfied ; Find mid ; Search in the right half ; Search in the left half ; Return the position after Binary Search ; Function to find the required sum ; Precompute smallest prime factor of each value ; Calculate count of total factors from 1 to N ; Binary search to find minimum N ; Given Sum ; Function Call
var MAX = 1000050 ; var spf = Array ( MAX + 1 ) ; function calculate_SPF ( ) { for ( var i = 0 ; i <= MAX ; i ++ ) spf [ i ] = i ; for ( var i = 4 ; i <= MAX ; i += 2 ) spf [ i ] = 2 ; for ( var i = 3 ; i * i <= MAX ; i ++ ) { if ( spf [ i ] == i ) { for ( var j = i * i ; j <= MAX ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } var tfactor = Array ( MAX + 1 ) ; var pre = Array ( MAX + 1 ) ; function CountTotalfactors ( ) { tfactor [ 1 ] = pre [ 1 ] = 1 ; for ( var i = 2 ; i <= MAX ; i ++ ) { var mspf = spf [ i ] ; var prim = mspf ; var temp = i ; var cnt = 0 ; while ( temp % mspf == 0 ) { temp = parseInt ( temp / mspf ) ; cnt += 1 ; prim = prim * mspf ; } tfactor [ i ] = ( cnt + 1 ) * tfactor [ temp ] ; pre [ i ] = pre [ i - 1 ] + tfactor [ i ] ; } } function BinarySearch ( X ) { var start = 1 ; var end = MAX - 1 ; while ( start < end ) { var mid = parseInt ( ( start + end ) / 2 ) ; if ( pre [ mid ] == X ) return mid ; else if ( pre [ mid ] < X ) start = mid + 1 ; else end = mid ; } return start ; } function findSumOfCount ( X ) { calculate_SPF ( ) ; CountTotalfactors ( ) ; document . write ( BinarySearch ( X ) + " " ) ; } var X = 10 ; findSumOfCount ( X ) ;
Check if the Matrix follows the given constraints or not | Stores true at prime indices ; Function to generate the prime numbers using Sieve of Eratosthenes ; If p is still true ; Mark all multiples of p ; Function returns sum of all elements of matrix ; Function to check if for all prime ( i + j ) , a [ i ] [ j ] is prime ; If index is prime ; Driver Code ; Check for both conditions
var prime = [ ] ; function buildSieve ( sum ) { prime = Array ( sum + 1 ) . fill ( true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( var p = 2 ; p * p < ( sum + 1 ) ; p ++ ) { if ( prime [ p ] == true ) { for ( var i = p * 2 ; i < ( sum + 1 ) ; i += p ) prime [ i ] = false ; } } } function getSum ( a ) { var s = 0 ; for ( var i = 0 ; i < 4 ; i ++ ) for ( var j = 0 ; j < 5 ; j ++ ) s += a [ i ] [ j ] ; return s ; } function checkIndex ( n , m , a ) { for ( var i = 0 ; i < n ; i ++ ) for ( var j = 0 ; j < m ; j ++ ) { if ( prime [ i + j ] && ! prime [ a [ i ] [ j ] ] ) { return false ; } } return true ; } var n = 4 , m = 5 ; var a = [ [ 1 , 2 , 3 , 2 , 2 ] , [ 2 , 2 , 7 , 7 , 7 ] , [ 7 , 7 , 21 , 7 , 10 ] , [ 2 , 2 , 3 , 6 , 7 ] ] ; var sum = getSum ( a ) ; buildSieve ( sum ) ; if ( prime [ sum ] && checkIndex ( n , m , a ) ) { document . write ( " " ) ; } else document . write ( " " ) ;
How to calculate the Easter date for a given year using Gauss ' Algorithm | Function calculates and prlets easter date for given year Y ; All calculations done on the basis of Gauss Easter Algorithm ; A corner case , when D is 29 ; Another corner case , when D is 28 ; If days > 31 , move to April April = 4 th Month ; Otherwise , stay on March March = 3 rd Month ; Driver Code
function gaussEaster ( Y ) { let A , B , C , P , Q , M , N , D , E ; A = Y % 19 ; B = Y % 4 ; C = Y % 7 ; P = Math . floor ( Y / 100 ) ; Q = Math . floor ( ( 13 + 8 * P ) / 25 ) ; M = ( 15 - Q + P - P / 4 ) % 30 ; N = ( 4 + P - P / 4 ) % 7 ; D = ( 19 * A + M ) % 30 ; E = ( 2 * B + 4 * C + 6 * D + N ) % 7 ; let days = ( 22 + D + E ) ; if ( ( D == 29 ) && ( E == 6 ) ) { document . write ( Y + " " + " " ) ; return ; } else if ( ( D == 28 ) && ( E == 6 ) ) { document . write ( Y + " " + " " ) ; return ; } else { if ( days > 31 ) { document . write ( Y + " " + ( days - 31 ) ) ; return ; } else { document . write ( Y + " " + days ) ; return ; } } } let Y = 2020 ; gaussEaster ( Y ) ;
Place Value of a given digit in a number | Function to find place value ; Digit , which we want to find place value . ; Number from where we want to find place value .
function placeValue ( N , num ) { var total = 1 , value = 0 , rem = 0 ; while ( true ) { rem = N % 10 ; N = parseInt ( N / 10 ) ; if ( rem == num ) { value = total * rem ; break ; } total = total * 10 ; } return value ; } var D = 5 ; var N = 85932 ; document . write ( placeValue ( N , D ) ) ;
Super | Function to find the divisors ; Loop to iterate over the square root of the N ; Check if divisors are equal ; Function to check if N is a super Poulet number ; Loop to check that every divisor divides 2 ^ D - 2 ; Driver Code
function findDivisors ( n ) { let divisors = [ ] for ( let i = 1 ; i < Math . floor ( Math . sqrt ( n ) + 1 ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { divisors . push ( i ) } else { divisors . push ( i ) divisors . push ( Math . floor ( n / i ) ) } } } return divisors . sort ( ( a , b ) => a - b ) } function isSuperdNum ( n ) { let d = findDivisors ( n ) for ( let i in d ) { let x = ( 2 ** i - 2 ) / i if ( Math . floor ( x ) != x ) { return false } } return true } let n = 341 if ( isSuperdNum ( n ) == true ) { document . write ( " " ) } else { document . write ( " " ) }
Beatty sequence | Function to print the first N terms of the Beatty sequence ; Driver code
function BeattySequence ( n ) { for ( let i = 1 ; i <= n ; i ++ ) { let ans = parseInt ( Math . floor ( i * Math . sqrt ( 2 ) ) ) ; document . write ( ans + " " ) ; } } let n = 5 ; BeattySequence ( n ) ;
Primitive Abundant Number | Function to sum of divisors ; Note that this loop runs till square root of N ; If divisors are equal , take only one of them ; Otherwise take both ; Function to check Abundant Number ; Return true if sum of divisors is greater than N . ; Function to check Deficient Number ; Check if sum ( n ) < 2 * n ; Function to check all proper divisors of N is deficient number or not ; If number itself is not abundant retuen false ; Find all divisors which divides ' num ' ; if ' i ' is divisor of ' num ' ; if both divisors are same then add it only once else add both ; Driver Code
function getSum ( n ) { let sum = 0 ; for ( let i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) sum = sum + i ; else { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum ; } function checkAbundant ( n ) { return ( getSum ( n ) - n > n ) ; } function isDeficient ( n ) { return ( getSum ( n ) < ( 2 * n ) ) ; } function checkPrimitiveAbundant ( num ) { if ( ! checkAbundant ( num ) ) { return false ; } for ( let i = 2 ; i <= Math . sqrt ( num ) ; i ++ ) { if ( num % i == 0 && i != num ) { if ( i * i == num ) { if ( ! isDeficient ( i ) ) { return false ; } } else if ( ! isDeficient ( i ) || ! isDeficient ( num / i ) ) { return false ; } } } return true ; } let n = 20 ; if ( checkPrimitiveAbundant ( n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Canada Numbers | Function to calculate sum of all trivial divisors of given natural number ; Final result of summation of trivial divisors ; Find all divisors which divides ' num ' ; if ' i ' is divisor of ' num ' ; if both divisors are same then add it only once else add both ; Function to return sum of squares of digits of N ; Function to check if N is a Canada number ; Given Number ; Function Call
function divSum ( num ) { let result = 0 ; for ( let i = 1 ; i <= Math . sqrt ( num ) ; i ++ ) { if ( num % i == 0 ) { if ( i == ( num / i ) ) result += i ; else result += ( i + num / i ) ; } } return ( result - 1 - num ) ; } function getSum ( n ) { let sum = 0 ; while ( n != 0 ) { let r = n % 10 ; sum = sum + r * r ; n = parseInt ( n / 10 ) ; } return sum ; } function isCanada ( n ) { return divSum ( n ) == getSum ( n ) ; } let n = 125 ; if ( isCanada ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Self Numbers | Function to find the sum of digits of a number N ; Function to check for Self number ; Driver code
function getSum ( n ) { let sum = 0 ; while ( n != 0 ) { sum = sum + n % 10 ; n = parseInt ( n / 10 ) ; } return sum ; } function isSelfNum ( n ) { for ( let m = 1 ; m <= n ; m ++ ) { if ( m + getSum ( m ) == n ) return false ; } return true ; } let n = 20 ; if ( isSelfNum ( n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Central binomial coefficient | Function to find the value of Nth Central Binomial Coefficient ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Driver Code
function binomialCoeff ( n , k ) { var C = Array . from ( Array ( n + 1 ) , ( ) => Array ( k + 1 ) ) ; var i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } var n = 3 ; var k = n ; n = 2 * n ; document . write ( binomialCoeff ( n , k ) ) ;
Multiply perfect number | Function to find the sum of divisors ; Note that this loop runs till square root of N ; If divisors are equal , take only one of them ; Otherwise take both ; Function to check Multiply - perfect number ; Driver code
function getSum ( n ) { let sum = 0 ; for ( i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) sum = sum + i ; else { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum ; } function MultiplyPerfectNumber ( n ) { if ( getSum ( n ) % n == 0 ) return true ; else return false ; } let n = 28 ; if ( MultiplyPerfectNumber ( n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Perfect totient number | Function to find the Totient number of the given value ; Initialize result as n ; Consider all prime factors of n and subtract their multiples from result ; Check if p is a prime factor ; If yes , then update N and result ; If n has a prime factor greater than sqrt ( n ) ( There can be at - most one such prime factor ) ; Function to check if the number is a perfect totient number ; Store original value of n ; Loop to calculate sum of iterated totients ; Condition for Perfect Totient Number ; Driver Code
function phi ( n ) { let result = n ; for ( let p = 2 ; p * p <= n ; ++ p ) { if ( n % p == 0 ) { while ( n % p == 0 ) { n = parseInt ( n / p ) ; } result -= parseInt ( result / p ) ; } } if ( n > 1 ) result -= parseInt ( result / n ) ; return result ; } function isPerfectTotientNum ( n ) { let temp = n ; let sum = 0 ; while ( n > 1 ) { sum = sum + phi ( n ) ; n = phi ( n ) ; } if ( sum == temp ) return true ; return false ; } let n = 9 ; if ( isPerfectTotientNum ( n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Cunningham Numbers | Function to check if a number can be expressed as a ^ b . ; Function to check if N is a Cunningham number ; Given Number ; Function Call
function isPower ( a ) { if ( a == 1 ) return true ; for ( let i = 2 ; i * i <= a ; i ++ ) { let val = Math . log ( a ) / Math . log ( i ) ; if ( ( val - parseInt ( val ) < 0.00000001 ) ) return true ; } return false ; } function isCunningham ( n ) { return isPower ( n - 1 ) || isPower ( n + 1 ) ; } let n = 126 ; if ( isCunningham ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Product of all Subarrays of an Array | Set 2 | Function to find the product of elements of all subarray ; Initialize the result ; Computing the product of subarray using formula ; Return the product of all elements of each subarray ; Given array arr [ ] ; Function Call
function SubArrayProdct ( arr , n ) { let result = 1 ; for ( let i = 0 ; i < n ; i ++ ) result *= Math . pow ( arr [ i ] , ( i + 1 ) * ( n - i ) ) ; return result ; } let arr = [ 2 , 4 ] ; let N = arr . length ; document . write ( SubArrayProdct ( arr , N ) ) ;