codeparrot/xlcost-text-to-code huggingface dataset:
Find if it is possible to get a ratio from given ranges of costs and quantities | Returns true if it is possible to get ratio r from given cost and quantity ranges . ; Calculating cost corresponding to value of i ; Driver code
function isRatioPossible ( lowCost , upCost , lowQuant , upQuant , r ) { for ( let i = lowQuant ; i <= upQuant ; i ++ ) { let ans = i * r ; if ( lowCost <= ans && ans <= upCost ) return true ; } return false ; } let lowCost = 14 , upCost = 30 , lowQuant = 5 , upQuant = 12 , r = 9 ; if ( isRatioPossible ( lowCost , upCost , lowQuant , upQuant , r ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find N integers with given difference between product and sum | Function to implement calculation ; Driver code
function findNumbers ( n , d ) { for ( let i = 0 ; i < n - 2 ; i ++ ) document . write ( " " + " " ) ; document . write ( " " + " " ) ; document . write ( n + d ) ; } let N = 3 , D = 5 ; findNumbers ( N , D ) ;
Sum of fourth powers of first n odd natural numbers | calculate the sum of fourth power of first n odd natural numbers ; Driven Program
function oddNumSum ( n ) { return ( n * ( 2 * n + 1 ) * ( 24 * n * n * n - 12 * n * n - 14 * n + 7 ) ) / 15 ; } var n = 4 ; document . write ( oddNumSum ( n ) ) ;
Trailing number of 0 s in product of two factorials | Returns number of zeros in factorial n ; dividing x by powers of 5 and update count ; Returns count of trailing zeros in M ! x N ! ; Driver Code
function trailingZero ( x ) { let i = 5 ; let count = 0 ; while ( x > i ) { count = count + parseInt ( x / i ) ; i = i * 5 ; } return count ; } function countProductTrailing ( M , N ) { return trailingZero ( N ) + trailingZero ( M ) ; } let N = 67 ; let M = 98 ; document . write ( countProductTrailing ( N , M ) ) ;
Trimorphic Number | Function to check Trimorphic number ; Store the cube ; Start Comparing digits ; Return false , if any digit of N doesn ' t ▁ match ▁ with ▁ ▁ its ▁ cube ' s digits from last ; Reduce N and cube ; Driver code
function isTrimorphic ( N ) { let cube = N * N * N ; while ( N > 0 ) { if ( N % 10 != cube % 10 ) return false ; N = parseInt ( N / 10 , 10 ) ; cube = parseInt ( cube / 10 , 10 ) ; } return true ; } let N = 24 ; if ( isTrimorphic ( N ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
Trimorphic Number | JavaScript program for the above approach ; Functions to find nth Trimorphic number ; Comparing the digits ; Return false , if any digit of num doesn ' t ▁ ▁ match ▁ with ▁ its ▁ cube ' s digits from last ; Reduce num and cube ; Check in max let size ; check number is Trimorphic or not ; if counter is equal to the n then return nth number ; Driver Code
let LET_MAX = 2147483647 ; function checkTrimorphic ( num ) { let cube = num * num * num ; while ( num > 0 ) { if ( num % 10 != cube % 10 ) return false ; num = Math . floor ( num / 10 ) ; cube = Math . floor ( cube / 10 ) ; } return true ; } function nthTrimorphic ( n ) { let count = 0 ; for ( let i = 0 ; i < LET_MAX ; i ++ ) { if ( checkTrimorphic ( i ) ) count ++ ; if ( count == n ) return i ; } return - 1 ; } let n = 9 ; document . write ( nthTrimorphic ( n ) ) ;
Find minimum moves to reach target on an infinite line | JavaScript program to find minimum moves to reach target if we can move i steps in i - th move . ; Handling negatives by symmetry ; Keep moving while sum is smaller or difference is odd . ; Driver code
function reachTarget ( target ) { target = Math . abs ( target ) ; let sum = 0 , step = 0 ; while ( sum < target || ( sum - target ) % 2 != 0 ) { step ++ ; sum += step ; } return step ; } let target = 5 ; document . write ( reachTarget ( target ) ) ;
Sum of fifth powers of the first n natural numbers | calculate the sum of fifth power of first n natural numbers ; Driven Program
function fifthPowerSum ( n ) { return ( ( 2 * n * n * n * n * n * n ) + ( 6 * n * n * n * n * n ) + ( 5 * n * n * n * n ) - ( n * n ) ) / 12 ; } let n = 5 ; document . write ( fifthPowerSum ( n ) + " " ) ;
Find unit digit of x raised to power y | Returns unit digit of x raised to power y ; Initialize result as 1 to handle case when y is 0. ; One by one multiply with x mod 10 to avoid overflow . ; Driver Code
function unitDigitXRaisedY ( x , y ) { let res = 1 ; for ( let i = 0 ; i < y ; i ++ ) res = ( res * x ) % 10 ; return res ; } document . write ( unitDigitXRaisedY ( 4 , 2 ) ) ;
Max occurring divisor in an interval | function to find max occurring divisor interval [ x , y ] ; if there is only one number in the in the interval , return that number ; otherwise , 2 is the max occurring divisor ; Driver program
function findDivisor ( x , y ) { if ( x == y ) return y ; return 2 ; } var x = 3 , y = 16 ; document . write ( findDivisor ( x , y ) ) ;
Average of Squares of Natural Numbers | Function to calculate average of square number ; Driver code
function AvgofSquareN ( n ) { let sum = 0 ; for ( let i = 1 ; i <= n ; i ++ ) sum += ( i * i ) ; return sum / n ; } let n = 2 ; document . write ( AvgofSquareN ( n ) . toFixed ( 6 ) ) ;
Find sum of even factors of a number | Returns sum of all factors of n . ; If n is odd , then there are no even factors . ; Traversing through all prime factors . ; While i divides n , print i and divide n ; here we remove the 2 ^ 0 that is 1. All other factors ; This condition is to handle the case when n is a prime number . ; Driver Function
function sumofFactors ( n ) { if ( n % 2 != 0 ) return 0 ; let res = 1 ; for ( let i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { let count = 0 , curr_sum = 1 ; let curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; if ( i == 2 && count == 1 ) curr_sum = 0 ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; } if ( n >= 2 ) res *= ( 1 + n ) ; return res ; } let n = 18 ; document . write ( sumofFactors ( n ) ) ;
Find LCM of rational numbers | JAVASCRIPT program to find LCM of given array ; get lcm of two numbers ; Finds LCM of numerators ; calculate the lcm of all numerators ; return all numerator lcm ; Get GCD of all the denominators ; calculate the gcd of all the denominators ; return all denominator gcd ; find lcm of all the rational number ; return the LCM of all numerator / GCD of all denominator ; Driver code ; give rational number 2 / 7 , 3 / 14 , 5 / 3 make pair as a numerator and denominator
class pair { constructor ( first , second ) { this . first = first ; this . second = second ; } } function LCM ( a , b ) { return Math . floor ( ( a * b ) / ( __gcd ( a , b ) ) ) ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } function lcmOfNumerator ( vect ) { let lcm = vect [ 0 ] . first ; for ( let i = 1 ; i < vect . length ; i ++ ) lcm = LCM ( vect [ i ] . first , lcm ) ; return lcm ; } function gcdOfDemoninators ( vect ) { let gcd = vect [ 0 ] . second ; for ( let i = 1 ; i < vect . length ; i ++ ) gcd = __gcd ( vect [ i ] . second , gcd ) ; return gcd ; } function lcmOfRationals ( vect ) { document . write ( lcmOfNumerator ( vect ) + " " + gcdOfDemoninators ( vect ) ) ; } let vect = [ ] ; vect . push ( new pair ( 2 , 7 ) ) ; vect . push ( new pair ( 3 , 14 ) ) ; vect . push ( new pair ( 5 , 3 ) ) ; lcmOfRationals ( vect ) ;
Program to determine focal length of a spherical mirror | Determines focal length of a spherical concave mirror ; Determines focal length of a spherical convex mirror ; Driver Function
function focal_length_concave ( R ) { return R / 2 ; } function focal_length_convex ( R ) { return - ( R / 2 ) ; } let R = 30 ; document . write ( " " + " " + " " + focal_length_concave ( R ) + " " + " " ) ; document . write ( " " + " " + " " + focal_length_convex ( R ) + " " ) ;
Find sum of odd factors of a number | Returns sum of all factors of n . ; Traversing through all prime factors . ; ignore even factors by removing all powers of 2 ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number . ; Driver code
function sumofoddFactors ( n ) { let res = 1 ; while ( n % 2 == 0 ) n = n / 2 ; for ( let i = 3 ; i <= Math . sqrt ( n ) ; i ++ ) { let count = 0 ; let curr_sum = 1 ; let curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; } if ( n >= 2 ) res *= ( 1 + n ) ; return res ; } let n = 30 ; document . write ( sumofoddFactors ( n ) ) ;
Number of non | return number of non negative integral solutions ; initialize total = 0 ; Base Case if n = 1 and val >= 0 then it should return 1 ; iterate the loop till equal the val ; total solution of equations and again call the recursive function Solutions ( variable , value ) ; return the total no possible solution ; Driver code
function countSolutions ( n , val ) { let total = 0 ; if ( n == 1 && val >= 0 ) return 1 ; for ( let i = 0 ; i <= val ; i ++ ) { total += countSolutions ( n - 1 , val - i ) ; } return total ; } let n = 5 ; let val = 20 ; document . write ( countSolutions ( n , val ) ) ;
Fibonomial coefficient and Fibonomial triangle | Javascript Program to print Fibonomial Triangle of height n . ; Function to produce Fibonacci Series . ; * 0 th and 1 st number of the series are 0 and 1 ; * Add the previous 2 numbers in the series and store it ; Function to produce fibonomial coefficient ; Function to prvar Fibonomial Triangle . ; Finding the fibonacci series . ; to store triangle value . ; initialising the 0 th element of each row and diagonal element equal to 0. ; for each row . ; for each column . ; finding each element using recurrence relation . ; printing the Fibonomial Triangle . ; Driver code
var N = 6 ; function fib ( f , n ) { var i ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; for ( i = 2 ; i <= n ; i ++ ) f [ i ] = f [ i - 1 ] + f [ i - 2 ] ; } function fibcoef ( fc , f , n ) { for ( i = 0 ; i <= n ; i ++ ) fc [ i ] [ 0 ] = 1 ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= i ; j ++ ) { var k = j ; while ( k > 0 ) { k -- ; fc [ i ] [ j ] *= f [ k ] ; } k = 1 ; while ( ( j + 1 ) != k ) fc [ i ] [ j ] /= f [ k ++ ] ; } } } function printFibonomialTriangle ( n ) { var f = Array ( N + 1 ) . fill ( 0 ) ; fib ( f , n ) ; var dp = Array ( N + 1 ) ; for ( var i = 0 ; i < N + 1 ; i ++ ) dp [ i ] = Array ( N + 1 ) . fill ( 0 ) ; for ( i = 0 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = dp [ i ] [ i ] = 1 ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j < i ; j ++ ) dp [ i ] [ j ] = f [ i - j + 1 ] * dp [ i - 1 ] [ j - 1 ] + f [ j - 1 ] * dp [ i - 1 ] [ j ] ; } for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= i ; j ++ ) document . write ( dp [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } var n = 6 ; printFibonomialTriangle ( n ) ;
Sum of Arithmetic Geometric Sequence | Return the sum of first n term of AGP ; finding the each term of AGP and adding it to sum . ; Driver Code
function sumofNterm ( a , d , b , r , n ) { let sum = 0 ; for ( let i = 1 ; i <= n ; i ++ ) sum += ( ( a + ( i - 1 ) * d ) * ( b * Math . pow ( r , i - 1 ) ) ) ; return sum ; } let a = 1 ; let d = 1 ; let b = 2 ; let r = 2 ; let n = 3 ; document . write ( sumofNterm ( a , d , b , r , n ) ) ;
Sum of the series 2 + ( 2 + 4 ) + ( 2 + 4 + 6 ) + ( 2 + 4 + 6 + 8 ) + …… + ( 2 + 4 + 6 + 8 + … . + 2 n ) | function to find the sum of the given series ; first term of each i - th term ; next term ; required sum ; Driver program to test above
function sumOfTheSeries ( n ) { let sum = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { let k = 2 ; for ( let j = 1 ; j <= i ; j ++ ) { sum += k ; k += 2 ; } } return sum ; } let n = 5 ; document . write ( " " + sumOfTheSeries ( n ) ) ;
Program to find sum of series 1 * 2 * 3 + 2 * 3 * 4 + 3 * 4 * 5 + . . . + n * ( n + 1 ) * ( n + 2 ) | Function to calculate sum of series . ; Driver Code
function sumOfSeries ( n ) { let sum = 0 ; for ( let i = 1 ; i <= n ; i ++ ) sum = sum + i * ( i + 1 ) * ( i + 2 ) ; return sum ; } let n = 10 ; document . write ( sumOfSeries ( n ) ) ;
Program to get the Sum of series : 1 | Function to get the series ; Sum of n - 1 terms starting from 2 nd term ; Driver Code
function Series ( x , n ) { let sum = 1 , term = 1 , fct , j , y = 2 , m ; let i ; for ( i = 1 ; i < n ; i ++ ) { fct = 1 ; for ( j = 1 ; j <= y ; j ++ ) { fct = fct * j ; } term = term * ( - 1 ) ; m = term * Math . pow ( x , y ) / fct ; sum = sum + m ; y += 2 ; } return sum ; } let x = 9 ; let n = 10 ; document . write ( Series ( x , n ) . toFixed ( 4 ) ) ;
Program to get the Sum of series : 1 | Function to get the series ; Computing sum of remaining n - 1 terms . ; Driver Code
function Series ( x , n ) { var sum = 1 , term = 1 , fct = 1 ; var p = 1 , multi = 1 ; for ( let i = 1 ; i < n ; i ++ ) { fct = fct * multi * ( multi + 1 ) ; p = p * x * x ; term = ( - 1 ) * term ; multi += 2 ; sum = sum + ( term * p ) / fct ; } return sum ; } var x = 9 ; var n = 10 ; document . write ( Series ( x , n ) . toFixed ( 4 ) ) ;
Find the number of consecutive zero at the end after multiplying n numbers | Function to count two 's factor ; Count number of 2 s present in n ; Function to count five 's  factor ; Function to count number of zeros ; Count the two 's factor  of n number ; Count the five 's factor  of n number ; Return the minimum ; Driver code
function two_factor ( n ) { let twocount = 0 ; while ( n % 2 == 0 ) { twocount ++ ; n = n / 2 ; } return twocount ; } function five_factor ( n ) { let fivecount = 0 ; while ( n % 5 == 0 ) { fivecount ++ ; n = n / 5 ; } return fivecount ; } function find_con_zero ( arr , n ) { let twocount = 0 ; let fivecount = 0 ; for ( let i = 0 ; i < n ; i ++ ) { twocount += two_factor ( arr [ i ] ) ; fivecount += five_factor ( arr [ i ] ) ; } if ( twocount < fivecount ) return twocount ; else return fivecount ; } let arr = [ 100 , 10 , 5 , 25 , 35 , 14 ] ; let n = 6 ; document . write ( find_con_zero ( arr , n ) ) ;
First occurrence of a digit in a given fraction | Function to print the first digit ; Reduce the number to its mod ; Traverse for every decimal places ; Get every fraction places when ( a * 10 / b ) / c ; Check if it is equal to the required integer ; Mod the number ; Driver Code
function first ( a , b , c ) { a %= b ; for ( let i = 1 ; i <= b ; i ++ ) { a = a * 10 ; if ( a / b == c ) return i ; a %= b ; } return - 1 ; } let a = 1 , b = 4 , c = 5 ; document . write ( first ( a , b , c ) ) ;
Minimize the absolute difference of sum of two subsets | function to print difference ; summation of n elements ; if divisible by 4 ; if remainder 1 or 2. In case of remainder 2 , we divide elements from 3 to n in groups of size 4 and put 1 in one group and 2 in group . This also makes difference 1. ; We put elements from 4 to n in groups of size 4. Remaining elements 1 , 2 and 3 can be divided as ( 1 , 2 ) and ( 3 ) . ; Driver code
function subsetDifference ( n ) { let s = n * ( n + 1 ) / 2 ; if ( n % 4 == 0 ) { document . write ( " " + Math . floor ( s / 2 ) ) ; document . write ( " " + Math . floor ( s / 2 ) ) ; document . write ( " " + 0 ) ; } else { if ( n % 4 == 1 n % 4 == 2 ) { document . write ( " " + Math . floor ( s / 2 ) ) ; document . write ( " " + Math . floor ( s / 2 + 1 ) ) ; document . write ( " " + 1 ) ; } else { document . write ( " " + Math . floor ( s / 2 ) ) ; document . write ( " " + Math . floor ( s / 2 ) ) ; document . write ( " " + 0 ) ; } } } let n = 6 ; subsetDifference ( n ) ;
Time required to meet in equilateral triangle | function to calculate time to meet ; Driver Code
function timeToMeet ( s , v ) { var V = 3 * v / 2 ; var time = s / V ; document . write ( time . toFixed ( 6 ) ) ; } var s = 25 , v = 56 ; timeToMeet ( s , v ) ;
Sum of the series 1 + ( 1 + 3 ) + ( 1 + 3 + 5 ) + ( 1 + 3 + 5 + 7 ) + â €¦ â €¦ + ( 1 + 3 + 5 + 7 + â €¦ + ( 2 n | functionn to find the sum of the given series ; first term of each i - th term ; next term ; required sum ; Driver program
function sumOfTheSeries ( n ) { let sum = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { let k = 1 ; for ( let j = 1 ; j <= i ; j ++ ) { sum += k ; k += 2 ; } } return sum ; } let n = 5 ; document . write ( " " + sumOfTheSeries ( n ) ) ;
Check if a number can be written as sum of three consecutive integers | function to check if a number can be written as sum of three consecutive integer . ; if n is 0 ; if n is positive , increment loop by 1. ; if n is negative , decrement loop by 1. ; Running loop from 0 to n - 2 ; check if sum of three consecutive integer is equal to n . ; Driver program to test above function
function checksum ( n ) { if ( n == 0 ) { document . write ( " " ) ; return ; } var inc ; if ( n > 0 ) inc = 1 ; else inc = - 1 ; for ( i = 0 ; i <= n - 2 ; i += inc ) { if ( i + i + 1 + i + 2 == n ) { document . write ( i + " " + ( i + 1 ) + " " + ( i + 2 ) ) ; return ; } } document . write ( " " ) ; } var n = 6 ; checksum ( n ) ;
Sum of all divisors from 1 to n | Utility function to find sum of all divisor of number up to ' n ' ; Find all divisors of i and add them ; Driver code
function divisorSum ( n ) { let sum = 0 ; for ( let i = 1 ; i <= n ; ++ i ) { for ( let j = 1 ; j * j <= i ; ++ j ) { if ( i % j == 0 ) { if ( i / j == j ) sum += j ; else sum += j + i / j ; } } } return sum ; } let n = 4 ; document . write ( divisorSum ( n ) + " " ) ; n = 5 ; document . write ( divisorSum ( n ) + " " ) ;
Program for Binomial Coefficients table | Function to print binomial table ; B ( m , x ) is 1 if either m or x is 0. ; Otherwise using recursive formula B ( m , x ) = B ( m , x - 1 ) * ( m - x + 1 ) / x ; Driver Code
function printbinomial ( max ) { for ( let m = 0 ; m <= max ; m ++ ) { document . write ( m ) ; let binom = 1 ; for ( let x = 0 ; x <= m ; x ++ ) { if ( m != 0 && x != 0 ) binom = binom * ( m - x + 1 ) / x ; document . write ( " " + binom + " " ) ; } document . write ( " " ) ; } } let max = 10 ; printbinomial ( max ) ;
Count unset bits in a range | Function to get no of set bits in the binary representation of ' n ' ; function to count unset bits in the given range ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; returns number of unset bits in the range ' l ' to ' r ' in ' n ' ; Driver program to test above
function countSetBits ( n ) { var count = 0 ; while ( n ) { n &= ( n - 1 ) ; count ++ ; } return count ; } function countUnsetBitsInGivenRange ( n , l , r ) { var num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; return ( r - l + 1 ) - countSetBits ( n & num ) ; } var n = 80 ; var l = 1 , r = 4 ; document . write ( countUnsetBitsInGivenRange ( n , l , r ) ) ;
Sum of fourth power of first n even natural numbers | calculate the sum of fourth power of first n even natural numbers ; made even number ; Driven Program
function evenPowerSum ( n ) { let sum = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { let j = 2 * i ; sum = sum + ( j * j * j * j ) ; } return sum ; } let n = 5 ; document . write ( evenPowerSum ( n ) ) ;
Sum of fourth power of first n even natural numbers | calculate the sum of fourth power of first n even natural numbers ; Driven Program
function evenPowerSum ( n ) { return ( 8 * n * ( n + 1 ) * ( 2 * n + 1 ) * ( 3 * n * n + 3 * n - 1 ) ) / 15 ; } var n = 4 ; document . write ( evenPowerSum ( n ) ) ;
Balanced Prime | Javascript Program to find Nth Balanced Prime ; Return the Nth balanced prime . ; Sieve of Eratosthenes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; storing all primes ; Finding the Nth balanced Prime ; Driven Program
var MAX = 501 ; function balancedprime ( n ) { var prime = Array ( MAX + 1 ) . fill ( true ) ; for ( var p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( var i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } var v = [ ] ; for ( var p = 3 ; p <= MAX ; p += 2 ) if ( prime [ p ] ) v . push ( p ) ; var count = 0 ; for ( var i = 1 ; i < v . length ; i ++ ) { if ( v [ i ] == ( v [ i + 1 ] + v [ i - 1 ] ) / 2 ) count ++ ; if ( count == n ) return v [ i ] ; } } var n = 4 ; document . write ( balancedprime ( n ) ) ;
Smallest integer which has n factors or more | javascript program to prvar the smallest integer with n factors or more ; array to store prime factors ; function to generate all prime factors of numbers from 1 to 10 ^ 6 ; Initializes all the positions with their value . ; Initializes all multiples of 2 with 2 ; A modified version of Sieve of Eratosthenes to store the smallest prime factor that divides every number . ; check if it has no prime factor . ; Initializes of j starting from i * i ; if it has no prime factor before , then stores the smallest prime divisor ; function to calculate number of factors ; stores the smallest prime number that divides n ; stores the count of number of times a prime number divides n . ; reduces to the next number after prime factorization of n ; false when prime factorization is done ; if the same prime number is dividing n , then we increase the count ; * if its a new prime factor that is factorizing n , then we again set c = 1 and * change dup to the new prime factor , and apply the formula explained above . ; prime factorizes a number ; for the last prime factor ; function to find the smallest integer with n factors or more . ; check if no of factors is more than n or not ; generate prime factors of number upto 10 ^ 6
var MAX = 1000001 ; var factor = Array ( MAX ) . fill ( 0 ) ; function generatePrimeFactors ( ) { factor [ 1 ] = 1 ; for ( i = 2 ; i < MAX ; i ++ ) factor [ i ] = i ; for ( i = 4 ; i < MAX ; i += 2 ) factor [ i ] = 2 ; for ( i = 3 ; i * i < MAX ; i ++ ) { if ( factor [ i ] == i ) { for ( j = i * i ; j < MAX ; j += i ) { if ( factor [ j ] == j ) factor [ j ] = i ; } } } } function calculateNoOFactors ( n ) { if ( n == 1 ) return 1 ; var ans = 1 ; var dup = factor [ n ] ; var c = 1 ; var j = n / factor [ n ] ; while ( j != 1 ) { if ( factor [ j ] == dup ) c += 1 ; else { dup = factor [ j ] ; ans = ans * ( c + 1 ) ; c = 1 ; } j = j / factor [ j ] ; } ans = ans * ( c + 1 ) ; return ans ; } function smallest ( n ) { for ( i = 1 ; ; i ++ ) if ( calculateNoOFactors ( i ) >= n ) return i ; } generatePrimeFactors ( ) ; var n = 4 ; document . write ( smallest ( n ) ) ;
Sum of squares of first n natural numbers | Return the sum of square of first n natural numbers ; Iterate i from 1 and n finding square of i and add to sum . ; Driven Program
function squaresum ( n ) { let sum = 0 ; for ( let i = 1 ; i <= n ; i ++ ) sum += ( i * i ) ; return sum ; } let n = 4 ; document . write ( squaresum ( n ) + " " ) ;
Break a number such that sum of maximum divisors of all parts is minimum | Function to check if a number is prime or not . ; If n is an even number ( we can write it as sum of two primes ) ; If n is odd and n - 2 is prime . ; If n is odd , n - 3 must be even . ; Driver code
function isPrime ( n ) { let i = 2 ; while ( i * i <= n ) { if ( n % i == 0 ) return false ; i ++ ; } return true ; } function minimumSum ( n ) { if ( isPrime ( n ) ) return 1 ; if ( n % 2 == 0 ) return 2 ; if ( isPrime ( n - 2 ) ) return 2 ; return 3 ; } let n = 27 ; document . write ( minimumSum ( n ) ) ;
Find first and last digits of a number | Find the first digit ; Remove last digit from number till only one digit is left ; return the first digit ; Find the last digit ; return the last digit ; Driver code
function firstDigit ( n ) { while ( n >= 10 ) n /= 10 ; return Math . floor ( n ) ; } function lastDigit ( n ) { return Math . floor ( n % 10 ) ; } let n = 98562 ; document . write ( firstDigit ( n ) + " " + lastDigit ( n ) ) ;
Express an odd number as sum of prime numbers | Function to check if a number is prime or not . ; Prlets at most three prime numbers whose sum is n . ; if ( isPrime ( n ) ) CASE - I ; else if ( isPrime ( n - 2 ) ) CASE - II ; else CASE - III ; Driver code
function isPrime ( x ) { if ( x == 0 x == 1 ) return false ; for ( let i = 2 ; i * i <= x ; ++ i ) if ( x % i == 0 ) return false ; return true ; } function findPrimes ( n ) { document . write ( n ) ; document . write ( 2 + " " + ( n - 2 ) ) ; { document . write ( 3 + " " ) ; n = n - 3 ; for ( let i = 0 ; i < n ; i ++ ) { if ( isPrime ( i ) && isPrime ( n - i ) ) { document . write ( i + " " + ( n - i ) ) ; break ; } } } } let n = 27 ; findPrimes ( n ) ;