codeparrot/xlcost-text-to-code huggingface dataset:
Number with even sum of digits | Function to find kth good number . ; Find the last digit of n . ; If last digit is between 0 to 4 then return 2 * n . ; If last digit is between 5 to 9 then return 2 * n + 1. ; Driver code
function findKthGoodNo ( n ) { let lastDig = n % 10 ; if ( lastDig >= 0 && lastDig <= 4 ) return n << 1 ; else return ( n << 1 ) + 1 ; } let n = 10 ; document . write ( findKthGoodNo ( n ) ) ;
Nicomachu 's Theorem | JavaScript program to verify Nicomachu 's Theorem ; Compute sum of cubes ; Check if sum is equal to given formula . ; Driver code
function NicomachuTheorum_sum ( n ) { let sum = 0 ; for ( let k = 1 ; k <= n ; k ++ ) sum += k * k * k ; let triNo = n * ( n + 1 ) / 2 ; if ( sum == triNo * triNo ) document . write ( " " ) ; else document . write ( " " ) ; } let n = 5 ; NicomachuTheorum_sum ( n ) ;
Largest even digit number not greater than N | function to check if all digits are even of a given number ; iterate for all digits ; if digit is odd ; all digits are even ; function to return the largest number with all digits even ; iterate till we find a number with all digits even ; Driver Code
function checkDigits ( n ) { while ( n > 0 ) { if ( ( ( n % 10 ) % 2 ) > 0 ) return 0 ; n = parseInt ( n / 10 ) ; } return 1 ; } function largestNumber ( n ) { for ( i = n ; ; i -- ) if ( checkDigits ( i ) > 0 ) return i ; } var N = 23 ; document . write ( largestNumber ( N ) ) ;
Largest even digit number not greater than N | function to return the largest number with all digits even ; convert the number to a string for easy operations ; find first odd digit ; if no digit , then N is the answer ; till first odd digit , add all even numbers ; decrease 1 from the odd digit ; add 0 in the rest of the digits ; Driver Code
function largestNumber ( n ) { var s = " " ; var duplicate = n ; while ( n > 0 ) { s = String . fromCharCode ( n % 10 + 48 ) + s ; n = parseInt ( n / 10 ) ; } var index = - 1 ; for ( i = 0 ; i < s . length ; i ++ ) { if ( ( ( ( s . charAt ( i ) . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) % 2 ) & 1 ) > 0 ) { index = i ; break ; } } if ( index == - 1 ) return duplicate ; var num = 0 ; for ( i = 0 ; i < index ; i ++ ) num = num * 10 + ( s . charAt ( i ) . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; num = num * 10 + ( s . charAt ( index ) . charCodeAt ( 0 ) - ( ' ' ) . charCodeAt ( 0 ) - 1 ) ; for ( i = index + 1 ; i < s . length ; i ++ ) num = num * 10 + 8 ; return num ; } var N = 24578 ; document . write ( largestNumber ( N ) ) ;
Number of digits in 2 raised to power n | Function to find number of digits in 2 ^ n ; Driver code
function countDigits ( n ) { return ( n * Math . log10 ( 2 ) + 1 ) ; } let n = 5 ; document . write ( Math . floor ( countDigits ( n ) ) ) ;
Smallest even digits number not less than N | function to check if all digits are even of a given number ; iterate for all digits ; if digit is odd ; all digits are even ; function to return the smallest number with all digits even ; iterate till we find a number with all digits even ; Driver Code
function check_digits ( n ) { while ( n != 0 ) { if ( ( n % 10 ) % 2 != 0 ) return 0 ; n = parseInt ( n / 10 ) ; } return 1 ; } function smallest_number ( n ) { for ( i = n ; ; i ++ ) if ( check_digits ( i ) != 0 ) return i ; } var N = 2397 ; document . write ( smallest_number ( N ) ) ;
Smallest triangular number larger than p | javascript code to find the bucket to choose for picking flowers out of it ; Driver code
function findBucketNo ( p ) { return Math . ceil ( ( Math . sqrt ( 8 * p + 1 ) - 1 ) / 2 ) ; } let p = 10 ; document . write ( findBucketNo ( p ) ) ;
LCM of factorial and its neighbors | function to calculate the factorial ; returning the factorial of the largest number in the given three consecutive numbers ; Driver code
function factorial ( n ) { if ( n == 0 ) return 1 ; return n * factorial ( n - 1 ) ; } function LCMOfNeighbourFact ( n ) { return factorial ( n + 1 ) ; } var N = 5 ; document . write ( LCMOfNeighbourFact ( N ) ) ;
Expressing factorial n as sum of consecutive numbers | Javascript program to count number of ways we can express a factorial as sum of consecutive numbers ; sieve of Eratosthenes to compute the prime numbers ; Store all prime numbers ; function to calculate the largest power of a prime in a number ; Modular multiplication to avoid the overflow of multiplication Please see below for details https : www . geeksforgeeks . org / how - to - avoid - overflow - in - modular - multiplication / ; Returns count of ways to express n ! as sum of consecutives . ; We skip 2 ( First prime ) as we need to consider only odd primes ; compute the largest power of prime ; if the power of current prime number is zero in N ! , power of primes greater than current prime number will also be zero , so break out from the loop ; multiply the result at every step ; subtract 1 to exclude the case of 1 being an odd divisor ; Driver function
let MAX = 50002 ; let primes = [ ] ; function sieve ( ) { let isPrime = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) isPrime [ i ] = true ; for ( let p = 2 ; p * p < MAX ; p ++ ) { if ( isPrime [ p ] == true ) { for ( let i = p * 2 ; i < MAX ; i += p ) isPrime [ i ] = false ; } } for ( let p = 2 ; p < MAX ; p ++ ) if ( isPrime [ p ] == true ) primes . push ( p ) ; } function power ( x , y ) { let count = 0 ; let z = y ; while ( x >= z ) { count += Math . floor ( x / z ) ; z *= y ; } return count ; } function modMult ( a , b , mod ) { let res = 0 ; a = a % mod ; while ( b > 0 ) { if ( b % 2 == 1 ) res = ( res + a ) % mod ; a = ( a * 2 ) % mod ; b = Math . floor ( b / 2 ) ; } return res % mod ; } function countWays ( n , m ) { let ans = 1 ; for ( let i = 1 ; i < primes . length ; i ++ ) { let powers = power ( n , primes [ i ] ) ; if ( powers == 0 ) break ; ans = modMult ( ans , powers + 1 , m ) % m ; } if ( ( ( ans - 1 ) % m ) < 0 ) return ( ans - 1 + m ) % m ; else return ( ans - 1 ) % m ; } sieve ( ) ; let n = 4 , m = 7 ; document . write ( countWays ( n , m ) ) ;
Check if the given two numbers are friendly pair or not | Returns sum of all factors of n . ; Traversing through all prime factors . ; THE BELOW STATEMENT MAKES IT BETTER THAN ABOVE METHOD AS WE REDUCE VALUE OF n . ; This condition is to handle the case when n is a prime number greater than 2. ; Function to return gcd of a and b ; Function to check if the given two number are friendly pair or not . ; Finding the sum of factors of n and m ; finding gcd of n and sum of its factors . ; finding gcd of m and sum of its factors . ; checking is numerator and denominator of abundancy index of both number are equal or not . ; Driver Code
function sumofFactors ( n ) { 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 ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; } if ( n >= 2 ) res *= ( 1 + n ) ; return res ; } function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function checkFriendly ( n , m ) { let sumFactors_n = sumofFactors ( n ) ; let sumFactors_m = sumofFactors ( m ) ; let gcd_n = gcd ( n , sumFactors_n ) ; let gcd_m = gcd ( m , sumFactors_m ) ; if ( n / gcd_n == m / gcd_m && sumFactors_n / gcd_n == sumFactors_m / gcd_m ) return true ; else return false ; } let n = 6 , m = 28 ; if ( checkFriendly ( n , m ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find n | JavaScript program to find n - th Fortunate number ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to Find primorial of order n ( product of first n prime numbers ) . ; Function to find next prime number greater than n ; Note that difference ( or m ) should be greater than 1. ; loop continuously until isPrime returns true for a number above n ; Ignoring the prime number that is 1 greater than n ; Returns n - th Fortunate number ; 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 primorial ( n ) { let p = 2 ; n -- ; for ( let i = 3 ; n != 0 ; i ++ ) { if ( isPrime ( i ) == true ) { p = p * i ; n -- ; } i ++ ; } return p ; } function findNextPrime ( n ) { let nextPrime = n + 2 ; while ( true ) { if ( isPrime ( nextPrime ) == true ) break ; nextPrime ++ ; } return nextPrime ; } function fortunateNumber ( n ) { let p = primorial ( n ) ; return findNextPrime ( p ) - p ; } let n = 5 ; document . write ( fortunateNumber ( n ) ) ;
Probability for three randomly chosen numbers to be in AP | function to calculate probability ; Driver code
function procal ( n ) { return ( 3.0 * n ) / ( 4.0 * ( n * n ) - 1 ) ; } let a = [ 1 , 2 , 3 , 4 , 5 ] ; let n = a . length ; document . write ( procal ( n ) ) ;
Fermat 's Last Theorem | JavaScript program to verify fermat 's last theorem for a given range and n. ; Check if there exists a triplet such that a ^ n + b ^ n = c ^ n ; Driver Code
function testSomeNumbers ( limit , n ) { if ( n < 3 ) return ; for ( let a = 1 ; a <= limit ; a ++ ) for ( let b = a ; b <= limit ; b ++ ) { let pow_sum = ( Math . pow ( a , n ) + Math . pow ( b , n ) ) ; let c = Math . pow ( pow_sum , 1.0 / n ) ; let c_pow = Math . pow ( Math . round ( c ) , n ) ; if ( c_pow == pow_sum ) { document . write ( " " ) ; return ; } } document . write ( " " + " " ) ; } testSomeNumbers ( 12 , 5 ) ;
Product of given N fractions in reduced form | Function to return gcd of a and b ; Print the Product of N fraction in Reduced Form . ; finding the product of all N numerators and denominators . ; Finding GCD of new numerator and denominator ; Converting into reduced form . ; Driven Program
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function productReduce ( n , num , den ) { let new_num = 1 , new_den = 1 ; for ( let i = 0 ; i < n ; i ++ ) { new_num *= num [ i ] ; new_den *= den [ i ] ; } let GCD = gcd ( new_num , new_den ) ; new_num /= GCD ; new_den /= GCD ; document . write ( new_num + " " + new_den ) ; } let n = 3 ; let num = [ 1 , 2 , 5 ] ; let den = [ 2 , 1 , 6 ] ; productReduce ( n , num , den ) ;
Find value of ( n ^ 1 + n ^ 2 + n ^ 3 + n ^ 4 ) mod 5 for given n | function for f ( n ) mod 5 ; if n % 5 == 1 return 4 ; else return 0 ; Driver Code
function fnMod ( n ) { if ( n % 5 == 1 ) return 4 ; else return 0 ; } let n = 10 ; document . write ( fnMod ( n ) + " " ) ; n = 11 ; document . write ( fnMod ( n ) + " " ) ;
Recursive sum of digits of a number formed by repeated appends | Return single digit sum of a number . ; Returns recursive sum of digits of a number formed by repeating a number X number of times until sum become single digit . ; Driver Code
function digSum ( n ) { if ( n == 0 ) return 0 ; return ( n % 9 == 0 ) ? 9 : ( n % 9 ) ; } function repeatedNumberSum ( n , x ) { sum = x * digSum ( n ) ; return digSum ( sum ) ; } let n = 24 ; let x = 3 ; document . write ( repeatedNumberSum ( n , x ) ) ;
Sum of n digit numbers divisible by a given number | Returns sum of n digit numbers divisible by ' number ' ; compute the first and last term ; sum of number which having n digit and divisible by number ; Driver Code
function totalSumDivisibleByNum ( n , number ) { let firstnum = Math . pow ( 10 , n - 1 ) ; let lastnum = Math . pow ( 10 , n ) ; let sum = 0 ; for ( let i = firstnum ; i < lastnum ; i ++ ) if ( i % number == 0 ) sum += i ; return sum ; } let n = 3 , num = 7 ; document . write ( totalSumDivisibleByNum ( n , num ) ) ;
Count n digit numbers divisible by given number | Returns count of n digit numbers divisible by ' number ' ; compute the first and last term ; count total number of which having n digit and divisible by number ; Driver Code
function numberofterm ( n , number ) { let firstnum = Math . pow ( 10 , n - 1 ) ; let lastnum = Math . pow ( 10 , n ) ; let count = 0 ; for ( let i = firstnum ; i < lastnum ; i ++ ) if ( i % number == 0 ) count ++ ; return count ; } let n = 3 , num = 7 ; document . write ( numberofterm ( n , num ) ) ;
N | JavaScript program to find N - th term in George Cantor set of rational number ; let i = numerator ; let j = denominator ; to keep the check of no . of terms ; loop till k is not equal to n ; check if k is already equal to N then the first term is the required rational number ; loop for traversing from right to left downwards diagonally ; loop for traversing from left to right upwards diagonally ; Driver Code
function georgeCantor ( n ) { let i = 1 ; let j = 1 ; let k = 1 ; while ( k < n ) { j ++ ; k ++ ; if ( k == n ) break ; while ( j > 1 && k < n ) { i ++ ; j -- ; k ++ ; } if ( k == n ) break ; i ++ ; k ++ ; if ( k == n ) break ; while ( i > 1 && k < n ) { i -- ; j ++ ; k ++ ; } } document . write ( " " + i + " " + j ) ; } let n = 15 ; georgeCantor ( n ) ;
Number is divisible by 29 or not | Returns true if n is divisible by 29 else returns false . ; add the lastdigit * 3 to remaining number until number becomes of only 2 digit ; return true if number is divisible by 29 another ; Driver Code
function isDivisible ( n ) { while ( parseInt ( n / 100 ) ) { let last_digit = n % 10 ; n = parseInt ( n / 10 ) ; n += last_digit * 3 ; } return ( n % 29 == 0 ) ; } let n = 348 ; if ( isDivisible ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if a given number is Pronic | Efficient Approach | function to check Pronic Number ; Checking Pronic Number by multiplying consecutive numbers ; Driver Code
function pronic_check ( n ) { var x = parseInt ( Math . sqrt ( n ) ) ; if ( x * ( x + 1 ) == n ) return true ; else return false ; } var n = 56 ; pronic_check ( n ) == true ? document . write ( " " ) : document . write ( " " ) ;
Check if given number is perfect square | Javascript program for the above approach ; If ceil and floor are equal the number is a perfect square ; Driver Code
function checkperfectsquare ( n ) { if ( Math . ceil ( Math . sqrt ( n ) ) == Math . floor ( Math . sqrt ( n ) ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let n = 49 ; checkperfectsquare ( n ) ;
Writing power function for large numbers | Maximum number of digits in output ; This function multiplies x with the number represented by res [ ] . res_size is size of res [ ] or number of digits in the number represented by res [ ] . This function uses simple school mathematics for multiplication . This function may value of res_size and returns the new value of res_size ; Initialize carry ; One by one multiply n with individual digits of res [ ] ; Store last digit of ' prod ' in res [ ] ; Put rest in carry ; Put carry in res and increase result size ; This function finds power of a number x ; printing value "1" for power = 0 ; Initialize result ; Multiply x n times ( x ^ n = x * x * x ... . n times ) ; Driver Code
let MAX = 100000 function multiply ( x , res , res_size ) { let carry = 0 ; for ( let i = 0 ; i < res_size ; i ++ ) { let prod = res [ i ] * x + carry ; res [ i ] = prod % 10 ; carry = Math . floor ( prod / 10 ) ; } while ( carry ) { res [ res_size ] = carry % 10 ; carry = Math . floor ( carry / 10 ) ; res_size ++ ; } return res_size ; } function power ( x , n ) { if ( n == 0 ) { document . write ( " " ) ; return ; } let res = new Array ( MAX ) ; let res_size = 0 ; let temp = x ; while ( temp != 0 ) { res [ res_size ++ ] = temp % 10 ; temp = Math . floor ( temp / 10 ) ; } for ( let i = 2 ; i <= n ; i ++ ) res_size = multiply ( x , res , res_size ) ; document . write ( x + " " + n + " " ) ; for ( let i = res_size - 1 ; i >= 0 ; i -- ) document . write ( res [ i ] ) ; } let exponent = 100 ; let base = 2 ; power ( base , exponent ) ;
P | function to check if number n is a P - smooth number or not ; prime factorise it by 2 ; if the number is divisible by 2 ; check for all the possible numbers that can divide it ; prime factorize it by i ; stores the maximum if maximum and i , if i divides the number ; if n at the end is a prime number , then it a divisor itself ; Driver Code
function check ( n , p ) { let maximum = - 1 ; while ( ! ( n % 2 ) ) { maximum = Math . max ( maximum , 2 ) n = n / 2 ; } var i ; for ( i = 3 ; i * i <= n ; i += 2 ) { while ( n % i == 0 ) { maximum = Math . max ( maximum , i ) ; n = n / i ; } } if ( n > 2 ) maximum = Math . max ( maximum , n ) ; if ( maximum <= p ) return true ; else return false ; } let n = 24 , p = 7 ; if ( check ( n , p ) ) document . write ( " " ) ; else document . write ( " " ) ;
Program to Change RGB color model to HSV color model | javascript program change RGB Color Model to HSV Color Model ; R , G , B values are divided by 255 to change the range from 0. .255 to 0. .1 ; h , s , v = hue , saturation , value var cmax = Math . max ( r , Math . max ( g , b ) ) ; maximum of r , g , b var cmin = Math . min ( r , Math . min ( g , b ) ) ; minimum of r , g , b var diff = cmax - cmin ; diff of cmax and cmin . ; if cmax and cmax are equal then h = 0 ; if cmax equal r then compute h ; if cmax equal g then compute h ; if cmax equal b then compute h ; if cmax equal zero ; compute v ; rgb_to_hsv ( 45 , 215 , 0 ) ; rgb_to_hsv ( 31 , 52 , 29 ) ;
function rgb_to_hsv ( r , g , b ) { r = r / 255.0 ; g = g / 255.0 ; b = b / 255.0 ; var h = - 1 , s = - 1 ; if ( cmax == cmin ) h = 0 ; else if ( cmax == r ) h = ( 60 * ( ( g - b ) / diff ) + 360 ) % 360 ; else if ( cmax == g ) h = ( 60 * ( ( b - r ) / diff ) + 120 ) % 360 ; else if ( cmax == b ) h = ( 60 * ( ( r - g ) / diff ) + 240 ) % 360 ; if ( cmax == 0 ) s = 0 ; else s = ( diff / cmax ) * 100 ; var v = cmax * 100 ; document . write ( " " + h . toFixed ( 1 ) + " " + s + " " + v + " " ) ; } rgb_to_hsv ( 129 , 88 , 47 ) ;
Time when minute hand and hour hand coincide | function to find the minute ; finding the angle between minute hand and the first hour hand ; Driver code
function find_time ( h1 ) { theta = 30 * h1 ; document . write ( " " + ( theta * 2 ) + " " + " " + " " + " " ) ; } h1 = 3 ; find_time ( h1 ) ;
Sum of Series ( n ^ 2 | function that calculate the sum of the nth series ; using formula of the nth term ; Driver code
function sum_series ( n ) { let nSquare = n * n ; return nSquare * ( nSquare - 1 ) / 4 ; } let n = 2 ; document . write ( sum_series ( n ) ) ;
Check if a number is sandwiched between primes | returns true if number n is prime ; 0 and 1 both are non - primes ; finding square root of n ; checking if n has any factor upto square root of n if yes its not prime ; Driver code
function isPrime ( n ) { if ( n == 0 n == 1 ) return false ; let root = Math . sqrt ( n ) ; for ( let i = 2 ; i <= root ; i ++ ) if ( n % i == 0 ) return false ; return true ; } function isSandwitched ( n ) { return ( isPrime ( n - 1 ) && isPrime ( n + 1 ) ) ; } let n = 642 ; document . write ( n + " " ) ; if ( isSandwitched ( n ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; n = 9 ; document . write ( n + " " ) ; if ( isSandwitched ( n ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;
Tomohiko Sakamoto 's Algorithm | function to implement tomohiko sakamoto algorithm ; array with leading number of days values ; if month is less than 3 reduce year by 1 ; Driver code
function day_of_the_week ( y , m , d ) { let t = [ 0 , 3 , 2 , 5 , 0 , 3 , 5 , 1 , 4 , 6 , 2 , 4 ] ; if ( m < 3 ) y -= 1 ; return ( y + y / 4 - y / 100 + y / 400 + t [ m - 1 ] + d ) % 7 ; } let day = 13 , month = 7 , year = 2017 ; document . write ( Math . round ( day_of_the_week ( year , month , day ) ) ) ;
Recursive program for prime number | Returns true if n is prime , else return false . i is current divisor to check . ; Base cases ; Check for next divisor ; Driver code
function isPrime ( n , i ) { if ( n <= 2 ) return ( n == 2 ) ? true : false ; if ( n % i == 0 ) return false ; if ( i * i > n ) return true ; return isPrime ( n , i + 1 ) ; } let n = 15 ; if ( isPrime ( n , 2 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Square Free Number | Returns true if n is a square free number , else returns false . ; If 2 again divides n , then n is not a square free number . ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; Check if i is a prime factor ; If i again divides , then n is not square free ; Driver code
function isSquareFree ( n ) { if ( n % 2 == 0 ) n = n / 2 ; if ( n % 2 == 0 ) return false ; for ( let i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { n = n / i ; if ( n % i == 0 ) return false ; } } return true ; } let n = 10 ; if ( isSquareFree ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Area of a square from diagonal length | Returns area of square from given diagonal ; Driver code
function findArea ( d ) { return ( d * d ) / 2 ; } let d = 10 ; document . write ( findArea ( d ) ) ;
Sum of series 1 ^ 2 + 3 ^ 2 + 5 ^ 2 + . . . + ( 2 * n | Function to find sum of series . ; Driver Code
function sumOfSeries ( n ) { let sum = 0 ; for ( let i = 1 ; i <= n ; i ++ ) sum = sum + ( 2 * i - 1 ) * ( 2 * i - 1 ) ; return sum ; } let n = 10 ; document . write ( sumOfSeries ( n ) ) ;
Sum of series 1 ^ 2 + 3 ^ 2 + 5 ^ 2 + . . . + ( 2 * n | Function that find sum of series . ; Formula to find sum of series . ; Driver code
function sumOfSeries ( n ) { return ( n * ( 2 * n - 1 ) * ( 2 * n + 1 ) ) / 3 ; } let n = 10 ; document . write ( sumOfSeries ( n ) ) ;
Minimum moves to reach target on a infinite line | Set 2 | Function to find minimum steps to reach target ; Handling negatives by symmetry ; Keep moving while sum is smaller i . e calculating n ; Case 1 : d is even ; d is odd ; Driver Code ; Function call
function StepstoReachTarget ( target ) { target = Math . abs ( target ) ; let n = Math . ceil ( ( - 1.0 + Math . sqrt ( 1 + 8.0 * target ) ) / 2 ) ; let sum = n * ( n + 1 ) / 2 ; if ( sum == target ) return n ; let d = sum - target ; if ( ( d & 1 ) == 0 ) return n ; else return n + ( ( n & 1 ) != 0 ? 2 : 1 ) ; } let target = 5 ; document . write ( StepstoReachTarget ( target ) ) ;
Sum of series 2 / 3 | Function to find sum of series up - to n terms ; initializing counter by 1 ; variable to calculate result ; while loop until nth term is not reached ; boolean type variable for checking validation ; Driver Code
function seriesSum ( n ) { let i = 1 ; let res = 0.0 ; let sign = true ; while ( n > 0 ) { n -- ; if ( sign ) { sign = ! sign ; res = res + ++ i / ++ i ; } else { sign = ! sign ; res = res - ++ i / ++ i ; } } return res ; } let n = 5 ; document . write ( seriesSum ( n ) . toFixed ( 6 ) ) ;
Number of Symmetric Relations on a Set | function find the square of n ; Base case ; Return 2 ^ ( n ( n + 1 ) / 2 ) ; Driver Code
function countSymmetric ( n ) { if ( n == 0 ) return 1 ; return 1 << ( ( n * ( n + 1 ) ) / 2 ) ; } let n = 3 ; document . write ( countSymmetric ( n ) ) ;
Program for centered nonagonal number | Function to find nth centered nonagonal number . ; Formula to find nth centered nonagonal number . ; Driver function .
function centeredNonagonal ( n ) { return parseInt ( ( 3 * n - 2 ) * ( 3 * n - 1 ) / 2 ) ; } let n = 10 ; document . write ( centeredNonagonal ( n ) ) ;
Program for Mean Absolute Deviation | Function to find mean of the array elements . ; Calculate sum of all elements . ; Function to find mean absolute deviation of given elements . ; Calculate the sum of absolute deviation about mean . ; Return mean absolute deviation about mean . ; Driver code
function Mean ( arr , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum = sum + arr [ i ] ; return sum / n ; } function meanAbsDevtion ( arr , n ) { let absSum = 0 ; for ( let i = 0 ; i < n ; i ++ ) absSum = absSum + Math . abs ( arr [ i ] - Mean ( arr , n ) ) ; return absSum / n ; } let arr = [ 10 , 15 , 15 , 17 , 18 , 21 ] ; let n = arr . length ; document . write ( meanAbsDevtion ( arr , n ) ) ;