codeparrot/xlcost-text-to-code huggingface dataset:
Find if n can be written as product of k numbers | Prints k factors of n if n can be written as multiple of k numbers . Else prints - 1. ; A vector to store all prime factors of n ; Insert all 2 's in list ; n must be odd at this point So we skip one element ( i = i + 2 ) ; This is to handle when n > 2 and n is prime ; If size ( P ) < k , k factors are not possible ; printing first k - 1 factors ; calculating and printing product of rest of numbers ; Driver code
function kFactors ( n , k ) { var P = Array ( ) ; while ( n % 2 == 0 ) { P . push ( 2 ) ; n = parseInt ( n / 2 ) ; } for ( i = 3 ; i * i <= n ; i = i + 2 ) { while ( n % i == 0 ) { n = parseInt ( n / i ) ; P . push ( i ) ; } } if ( n > 2 ) P . push ( n ) ; if ( P . length < k ) { document . write ( " " ) ; return ; } for ( i = 0 ; i < k - 1 ; i ++ ) document . write ( P [ i ] + " " ) ; var product = 1 ; for ( i = k - 1 ; i < P . length ; i ++ ) product = product * P [ i ] ; document . write ( product ) ; } var n = 54 , k = 3 ; kFactors ( n , k ) ;
Largest number smaller than or equal to n and digits in non | Returns the required number ; Loop to recursively check the numbers less than or equal to given number ; Keep traversing digits from right to left . For every digit check if it is smaller than prev_dig ; We found the required number ; Driver code
function nondecdigits ( n ) { let x = 0 ; for ( x = n ; x >= 1 ; x -- ) { let no = x ; let prev_dig = 11 ; let flag = true ; while ( no != 0 ) { if ( prev_dig < no % 10 ) { flag = false ; break ; } prev_dig = no % 10 ; no = Math . floor ( no / 10 ) ; } if ( flag == true ) break ; } return x ; } let n = 200 ; document . write ( nondecdigits ( n ) ) ;
Largest number smaller than or equal to n and digits in non | Prints the largest number smaller than s and digits in non - decreasing order . ; Array to store digits of number ; Conversion of characters of string var number ; Variable holds the value of index after which all digits are set 9 ; Checking the condition if the digit is less than its left digit ; If first digit is 0 no need to prvar it ; Driver code
function nondecdigits ( s ) { var m = s . length ; var a = Array . from ( { length : m + 1 } , ( _ , i ) => 0 ) ; for ( i = 0 ; i < m ; i ++ ) a [ i ] = s . charAt ( i ) . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; var level = m - 1 ; for ( i = m - 1 ; i > 0 ; i -- ) { if ( a [ i ] < a [ i - 1 ] ) { a [ i - 1 ] -- ; level = i - 1 ; } } if ( a [ 0 ] != 0 ) { for ( i = 0 ; i <= level ; i ++ ) document . write ( a [ i ] ) ; for ( i = level + 1 ; i < m ; i ++ ) document . write ( " " ) ; } else { for ( i = 1 ; i < level ; i ++ ) document . write ( a [ i ] ) ; for ( i = level + 1 ; i < m ; i ++ ) document . write ( " " ) ; } } var n = " " ; nondecdigits ( n ) ;
Count Divisors of n in O ( n ^ 1 / 3 ) | function to count the divisors ; If divisors are equal , count only one ; else Otherwise count both ; Driver Code
function countDivisors ( n ) { let cnt = 0 ; for ( let i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) cnt ++ ; cnt = cnt + 2 ; } } return cnt ; } document . write ( " " + " " + countDivisors ( 100 ) ) ;
Check if the door is open or closed | Function to check whether ' n ' has even number of factors or not ; if ' n ' is a perfect square it has odd number of factors ; else ' n ' has even number of factors ; Function to find and print status of each door ; If even number of factors final status is closed ; else odd number of factors final status is open ; Driver Code
function hasEvenNumberOfFactors ( n ) { let root_n = Math . sqrt ( n ) ; if ( ( root_n * root_n ) == n ) return false ; return true ; } function printStatusOfDoors ( n ) { for ( let i = 1 ; i <= n ; i ++ ) { if ( hasEvenNumberOfFactors ( i ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; } } let n = 5 ; printStatusOfDoors ( n ) ;
Check if frequency of each digit is less than the digit | Function to validate number ( Check iffrequency of a digit is less than thedigit itself or not ) ; If current digit of temp is same as i ; if frequency is greater than digit value , return false ; Driver Code
function validate ( n ) { for ( let i = 0 ; i < 10 ; i ++ ) { let temp = n ; let count = 0 ; while ( temp > 0 ) { if ( temp % 10 == i ) count ++ ; if ( count > i ) return false ; temp /= 10 ; } } return true ; } let n = 1552793 ; if ( validate ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if a larger number divisible by 36 | Function to check whether a number is divisible by 36 or not ; null number cannot be divisible by 36 ; single digit number other than 0 is not divisible by 36 ; number formed by the last 2 digits ; if number is not divisible by 4 ; number is divisible by 4 calculate sum of digits ; sum of digits is not divisible by 9 ; number is divisible by 4 and 9 hence , number is divisible by 36 ; Driver Code
function divisibleBy36 ( num ) { let l = num . length ; if ( l == 0 ) return " " ; if ( l == 1 && num [ 0 ] != ' ' ) return " " ; let two_digit_num = ( num [ l - 2 ] - ' ' ) * 10 + ( num [ l - 1 ] - ' ' ) ; if ( two_digit_num % 4 != 0 ) return " " ; let sum = 0 ; for ( let i = 0 ; i < l ; i ++ ) sum += ( num [ i ] - ' ' ) ; if ( sum % 9 != 0 ) return " " ; return " " ; } let num = " " ; document . write ( divisibleBy36 ( num ) ) ;
Check if a large number is divisible by 8 or not | Function to find that number divisible by 8 or not ; Empty string ; If there is single digit ; If there is double digit ; If number formed by last three digits is divisible by 8 ; Driver Code
function check ( str ) { let n = str . length ; if ( n == 0 ) return false ; if ( n == 1 ) return ( ( str [ 0 ] - ' ' ) % 8 == 0 ) ; if ( n == 2 ) return ( ( ( str [ n - 2 ] - ' ' ) * 10 + ( str [ n - 1 ] - ' ' ) ) % 8 == 0 ) ; let last = str [ n - 1 ] - ' ' ; let second_last = str [ n - 2 ] - ' ' ; let third_last = str [ n - 3 ] - ' ' ; return ( ( third_last * 100 + second_last * 10 + last ) % 8 == 0 ) ; } let str = " " ; if ( check ( str ) ) document . write ( " " ) ; else document . write ( " " ) ;
Prime points ( Points that split a number into two primes ) | Function to count number of digits ; Function to check whether a number is prime or not . Returns 0 if prime else - 1 ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to print prime points ; Counting digits ; As single and double digit numbers do not have left and right number pairs ; Finding all left and right pairs . Printing the prime points accordingly . Discarding first and last index point ; Calculating left number ; Calculating right number ; Prime point condition ; No prime point found ; Driver Code
function countDigits ( n ) { let count = 0 ; while ( n > 0 ) { count ++ ; n = Math . floor ( n / 10 ) ; } return count ; } function checkPrime ( n ) { if ( n <= 1 ) return - 1 ; if ( n <= 3 ) return 0 ; if ( n % 2 == 0 n % 3 == 0 ) return - 1 ; for ( let i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return - 1 ; return 0 ; } function printPrimePoints ( n ) { let count = countDigits ( n ) ; if ( count == 1 count == 2 ) { document . write ( " " ) ; return ; } let found = false ; for ( let i = 1 ; i < ( count - 1 ) ; i ++ ) { let left = Math . floor ( n / ( Math . pow ( 10 , count - i ) ) ) ; let right = n % ( Math . pow ( 10 , count - i - 1 ) ) ; if ( checkPrime ( left ) == 0 && checkPrime ( right ) == 0 ) { document . write ( i + " " ) ; found = true ; } } if ( found == false ) document . write ( " " ) ; } let n = 2317 ; printPrimePoints ( n ) ;
Find ways an Integer can be expressed as sum of n | Function to calculate and return the power of any given number ; Function to check power representations recursively ; Initialize number of ways to express x as n - th powers of different natural numbers ; Calling power of ' i ' raised to ' n ' ; Recursively check all greater values of i ; If sum of powers is equal to x then increase the value of result . ; Return the final result ; Driver Code .
function power ( num , n ) { if ( n == 0 ) return 1 ; else if ( n % 2 == 0 ) return power ( num , parseInt ( n / 2 ) ) * power ( num , parseInt ( n / 2 ) ) ; else return num * power ( num , parseInt ( n / 2 ) ) * power ( num , parseInt ( n / 2 ) ) ; } function checkRecursive ( x , n , curr_num , curr_sum ) { var results = 0 ; var p = power ( curr_num , n ) ; while ( p + curr_sum < x ) { results += checkRecursive ( x , n , curr_num + 1 , p + curr_sum ) ; curr_num ++ ; p = power ( curr_num , n ) ; } if ( p + curr_sum == x ) results ++ ; return results ; } var x = 10 , n = 2 ; document . write ( checkRecursive ( x , n , 1 , 0 ) ) ;
Number Theory | Generators of finite cyclic group under addition | Function to return gcd of a and b ; Print generators of n ; 1 is always a generator ; A number x is generator of GCD is 1 ; Driver Code
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function printGenerators ( n ) { document . write ( " " ) ; for ( var i = 2 ; i < n ; i ++ ) if ( gcd ( i , n ) == 1 ) document . write ( i + " " ) ; } var n = 10 ; printGenerators ( n ) ;
Check if a large number is divisible by 3 or not | Function to find that number divisible by 3 or not ; Compute sum of digits ; Check if sum of digits is divisible by 3. ; Driver code
function check ( str ) { let n = str . length ; let digitSum = 0 ; for ( let i = 0 ; i < n ; i ++ ) digitSum += ( str [ i ] - ' ' ) ; return ( digitSum % 3 == 0 ) ; } let str = " " ; let x = check ( str ) ? " " : " " ; document . write ( x ) ;
Count all perfect divisors of a number | Javascript code to count total perfect divisors ; Pre - compute counts of all perfect divisors of all numbers upto MAX . ; Iterate through all the multiples of i * i ; Increment all such multiples by 1 ; Returns count of perfect divisors of n . ; Driver code
let MAX = 100001 ; ; let perfectDiv = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { perfectDiv [ i ] = 0 ; } function precomputeCounts ( ) { for ( let i = 1 ; i * i < MAX ; ++ i ) { for ( let j = i * i ; j < MAX ; j += i * i ) ++ perfectDiv [ j ] ; } } function countPerfectDivisors ( n ) { return perfectDiv [ n ] ; } precomputeCounts ( ) ; let n = 16 ; document . write ( " " + n + " " + countPerfectDivisors ( n ) + " " ) ; n = 12 ; document . write ( " " + n + " " + countPerfectDivisors ( n ) ) ;
Prime Factorization using Sieve O ( log n ) for multiple queries | Javascript program to find prime factorization of a number n in O ( Log n ) time with precomputation allowed . ; stores smallest prime factor for every number ; Calculating SPF ( Smallest Prime Factor ) for every number till MAXN . Time Complexity : O ( nloglogn ) ; marking smallest prime factor for every number to be itself . ; separately marking spf for every even number as 2 ; checking if i is prime ; marking SPF for all numbers divisible by i ; marking spf [ j ] if it is not previously marked ; A O ( log n ) function returning primefactorization by dividing by smallest prime factor at every step ; precalculating Smallest Prime Factor ; calling getFactorization function
let MAXN = 100001 ; let spf = new Array ( MAXN ) ; function sieve ( ) { spf [ 1 ] = 1 ; for ( let i = 2 ; i < MAXN ; i ++ ) spf [ i ] = i ; for ( let i = 4 ; i < MAXN ; i += 2 ) spf [ i ] = 2 ; for ( let i = 3 ; i * i < MAXN ; i ++ ) { if ( spf [ i ] == i ) { for ( let j = i * i ; j < MAXN ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } function getFactorization ( x ) { let ret = [ ] ; while ( x != 1 ) { ret . push ( spf [ x ] ) ; x = Math . floor ( x / spf [ x ] ) ; } return ret ; } sieve ( ) ; let x = 12246 ; document . write ( " " + x + " " ) ; let p = getFactorization ( x ) ; for ( let i = 0 ; i < p . length ; i ++ ) document . write ( p [ i ] + " " ) ; document . write ( " " ) ;
Nearest element with at | Javascript program to print nearest element with at least one common prime factor . ; Pre - computation of smallest prime divisor of all numbers ; Prime number will have same divisor ; Function to calculate all divisors of input array ; Pre - compute all the divisors of array element by using prime factors ; Traverse all elements , ; For every divisor of current element , find closest element . ; Visit divisor if not visited ; Fetch the index of visited divisor ; Update the divisor index to current index ; Set the minimum distance ; Set the min distance of current index ' i ' to nearest one ; Add 1 as indexing starts from 0 ; Set the min distance of found index ' ind ' ; Add 1 as indexing starts from 0 ; Driver code ; Simple sieve to find smallest prime divisor of number from 2 to MAX ; function to calculate nearest distance of every array elements ; Print the nearest distance having GDC > 1
let MAX = 100001 ; let INF = Number . MAX_VALUE ; let primedivisor = new Array ( MAX ) ; let dist = new Array ( MAX ) ; let pos = new Array ( MAX ) ; let divInd = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { primedivisor [ i ] = 0 ; dist [ i ] = 0 ; pos [ i ] = 0 ; divInd [ i ] = 0 ; } let divisors = [ ] ; function sieveOfEratosthenes ( ) { for ( let i = 2 ; i * i < MAX ; ++ i ) { if ( primedivisor [ i ] == 0 ) { for ( let j = i * i ; j < MAX ; j += i ) { primedivisor [ j ] = i ; } } } for ( let i = 1 ; i < MAX ; ++ i ) { if ( primedivisor [ i ] == 0 ) { primedivisor [ i ] = i ; } } } function findDivisors ( arr , n ) { for ( let i = 0 ; i < MAX ; ++ i ) { pos [ i ] = divInd [ i ] = - 1 ; dist [ i ] = INF ; } for ( let i = 0 ; i < n ; ++ i ) { let num = arr [ i ] ; while ( num > 1 ) { let div = primedivisor [ num ] ; divisors [ i ] . push ( div ) ; while ( num % div == 0 ) { num = Math . floor ( num / div ) ; } } } } function nearestGCD ( arr , n ) { findDivisors ( arr , n ) ; for ( let i = 0 ; i < n ; ++ i ) { for ( let div = 0 ; div < divisors [ i ] . length ; div ++ ) { if ( divInd [ divisors [ i ] [ div ] ] == - 1 ) { divInd [ divisors [ i ] [ div ] ] = i ; } else { let ind = divInd [ divisors [ i ] [ div ] ] ; divInd [ divisors [ i ] [ div ] ] = i ; if ( dist [ i ] > Math . abs ( ind - i ) ) { dist [ i ] = Math . abs ( ind - i ) ; pos [ i ] = ind + 1 ; } if ( dist [ ind ] > Math . abs ( ind - i ) ) { dist [ ind ] = Math . abs ( ind - i ) ; pos [ ind ] = i + 1 ; } } } } } for ( let i = 0 ; i < MAX ; i ++ ) { divisors . push ( [ ] ) ; } sieveOfEratosthenes ( ) ; let arr = [ 2 , 9 , 4 , 3 , 13 ] ; let n = arr . length ; nearestGCD ( arr , n ) ; for ( let i = 0 ; i < n ; ++ i ) { document . write ( pos [ i ] + " " ) ; }
Largest subsequence having GCD greater than 1 | Returns length of the largest subsequence with GCD more than 1. ; Finding the Maximum value in arr ; Iterate from 2 to maximum possible divisor of all give values ; If we found divisor , increment count ; Driver program to test above
function largestGCDSubsequence ( arr , n ) { var ans = 0 ; var maxele = Math . max ( ... arr ) ; for ( var i = 2 ; i <= maxele ; ++ i ) { var count = 0 ; for ( j = 0 ; j < n ; ++ j ) { if ( arr [ j ] % i == 0 ) ++ count ; } ans = Math . max ( ans , count ) ; } return ans ; } var arr = [ 3 , 6 , 2 , 5 , 4 ] ; var size = arr . length ; document . write ( largestGCDSubsequence ( arr , size ) ) ;
Minimum step to reach one | Structure represent one node in queue ; Method returns minimum step to reach one ; Set is used to visit numbers so that they won 't be pushed  in queue again ; Loop until we reach to 1 ; If current data value is 1 , return its steps from N ; Check curr - 1 , only if it not visited yet ; Loop from 2 to Math . sqrt ( value ) for its divisors ; Check divisor , only if it is not visited yet if i is divisor of val , then val / i will be its bigger divisor ; Driver code
class data { constructor ( val , steps ) { this . val = val ; this . steps = steps ; } } function minStepToReachOne ( N ) { let q = [ ] ; q . push ( new data ( N , 0 ) ) ; let st = new Set ( ) ; while ( q . length != 0 ) { let t = q . shift ( ) ; if ( t . val == 1 ) return t . steps ; if ( ! st . has ( t . val - 1 ) ) { q . push ( new data ( t . val - 1 , t . steps + 1 ) ) ; st . add ( t . val - 1 ) ; } for ( let i = 2 ; i * i <= t . val ; i ++ ) { if ( t . val % i == 0 && ! st . has ( t . val / i ) ) { q . push ( new data ( t . val / i , t . steps + 1 ) ) ; st . add ( t . val / i ) ; } } } return - 1 ; } let N = 17 ; document . write ( minStepToReachOne ( N ) + " " ) ;
Queries on the sum of prime factor counts in a range | Javascript program to find sum prime factors in given range . ; using sieve method to evaluating the prime factor of numbers ; if i is prime ; setting number of prime factor of a prime number . ; Returns sum of counts of prime factors in range from l to r . This function mainly uses count which is filled by Sieve ( ) ; finding the sum of number of prime factor of numbers in a range . ; Driver code
var MAX = 1000006 ; function sieve ( count ) { for ( i = 2 ; i * i <= MAX ; i ++ ) { if ( count [ i ] == 0 ) { for ( j = 2 * i ; j < MAX ; j += i ) count [ j ] ++ ; count [ i ] = 1 ; } } } function query ( count , l , r ) { var sum = 0 ; for ( i = l ; i <= r ; i ++ ) sum += count [ i ] ; return sum ; } var count = Array . from ( { length : MAX } , ( _ , i ) => 0 ) ; sieve ( count ) ; document . write ( query ( count , 6 , 10 ) + " " + query ( count , 1 , 5 ) ) ;
Generation of n numbers with given set of factors | Generate n numbers with factors in factor [ ] ; Array of k to store next multiples of given factors ; Prints n numbers let output = 0 ; Next number to print as output ; Find the next smallest multiple ; Printing minimum in each iteration print the value if output is not equal to current value ( to avoid the duplicates ) ; Incrementing the current value by the respective factor ; Driver code
function generateNumbers ( factor , n , k ) { let next = new Array ( k ) ; next . fill ( 0 ) ; for ( let i = 0 ; i < n ; ) { let toincrement = 0 ; for ( let j = 0 ; j < k ; j ++ ) if ( next [ j ] < next [ toincrement ] ) toincrement = j ; if ( output != next [ toincrement ] ) { output = next [ toincrement ] ; document . write ( next [ toincrement ] + " " ) ; i ++ ; } next [ toincrement ] += factor [ toincrement ] ; } } let factor = [ 3 , 5 , 7 ] ; let n = 10 ; let k = factor . length ; generateNumbers ( factor , n , k ) ;
Multiplicative order | function for GCD ; Function return smallest + ve integer that holds condition A ^ k ( mod N ) = 1 ; result store power of A that rised to the power N - 1 ; modular arithmetic ; return smallest + ve integer ; increment power ; Driver Code
function GCD ( a , b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } function multiplicativeOrder ( A , N ) { if ( GCD ( A , N ) != 1 ) return - 1 ; let result = 1 ; let K = 1 ; while ( K < N ) { result = ( result * A ) % N ; if ( result == 1 ) return K ; K ++ ; } return - 1 ; } let A = 4 , N = 7 ; document . write ( multiplicativeOrder ( A , N ) ) ;
Sum of product of x and y such that floor ( n / x ) = y | Return the sum of natural number in a range . ; n * ( n + 1 ) / 2. ; Return the sum of product x * y . ; Iterating i from 1 to sqrt ( n ) ; Finding the upper limit . ; Finding the lower limit . ; Driver Code
function sumOfRange ( a , b ) { let i = ( a * ( a + 1 ) ) >> 1 ; let j = ( b * ( b + 1 ) ) >> 1 ; return ( i - j ) ; } function sumofproduct ( n ) { let sum = 0 ; let root = Math . floor ( Math . sqrt ( n ) ) ; for ( let i = 1 ; i <= root ; i ++ ) { let up = Math . floor ( n / i ) ; let low = Math . max ( Math . floor ( n / ( i + 1 ) ) , root ) ; sum += ( i * sumOfRange ( up , low ) ) ; sum += ( i * Math . floor ( n / i ) ) ; } return sum ; } let n = 10 ; document . write ( sumofproduct ( n ) ) ;
Primitive root of a prime number n modulo n | Returns true if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Iterative Function to calculate ( x ^ n ) % p in O ( logy ) ; x = x % p ; 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 ; Utility function to store prime factors of a number ; Print the number of 2 s that divide n ; 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 find smallest primitive root of n ; Check if n is prime or not ; Find value of Euler Totient function of n Since n is a prime number , the value of Euler Totient function is n - 1 as there are n - 1 relatively prime numbers . ; Find prime factors of phi and store in a set ; Check for every number from 2 to phi ; Iterate through all prime factors of phi . and check if we found a power with value 1 ; Check if r ^ ( ( phi ) / primefactors ) mod n is 1 or not ; If there was no power with value 1. ; If no primitive root found ; Driver code
function isPrime ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( let i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } function power ( x , y , p ) { while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } function findPrimefactors ( s , n ) { while ( n % 2 == 0 ) { s . add ( 2 ) ; n = n / 2 ; } for ( let i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { s . add ( i ) ; n = n / i ; } } if ( n > 2 ) s . add ( n ) ; } function findPrimitive ( n ) { let s = new Set ( ) ; if ( isPrime ( n ) == false ) return - 1 ; let phi = n - 1 ; findPrimefactors ( s , phi ) ; for ( let r = 2 ; r <= phi ; r ++ ) { let flag = false ; for ( let it of s ) { if ( power ( r , phi / it , n ) == 1 ) { flag = true ; break ; } } if ( flag == false ) return r ; } return - 1 ; } let n = 761 ; document . write ( " " + n + " " + findPrimitive ( n ) ) ;
Minimum number of power terms with sum equal to n | Return minimum power terms of x required ; if x is 1 , return n since any power of 1 is 1 only . ; Consider n = a * x + b where a = n / x and b = n % x . ; Update count of powers for 1 's added ; Repeat the process for reduced n ; Driven Program
function minPower ( n , x ) { if ( x == 1 ) return n ; let ans = 0 ; while ( n > 0 ) { ans += ( n % x ) ; n = Math . floor ( n / x ) ; } return ans ; } let n = 5 , x = 3 ; document . write ( minPower ( n , x ) + " " ) ;
Sum of Perrin Numbers | function for sum of first n Perrin number . ; if ( n == 0 ) n = 0 ; if ( n == 1 ) n = 1 ; if ( n == 2 ) n = 2 ; calculate k = 5 sum of three previous step . ; Sum remaining numbers ; calculate next term ; Driver code
function calSum ( n ) { let a = 3 , b = 0 , c = 2 ; return 3 ; return 3 ; return 5 ; let sum = 5 ; while ( n > 2 ) { let d = a + b ; sum += d ; a = b ; b = c ; c = d ; n -- ; } return sum ; } let n = 9 ; document . write ( calSum ( n ) ) ;
Print the kth common factor of two numbers | Returns k 'th common factor of x and y. ; Find smaller of two numbers ; Count common factors until we either reach small or count becomes k . ; If we reached small ; Driver code
function findKHCF ( x , y , k ) { let small = Math . min ( x , y ) ; let count = 1 ; for ( let i = 2 ; i <= small ; i ++ ) { if ( x % i == 0 && y % i == 0 ) count ++ ; if ( count == k ) return i ; } return - 1 ; } let x = 4 , y = 24 , k = 3 ; document . write ( findKHCF ( x , y , k ) ) ;
Find minimum number to be divided to make a number a perfect square | Return the minimum number to be divided to make n a perfect square . ; Since 2 is only even prime , compute its power separately . ; If count is odd , it must be removed by dividing n by prime number . ; If count is odd , it must be removed by dividing n by prime number . ; Driver Code
function findMinNumber ( n ) { let count = 0 ; let ans = 1 ; while ( n % 2 == 0 ) { count ++ ; n /= 2 ; } if ( count % 2 ) ans *= 2 ; for ( let i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { count = 0 ; while ( n % i == 0 ) { count ++ ; n /= i ; } if ( count % 2 ) ans *= i ; } if ( n > 2 ) ans *= n ; return ans ; } let n = 72 ; document . write ( findMinNumber ( n ) + " " ) ;