codeparrot/xlcost-text-to-code huggingface dataset:
Centered Hexadecagonal Number | Centered hexadecagonal function ; Formula to calculate nth centered hexadecagonal number ; Driver code
function center_hexadecagonal_num ( n ) { return 8 * n * n - 8 * n + 1 ; } var n = 2 ; document . write ( n + " " + " " ) ; document . write ( center_hexadecagonal_num ( n ) + " " ) ; n = 12 ; document . write ( n + " " + " " ) ; document . write ( center_hexadecagonal_num ( n ) ) ;
Check if the n | Javascript Program to check if the nth is odd or even in a sequence where each term is sum of previous two term ; Return if the nth term is even or odd . ; Return true if odd ; Driven Program
var MAX = 100 ; function findNature ( a , b , n ) { var seq = Array ( MAX ) . fill ( 0 ) ; seq [ 0 ] = a ; seq [ 1 ] = b ; for ( var i = 2 ; i <= n ; i ++ ) seq [ i ] = seq [ i - 1 ] + seq [ i - 2 ] ; return ( seq [ n ] & 1 ) ; } var a = 2 , b = 4 ; var n = 3 ; ( findNature ( a , b , n ) ? ( document . write ( " " + " " ) ) : ( document . write ( " " + " " ) ) ) ;
Program to compare m ^ n and n ^ m | function to compare m ^ n and n ^ m ; m ^ n ; n ^ m ; Driver Code ; function call to compare m ^ n and n ^ m
function check ( m , n ) { var RHS = m * Math . log ( n ) ; var LHS = n * Math . log ( m ) ; if ( LHS > RHS ) { document . write ( " " ) ; } else if ( LHS < RHS ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var m = 987654321 ; var n = 123456987 ; check ( m , n ) ;
Find the GCD that lies in given range | Return the greatest common divisor of two numbers ; Return the gretest common divisor of a and b which lie in the given range . ; Loop from 1 to sqrt ( GCD ( a , b ) . ; if i divides the GCD ( a , b ) , then find maximum of three numbers res , i and g / i ; Driven Program
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function maxDivisorRange ( a , b , l , h ) { var g = gcd ( a , b ) ; var res = - 1 ; for ( i = l ; i * i <= g && i <= h ; i ++ ) if ( g % i == 0 ) res = Math . max ( res , Math . max ( i , g / i ) ) ; return res ; } var a = 3 , b = 27 , l = 1 , h = 5 ; document . write ( maxDivisorRange ( a , b , l , h ) ) ;
Number expressed as sum of five consecutive integers | function to check if a number can be expressed as sum of five consecutive integers . ; 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 - 4 ; check if sum of five consecutive integer is equal to n . ; Driver Program
function checksum ( n ) { if ( n == 0 ) { document . write ( " " ) ; return ; } var inc ; if ( n > 0 ) inc = 1 ; else inc = - 1 ; for ( i = 0 ; i <= n - 4 ; i += inc ) { if ( i + i + 1 + i + 2 + i + 3 + i + 4 == n ) { document . write ( ( i ) + " " + ( i + 1 ) + " " + ( i + 2 ) + " " + ( i + 3 ) + " " + ( i + 4 ) ) ; return ; } } document . write ( " " ) ; } var n = 15 ; checksum ( n ) ;
Number expressed as sum of five consecutive integers | function to check if a number can be expressed as sum of five consecutive integers . ; if n is multiple of 5 ; else prvar " - 1" . ; Driver Program
function checksum ( n ) { if ( n % 5 == 0 ) document . write ( ( n / 5 - 2 ) + " " + ( n / 5 - 1 ) + " " + ( n / 5 ) + " " + ( n / 5 + 1 ) + " " + ( n / 5 + 2 ) ) ; else document . write ( " " ) ; } var n = 15 ; checksum ( n ) ;
Number of Transpositions in a Permutation | Javascript Program to find the number of transpositions in a permutation ; This array stores which element goes to which position ; This function returns the size of a component cycle ; If it is already visited ; This functio returns the number of transpositions in the permutation ; Initializing visited [ ] array ; building the goesTo [ ] array ; Driver Code
let N = 1000001 var visited = new Array ( N ) ; var goesTo = new Array ( N ) ; function dfs ( i ) { if ( visited [ i ] == 1 ) return 0 ; visited [ i ] = 1 ; let x = dfs ( goesTo [ i ] ) ; return ( x + 1 ) ; } function noOfTranspositions ( P , n ) { for ( let i = 1 ; i <= n ; i ++ ) visited [ i ] = 0 ; for ( let i = 0 ; i < n ; i ++ ) goesTo [ P [ i ] ] = i + 1 ; let transpositions = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { if ( visited [ i ] == 0 ) { let ans = dfs ( i ) ; transpositions += ans - 1 ; } } return transpositions ; } let permutation = [ 5 , 1 , 4 , 3 , 2 ] ; let n = permutation . length ; document . write ( noOfTranspositions ( permutation , n ) ) ;
n | Function to find the nth term of series ; Loop to add 4 th powers ; Driver Code
function sumOfSeries ( n ) { let ans = 0 ; for ( let i = 1 ; i <= n ; i ++ ) ans += i * i * i * i ; return ans ; } let n = 4 ; document . write ( sumOfSeries ( n ) ) ;
Number of unmarked integers in a special sieve | javascript Program to determine the number of unmarked integers in a special sieve ; Driver Code
function countUnmarked ( N ) { if ( N % 2 == 0 ) return N / 2 ; else return N / 2 + 1 ; } var N = 4 ; document . write ( " " + " " + countUnmarked ( N ) ) ;
Sum of series 1 * 1 ! + 2 * 2 ! + …… . . + n * n ! | java script program to find sum of the series . ; Function to calculate required series ; Driver code
function factorial ( n ) { let res = 1 ; for ( let i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } function calculateSeries ( n ) { return factorial ( n + 1 ) - 1 ; } let n = 3 ; document . write ( calculateSeries ( n ) ) ;
Sum of series 1 * 1 * 2 ! + 2 * 2 * 3 ! + …… . . + n * n * ( n + 1 ) ! | java script program to find sum of the series . ; Function to calculate required series ; Drivers code
function factorial ( n ) { let res = 1 ; for ( let i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } function calculateSeries ( n ) { return 2 + ( n * n + n - 2 ) * factorial ( n + 1 ) ; } let n = 3 ; document . write ( calculateSeries ( n ) ) ;
Forming smallest array with given constraints | Return the size of smallest array with given constraint . ; Drivers code
function minimumLength ( x , y , z ) { return 1 + Math . abs ( x - y ) + Math . abs ( y - z ) ; } var x = 3 , y = 1 , z = 2 ; document . write ( minimumLength ( x , y , z ) ) ;
Find the other | Function to find the other - end point of diameter ; Find end point for x coordinates ; Find end point for y coordinates ; Driver code
function endPointOfDiameterofCircle ( x1 , y1 , c1 , c2 ) { document . write ( " " + ( 2 * c1 - x1 ) + " " ) ; document . write ( " " + ( 2 * c2 - y1 ) ) ; } let x1 = - 4 , y1 = - 1 ; let c1 = 3 , c2 = 5 ; endPointOfDiameterofCircle ( x1 , y1 , c1 , c2 ) ;
Centered heptagonal number | Function to find Centered heptagonal number ; Formula to calculate nth Centered heptagonal number ; Driver Code
function centered_heptagonal_num ( n ) { return parseInt ( ( 7 * n * n - 7 * n + 2 ) / 2 ) ; } let n = 5 ; document . write ( n + " " ) ; document . write ( centered_heptagonal_num ( n ) ) ;
Sum of square | Function to find sum of sum of square of first n natural number ; Driven Program
function findSum ( n ) { return ( n * ( n + 1 ) * ( n + 1 ) * ( n + 2 ) ) / 12 ; } let n = 3 ; document . write ( findSum ( n ) + " " ) ;
Check if a given matrix is Hankel or not | Function to check if given matrix is Hankel Matrix or not . ; for each row ; for each column ; checking if i + j is less than n ; checking if the element is equal to the corresponding diagonal constant ; checking if the element is equal to the corresponding diagonal constant ; Drivers code
function checkHankelMatrix ( n , m ) { for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( i + j < n ) { if ( m [ i ] [ j ] != m [ i + j ] [ 0 ] ) return false ; } else { if ( m [ i ] [ j ] != m [ i + j - n + 1 ] [ n - 1 ] ) return false ; } } } return true ; } let n = 4 ; let m = [ [ 1 , 2 , 3 , 5 ] , [ 2 , 3 , 5 , 8 ] , [ 3 , 5 , 8 , 0 ] , [ 5 , 8 , 0 , 9 ] ] ; if ( checkHankelMatrix ( n , m ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if a number can be expressed as power | Set 2 ( Using Log ) | javascript program to find if a number can be expressed as x raised to power y . ; Find Log n in different bases and check if the value is an integer ; Driver code
function isPower ( n ) { for ( x = 2 ; x <= parseInt ( Math . sqrt ( n ) ) ; x ++ ) { var f = Math . log ( n ) / Math . log ( x ) ; if ( ( f - parseInt ( f ) ) == 0.0 ) return true ; } return false ; } for ( i = 2 ; i < 100 ; i ++ ) if ( isPower ( i ) ) document . write ( i + " " ) ;
Queries on sum of odd number digit sums of all the factors of a number | Javascript Program to answer queries on sum of sum of odd number digits of all the factors of a number ; finding sum of odd digit number in each integer . ; for each number ; using previous number sum , finding the current number num of odd digit also , adding last digit if it is odd . ; finding sum of sum of odd digit of all the factors of a number . ; for each possible factor ; adding the contribution . ; Wrapper function ; Driver Program
var N = 1000005 ; function sumOddDigit ( digitSum ) { for ( var i = 1 ; i < N ; i ++ ) { digitSum [ i ] = digitSum [ parseInt ( i / 10 ) ] + ( i & 1 ) * ( i % 10 ) ; } } function sumFactor ( digitSum , factorDigitSum ) { for ( var i = 1 ; i < N ; i ++ ) { for ( var j = i ; j < N ; j += i ) { factorDigitSum [ j ] += digitSum [ i ] ; } } } function wrapper ( q , n ) { var digitSum = Array ( N ) . fill ( 0 ) ; var factorDigitSum = Array ( N ) . fill ( 0 ) ; sumOddDigit ( digitSum ) ; sumFactor ( digitSum , factorDigitSum ) ; for ( var i = 0 ; i < q ; i ++ ) document . write ( factorDigitSum [ n [ i ] ] + " " ) ; } var q = 2 ; var n = [ 10 , 36 ] ; wrapper ( q , n ) ;
Program for Gauss | JavaScript Implementation for Gauss - Jordan Elimination Method ; Function to print the matrix ; function to reduce matrix to reduced row echelon form . ; Performing elementary operations ; Excluding all i == j ; Converting Matrix to reduced row echelon form ( diagonal matrix ) ; Function to print the desired result if unique solutions exists , otherwise prints no solution or infinite solutions depending upon the input given . ; Printing the solution by dividing constants by their respective diagonal elements ; To check whether infinite solutions exists or no solution exists ; flag == 2 for infinite solution flag == 3 for No solution ; Driver code ; Order of Matrix ( n ) ; Performing Matrix transformation ; Printing Final Matrix ; Printing Solutions ( if exist )
let M = 10 ; function PrintMatrix ( a , n ) { for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j <= n ; j ++ ) document . write ( a [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } function PerformOperation ( a , n ) { let i , j , k = 0 , c , flag = 0 , m = 0 ; let pro = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] [ i ] == 0 ) { c = 1 ; while ( ( i + c ) < n && a [ i + c ] [ i ] == 0 ) c ++ ; if ( ( i + c ) == n ) { flag = 1 ; break ; } for ( j = i , k = 0 ; k <= n ; k ++ ) { let temp = a [ j ] [ k ] ; a [ j ] [ k ] = a [ j + c ] [ k ] ; a [ j + c ] [ k ] = temp ; } } for ( j = 0 ; j < n ; j ++ ) { if ( i != j ) { let p = a [ j ] [ i ] / a [ i ] [ i ] ; for ( k = 0 ; k <= n ; k ++ ) a [ j ] [ k ] = a [ j ] [ k ] - ( a [ i ] [ k ] ) * p ; } } } return flag ; } function PrintResult ( a , n , flag ) { document . write ( " " ) ; if ( flag == 2 ) document . write ( " " ) ; else if ( flag == 3 ) document . write ( " " ) ; else { for ( let i = 0 ; i < n ; i ++ ) document . write ( a [ i ] [ n ] / a [ i ] [ i ] + " " ) ; } } function CheckConsistency ( a , n , flag ) { let i , j ; let sum ; flag = 3 ; for ( i = 0 ; i < n ; i ++ ) { sum = 0 ; for ( j = 0 ; j < n ; j ++ ) sum = sum + a [ i ] [ j ] ; if ( sum == a [ i ] [ j ] ) flag = 2 ; } return flag ; } let a = [ [ 0 , 2 , 1 , 4 ] , [ 1 , 1 , 2 , 6 ] , [ 2 , 1 , 1 , 7 ] ] ; let n = 3 , flag = 0 ; flag = PerformOperation ( a , n ) ; if ( flag == 1 ) flag = CheckConsistency ( a , n , flag ) ; document . write ( " " ) ; PrintMatrix ( a , n ) ; document . write ( " " ) ; PrintResult ( a , n , flag ) ;
Number of digits in the nth number made of given four digits | Efficient function to calculate number of digits in the nth number constructed by using 6 , 1 , 4 and 9 as digits in the ascending order . ; Number of digits increase after every i - th number where i increases in powers of 4. ; Driver code
function number_of_digits ( n ) { let i , res , sum = 0 ; for ( i = 4 , res = 1 ; ; i *= 4 , res ++ ) { sum += i ; if ( sum >= n ) break ; } return res ; } let n = 21 ; document . write ( number_of_digits ( n ) + " " ) ;
Print prime numbers from 1 to N in reverse order | Javascript program to print all primes between 1 to N in reverse order using Sieve of Eratosthenes ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Print all prime numbers in reverse order ; static input ; to display
function Reverseorder ( n ) { let prime = new Array ( n + 1 ) ; for ( let i = 0 ; i < n ; i ++ ) prime [ i ] = true ; for ( let p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } for ( let p = n ; p >= 2 ; p -- ) if ( prime [ p ] ) document . write ( p + " " ) ; } let N = 25 ; document . write ( " " + " " ) ; if ( N == 1 ) document . write ( " " ) ; else
Vantieghems Theorem for Primality Test | Javascript code to verify Vantieghem 's Theorem ; Check if above condition is satisfied ; product of previous powers of 2 ; Driver Code
function checkVantieghemsTheorem ( limit ) { let prod = 1 ; for ( let n = 2 ; n < limit ; n ++ ) { if ( n == 2 ) document . write ( 2 + " " + " " ) ; if ( ( ( prod - n ) % ( ( 1 << n ) - 1 ) ) == 0 ) document . write ( n + " " + " " ) ; prod *= ( ( 1 << n ) - 1 ) ; } } checkVantieghemsTheorem ( 10 ) ;
Finding n | Function to generate a fixed number ; Driver Function
function magicOfSequence ( N ) { let sum = 0 ; for ( let i = 1 ; i <= N ; i ++ ) sum += ( i * i * i + i * 2 ) ; return sum ; } let N = 4 ; document . write ( magicOfSequence ( N ) ) ;
Making zero array by decrementing pairs of adjacent | Javascript program to find if it is possible to make all array elements 0 by decrement operations . ; Used for storing the sum of even and odd position element in array . ; If position is odd , store sum value of odd position in odd ; If position is even , store sum value of even position in even ; Driver code
function isPossibleToZero ( a , n ) { let even = 0 , odd = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( ( i & 1 ) == 0 ) odd += a [ i ] ; else even += a [ i ] ; } return ( odd == even ) ; } let arr = [ 0 , 1 , 1 , 0 ] ; let n = arr . length ; if ( isPossibleToZero ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Program for sum of cos ( x ) series | Javascript program to find the sum of cos ( x ) series ; here x is in degree . we have to convert it to radian for using it with series formula , as in series expansion angle is in radian ; Driver Code
const PI = 3.142 ; function cosXSertiesSum ( x , n ) { x = x * ( PI / 180.0 ) ; let res = 1 ; let sign = 1 , fact = 1 , pow = 1 , i ; for ( i = 1 ; i < 5 ; i ++ ) { sign = sign * - 1 ; fact = fact * ( 2 * i - 1 ) * ( 2 * i ) ; pow = pow * x * x ; res = res + sign * pow / fact ; } return res ; } let x = 50 ; let n = 5 ; document . write ( ( ( cosXSertiesSum ( x , 5 ) * 1000000 ) / 1000000.00 ) . toFixed ( 6 ) ) ;
Sum of digits written in different bases from 2 to n | function to calculate sum of digit for a given base ; Sum of digits ; Calculating the number ( n ) by taking mod with the base and adding remainder to the result and parallelly reducing the num value . ; returning the result ; function calling for multiple bases ; Driver Code
function solve ( n , base ) { var result = 0 ; while ( n > 0 ) { var remainder = n % base ; result = result + remainder ; n = parseInt ( n / base ) ; } return result ; } function printSumsOfDigits ( n ) { for ( base = 2 ; base < n ; ++ base ) document . write ( solve ( n , base ) + " " ) ; } var n = 8 ; printSumsOfDigits ( n ) ;
Possible two sets from first N natural numbers difference of sums as D | Function returns true if it is possible to split into two sets otherwise returns false ; Driver code
function check ( N , D ) { let temp = ( N * ( N + 1 ) ) / 2 + D ; return ( temp % 2 == 0 ) ; } let N = 5 ; let M = 7 ; if ( check ( N , M ) ) document . write ( " " ) ; else document . write ( " " ) ;
Minimum digits to remove to make a number Perfect Square | Function to check minimum number of digits should be removed to make this number a perfect square ; Size of the string ; Our final answer ; To store string which is perfect square . ; We make all possible subsequences ; To check jth bit is set or not . ; We do not consider a number with leading zeros ; Convert our temporary string into integer ; Checking temp is perfect square or not . ; Taking maximum sized string ; Print PerfectSquare ; Driver code
function perfectSquare ( s ) { let n = s . length ; let ans = - 1 ; let num = " " ; for ( let i = 1 ; i < ( 1 << n ) ; i ++ ) { let str = " " ; for ( j = 0 ; j < n ; j ++ ) { if ( ( i >> j ) & 1 ) { str = str + s [ j ] ; } } if ( str [ 0 ] != ' ' ) { let temp = 0 ; for ( let j = 0 ; j < str . length ; j ++ ) temp = temp * 10 + ( str [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; k = Math . floor ( Math . sqrt ( temp ) ) ; if ( ( k * k ) == temp ) { if ( ans < str . length ) { ans = str . length ; num = str ; } } } } if ( ans == - 1 ) return ans ; else { document . write ( num + " " ) ; return ( n - ans ) ; } } document . write ( perfectSquare ( " " ) + " " ) ; document . write ( perfectSquare ( " " ) + " " ) ;
Lagrange 's four square theorem | Prints all the possible combinations 4 numbers whose sum of squares is equal to the given no . ; Loops checking the sum of squares ; If sum of four squares equals the given no . ; Printing the numbers ; Driver code ; 74 = 0 * 0 + 0 * 0 + 5 * 5 + 7 * 7 74 = 0 * 0 + 1 * 1 + 3 * 3 + 8 * 8 74 = 0 * 0 + 3 * 3 + 4 * 4 + 7 * 7 74 = 1 * 1 + 1 * 1 + 6 * 6 + 6 * 6 74 = 2 * 2 + 3 * 3 + 5 * 5 + 6 * 6
function printFourSquares ( a ) { for ( let i = 0 ; i * i <= a ; i ++ ) { for ( let j = i ; j * j <= a ; j ++ ) { for ( let k = j ; k * k <= a ; k ++ ) { for ( let l = k ; l * l <= a ; l ++ ) { if ( i * i + j * j + k * k + l * l == a ) { document . write ( a + " " + i + " " + i + " " + j + " " + j + " " ) ; document . write ( k + " " + k + " " + l + " " + l + " " ) ; } } } } } } let a = 74 ; printFourSquares ( a ) ;
Hardy | A function to count prime factors of a given number n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; This condition is to handle the case when n is a prime number greater than 2 ; Driver Code
function exactPrimeFactorCount ( n ) { let count = 0 ; if ( n % 2 == 0 ) { count ++ ; while ( n % 2 == 0 ) n = n / 2 ; } for ( let i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { count ++ ; while ( n % i == 0 ) n = n / i ; } } if ( n > 2 ) count ++ ; return count ; } let n = 51242183 ; document . write ( " " + exactPrimeFactorCount ( n ) + " " ) ; document . write ( " " + Math . log ( Math . log ( n ) ) + " " ) ;
Number of Digits in a ^ b | function to calculate number of digits in a ^ b ; Driver program
function no_of_digit ( a , b ) { return ( Math . round ( ( b * Math . log10 ( a ) ) + 1 ) ) ; } let a = 2 , b = 100 ; document . write ( " " + no_of_digit ( a , b ) ) ;
Check whether a number is Emirpimes or not | Checking whether a number is semi - prime or not ; Increment count of prime numbers ; If number is still greater than 1 , after exiting the for loop add it to the count variable as it indicates the number is a prime number ; Return '1' if count is equal to '2' else return '0' ; Checking whether a number is emirpimes or not ; Number itself is not semiprime . ; Finding reverse of n . ; The definition of emirpimes excludes palindromes , hence we do not check further , if the number entered is a palindrome ; Checking whether the reverse of the semi prime number entered is also a semi prime number or not ; Driver code
function checkSemiprime ( num ) { let cnt = 0 ; for ( let i = 2 ; cnt < 2 && i * i <= num ; ++ i ) { while ( num % i == 0 ) { num = parseInt ( num / i , 10 ) ; ++ cnt ; } } if ( num > 1 ) ++ cnt ; return cnt == 2 ; } function isEmirpimes ( n ) { if ( checkSemiprime ( n ) == false ) return false ; let r = 0 ; for ( let t = n ; t != 0 ; t = parseInt ( t / n , 10 ) ) r = r * 10 + t % 10 ; if ( r == n ) return false ; return ( checkSemiprime ( r ) ) ; } let n = 15 ; if ( isEmirpimes ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Connell Sequence | Function to generate a fixed number of even or odd terms . The size of r decides whether numbers to be generated even or odd . ; Generating the first ' n ' terms of Connell Sequence ; A dummy 0 is inserted at the beginning for consistency ; Calling function gen ( ) to generate ' k ' number of terms ; Checking if ' n ' terms are already generated ; Removing the previously inserted dummy 0 ; Driver Method
function gen ( n , r ) { var a = r [ r . length - 1 ] ; a ++ ; for ( var i = 1 ; i <= n ; a += 2 , i ++ ) r . push ( a ) ; return r ; } function conell ( n ) { var res = [ ] ; var k = 1 ; res . push ( 0 ) ; while ( 1 ) { res = gen ( k , res ) ; k ++ ; var j = res . length - 1 ; while ( j != n && j + k > n ) k -- ; if ( j >= n ) break ; } res . shift ( ) ; return res ; } var n = 10 ; document . write ( " " + n + " " + " " ) ; var res = conell ( n ) ; for ( var i = 0 ; i < res . length ; i ++ ) document . write ( res [ i ] + " " ) ;
Generate a list of n consecutive composite numbers ( An interesting method ) | function to find factorial of given number ; Prints n consecutive numbers . ; Driver code
function factorial ( n ) { let res = 1 ; for ( let i = 2 ; i <= n ; i ++ ) { res *= i ; } return res ; } function printNComposite ( n ) { let fact = factorial ( n + 1 ) ; for ( let i = 2 ; i <= n + 1 ; ++ i ) { document . write ( fact + i + " " ) ; } } let n = 4 ; printNComposite ( n ) ;
Frugal Number | Finding primes upto entered number ; Finding primes by Sieve of Eratosthenes method ; If prime [ i ] is not changed , then it is prime ; Update all multiples of p ; Forming array of the prime numbers found ; Returns number of digits in n ; Checking whether a number is Frugal or not ; Finding number of digits in prime factorization of the number ; Exponent for current factor ; Counting number of times this prime factor divides ( Finding exponent ) ; Finding number of digits in the exponent Avoiding exponents of value 1 ; Checking condition for frugal number ; Driver Method to check for frugal number
function primes ( n ) { var prime = Array ( n + 1 ) . fill ( true ) ; for ( var i = 2 ; i * i <= n ; i ++ ) { if ( prime [ i ] == true ) { for ( var j = i * 2 ; j <= n ; j += i ) prime [ j ] = false ; } } var arr = [ ] ; for ( var i = 2 ; i < n ; i ++ ) if ( prime [ i ] ) arr . push ( i ) ; return arr ; } function countDigits ( n ) { var temp = n ; var c = 0 ; while ( temp != 0 ) { temp = parseInt ( temp / 10 ) ; c ++ ; } return c ; } function frugal ( n ) { var r = primes ( n ) ; var t = n ; var s = 0 ; for ( var i = 0 ; i < r . length ; i ++ ) { if ( t % r [ i ] == 0 ) { var k = 0 ; while ( t % r [ i ] == 0 ) { t = parseInt ( t / r [ i ] ) ; k ++ ; } if ( k == 1 ) s = s + countDigits ( r [ i ] ) ; else if ( k != 1 ) s = s + countDigits ( r [ i ] ) + countDigits ( k ) ; } } return ( countDigits ( n ) > s && s != 0 ) ; } var n = 343 ; if ( frugal ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
N | JavaScript program to find n - th number which is bothsquare and cube . ; Driver code
function nthSquareCube ( n ) { return n * n * n * n * n * n ; } let n = 5 ; document . write ( nthSquareCube ( n ) ) ;
Squared triangular number ( Sum of cubes ) | Function to find if the given number is sum of the cubes of first n natural numbers ; Start adding cubes of the numbers from 1 ; If sum becomes equal to s return n ; Driver code
function findS ( s ) { let sum = 0 ; for ( let n = 1 ; sum < s ; n ++ ) { sum += n * n * n ; if ( sum == s ) return n ; } return - 1 ; } let s = 9 ; let n = findS ( s ) ; n == - 1 ? document . write ( " " ) : document . write ( n ) ;