codeparrot/xlcost-text-to-code huggingface dataset:
Program to calculate the value of sin ( x ) and cos ( x ) using Expansion | Function for calculation ; Converting degrees to radian ; maps the sum along the series ; holds the actual value of sin ( n ) ; Main function
function cal_cos ( n ) { let accuracy = 0.0001 , x1 , denominator , cosx , cosval ; n = n * ( 3.142 / 180.0 ) ; x1 = 1 ; cosx = x1 ; cosval = Math . cos ( n ) ; let i = 1 ; do { denominator = 2 * i * ( 2 * i - 1 ) ; x1 = - x1 * n * n / denominator ; cosx = cosx + x1 ; i = i + 1 ; } while ( accuracy <= Math . abs ( cosval - cosx ) ) ; document . write ( cosx . toFixed ( 5 ) ) ; } let n = 30 ; cal_cos ( n ) ;
Find sum of digits in factorial of a number | Function to multiply x with large number stored in vector v . Result is stored in v . ; Calculate res + prev carry ; updation at ith position ; Returns sum of digits in n ! ; One by one multiply i to current vector and update the vector . ; Find sum of digits in vector v [ ] ; Driver code
let v = [ ] ; function multiply ( x ) { let carry = 0 ; let size = v . length ; for ( let i = 0 ; i < size ; i ++ ) { let res = carry + v [ i ] * x ; v [ i ] = res % 10 ; carry = Math . floor ( res / 10 ) ; } while ( carry != 0 ) { v . push ( carry % 10 ) ; carry = Math . floor ( carry / 10 ) ; } } function findSumOfDigits ( n ) { for ( let i = 1 ; i <= n ; i ++ ) multiply ( i ) ; let sum = 0 ; let size = v . length ; for ( let i = 0 ; i < size ; i ++ ) sum += v [ i ] ; return sum ; } let n = 1000 ; document . write ( findSumOfDigits ( n ) ) ;
Find other two sides of a right angle triangle | Finds two sides of a right angle triangle if it they exist . ; if n is odd ; case of n = 1 handled separately ; case of n = 2 handled separately ; Driver code
function printOtherSides ( n ) { if ( n % 2 != 0 ) { if ( n == 1 ) document . write ( " " ) ; else { var b = ( n * n - 1 ) / 2 ; var c = ( n * n + 1 ) / 2 ; document . write ( " " + b + " " + c ) ; } } else { if ( n == 2 ) document . write ( " " ) ; else { var b = n * n / 4 - 1 ; var c = n * n / 4 + 1 ; document . write ( " " + b + " " + c ) ; } } } var a = 3 ; printOtherSides ( a ) ;
Minimum positive integer to divide a number such that the result is an odd | javascript program to make a number odd ; Return 1 if already odd ; Check on dividing with a number when the result becomes odd Return that number ; If n is divided by i and n / i is odd then return i ; Driver code
function makeOdd ( n ) { if ( n % 2 != 0 ) return 1 ; var i ; for ( i = 2 ; i <= n ; i ++ ) if ( ( n % i == 0 ) && ( ( n / i ) % 2 == 1 ) ) break ; return i ; } var n = 36 ; var res = makeOdd ( n ) ; document . write ( res ) ;
XOR of all subarray XORs | Set 2 | Returns XOR of all subarray xors ; if even number of terms are there , all numbers will appear even number of times . So result is 0. ; else initialize result by 0 as ( a xor 0 = a ) ; Driver Code
function getTotalXorOfSubarrayXors ( arr , N ) { if ( N % 2 == 0 ) return 0 ; let res = 0 ; for ( let i = 0 ; i < N ; i += 2 ) { res ^= arr [ i ] ; } return res ; } let arr = [ 3 , 5 , 2 , 4 , 6 ] ; let N = arr . length ; document . write ( getTotalXorOfSubarrayXors ( arr , N ) ) ;
Fill array with 1 's using minimum iterations of filling neighbors | Returns count of iterations to fill arr [ ] with 1 s . ; Start traversing the array ; Traverse until a 0 is found ; Count contiguous 0 s ; Condition for Case 3 ; Condition to check if Case 1 satisfies : ; If count_zero is even ; If count_zero is odd ; Reset count_zero ; Case 2 ; Update res ; Driver Code
function countIterations ( arr , n ) { var oneFound = false ; var res = 0 ; for ( var i = 0 ; i < n ; ) { if ( arr [ i ] == 1 ) oneFound = true ; while ( i < n && arr [ i ] == 1 ) i ++ ; var count_zero = 0 ; while ( i < n && arr [ i ] == 0 ) { count_zero ++ ; i ++ ; } if ( oneFound == false && i == n ) return - 1 ; var curr_count ; if ( i < n && oneFound == true ) { if ( ( count_zero & 1 ) == 0 ) curr_count = count_zero / 2 ; else curr_count = ( count_zero + 1 ) / 2 ; count_zero = 0 ; } else { curr_count = count_zero ; count_zero = 0 ; } res = Math . max ( res , curr_count ) ; } return res ; } var arr = [ 0 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 ] ; document . write ( countIterations ( arr , arr . length ) ) ;
Interesting facts about Fibonacci numbers | JavaScript program to demonstrate that Fibonacci numbers that are divisible by their indexes have indexes as either power of 5 or multiple of 12. ; storing Fibonacci numbers
var MAX = 100 ; var arr = new Array ( MAX ) ; arr [ 0 ] = 0 ; arr [ 1 ] = 1 ; for ( var i = 2 ; i < MAX ; i ++ ) arr [ i ] = arr [ i - 1 ] + arr [ i - 2 ] ; document . write ( " " ) ; for ( var i = 1 ; i < MAX ; i ++ ) if ( arr [ i ] % i == 0 ) document . write ( i + " " ) ;
Express a number as sum of consecutive numbers | Print consecutive numbers from last to first ; Driver Code
function printConsecutive ( last , first ) { document . write ( first ++ ) ; for ( let x = first ; x <= last ; x ++ ) document . write ( " " + x ) ; } function findConsecutive ( N ) { for ( let last = 1 ; last < N ; last ++ ) { for ( let first = 0 ; first < last ; first ++ ) { if ( 2 * N == ( last - first ) * ( last + first + 1 ) ) { document . write ( N + " " ) ; printConsecutive ( last , first + 1 ) ; return ; } } } document . write ( " " ) ; } let n = 12 ; findConsecutive ( n ) ;
Find n | Return n - th number in series made of 4 and 7 ; create an array of size ( n + 1 ) ; If i is odd ; main function
function printNthElement ( n ) { var arr = Array ( n + 1 ) . fill ( 0 ) ; arr [ 1 ] = 4 ; arr [ 2 ] = 7 ; for ( var i = 3 ; i <= n ; i ++ ) { if ( i % 2 != 0 ) arr [ i ] = arr [ i / 2 ] * 10 + 4 ; else arr [ i ] = arr [ ( i / 2 ) - 1 ] * 10 + 7 ; } return arr [ n ] ; } var n = 6 ; document . write ( printNthElement ( n ) ) ;
Maximum sum of distinct numbers such that LCM of these numbers is N | Returns maximum sum of numbers with LCM as N ; Finding a divisor of n and adding it to max_sum ; Driver code
function maxSumLCM ( n ) { for ( let i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { max_sum += i ; if ( n / i != i ) max_sum += ( n / i ) ; } } return max_sum ; } let n = 2 ; document . write ( maxSumLCM ( n ) + " " ) ;
Square root of a number using log | Javascript program to demonstrate finding square root of a number using sqrt ( )
var n = 12 ; document . write ( Math . sqrt ( n ) . toFixed ( 6 ) ) ;
Maximum value of an integer for which factorial can be calculated on a machine | Javascript program to find maximum value of an integer for which factorial can be calculated on your system ; When fact crosses its size , it gives negative value ; Driver Code
function findMaxValue ( ) { let res = 2 ; let fact = 2 ; while ( true ) { if ( fact < 0 fact > 9223372036854775807 ) break ; res ++ ; fact = fact * res ; } return res - 1 ; } document . write ( " " + " " + findMaxValue ( ) ) ;
Check if LCM of array elements is divisible by a prime number or not | Function to check any number of array is divisible by k or not ; If any array element is divisible by k , then LCM of whole array should also be divisible . ; Driver Code
function func ( a , k ) { for ( let i = 0 ; i < a . length ; i ++ ) if ( a [ i ] % k == 0 ) return true ; return false ; } let a = [ 14 , 27 , 38 , 76 , 84 ] ; var k = 19 ; let res = func ( a , k ) ; document . write ( res ) ;
Find the closest and smaller tidy number | Javascript program to find closest tidy number smaller than the given number ; Check whether string violates tidy property ; If string violates tidy property , then decrease the value stored at that index by 1 and replace all the value stored right to that index by 9 ; Driver code ; num will store closest tidy number
function tidyNum ( str1 , len ) { var str = str1 . split ( ' ' ) ; for ( i = len - 2 ; i >= 0 ; i -- ) { if ( str [ i ] > str [ i + 1 ] ) { str [ i ] -- ; for ( j = i + 1 ; j < len ; j ++ ) str [ j ] = ' ' ; } } return str . join ( " " ) ; } var str = " " ; var len = str . length ; document . write ( tidyNum ( str , len ) ) ;
Count of m digit integers that are divisible by an integer n | Returns count of m digit numbers having n as divisor ; generating largest number of m digit ; generating largest number of m - 1 digit ; returning number of dividend ; Driver code
function findCount ( m , n ) { let num1 = 0 ; for ( let i = 0 ; i < m ; i ++ ) num1 = ( num1 * 10 ) + 9 ; let num2 = 0 ; for ( let i = 0 ; i < ( m - 1 ) ; i ++ ) num2 = ( num2 * 10 ) + 9 ; return ( ( num1 / n ) - ( num2 / n ) ) ; } let m = 2 ; n = 6 ; document . write ( findCount ( m , n ) + " " ) ;
Find the n | Function to calculate nth number made of even digits only ; Variable to note how many such numbers have been found till now ; Bool variable to check if 1 , 3 , 5 , 7 , 9 is there or not ; Checking each digit of the number ; If 1 , 3 , 5 , 7 , 9 is found temp is changed to false ; temp is true it means that it does not have 1 , 3 , 5 , 7 , 9 ; If nth such number is found return it ; Driver Code
function findNthEvenDigitNumber ( n ) { let count = 0 ; for ( let i = 0 ; ; i ++ ) { let curr = i ; let isCurrEvenDigit = true ; while ( curr != 0 ) { if ( curr % 10 == 1 curr % 10 == 3 curr % 10 == 5 curr % 10 == 7 curr % 10 == 9 ) isCurrEvenDigit = false ; curr = Math . floor ( curr / 10 ) ; } if ( isCurrEvenDigit === true ) count ++ ; if ( count === n ) return i ; } } document . write ( findNthEvenDigitNumber ( 2 ) + " " ) ; document . write ( findNthEvenDigitNumber ( 10 ) + " " ) ;
Find the n | function to find nth number made of even digits only ; If n = 1 return 0 ; vector to store the digits when converted into base 5 ; Reduce n to n - 1 to exclude 0 ; Reduce n to base 5 number and store digits ; pushing the digits into vector ; variable to represent the number after converting it to base 5. Since the digits are be in reverse order , we traverse vector from back ; return 2 * result ( to convert digits 0 , 1 , 2 , 3 , 4 to 0 , 2 , 4 , 6 , 8. ; Driver Code
function findNthEvenDigitNumber ( n ) { if ( n == 1 ) { return 0 ; } let v = [ ] ; n = n - 1 ; while ( n > 0 ) { v . push ( n % 5 ) ; n = Math . floor ( n / 5 ) ; } let result = 0 ; for ( let i = v . length - 1 ; i >= 0 ; i -- ) { result = result * 10 ; result = result + v [ i ] ; } return 2 * result ; } document . write ( findNthEvenDigitNumber ( 2 ) + " " ) ; document . write ( findNthEvenDigitNumber ( 10 ) ) ;
Check if a large number is divisible by 25 or not | Function to find that number divisible by 25 or not ; If length of string is single digit then it 's not divisible by 25 ; Driver Code
function isDivisibleBy25 ( str ) { n = str . length ; if ( n == 1 ) return false ; return ( ( str [ n - 1 ] - ' ' == 0 && str [ n - 2 ] - ' ' == 0 ) || ( ( str [ n - 2 ] - ' ' ) * 10 + ( str [ n - 1 ] - ' ' ) ) % 25 == 0 ) ; } var str = " " ; var x = isDivisibleBy25 ( str ) ? " " : " " ; document . write ( x ) ;
Check a large number is divisible by 16 or not | Function to find that number divisible by 16 or not ; Empty string ; If there is double digit ; If there is triple digit ; If number formed by last four digits is divisible by 16. ; Driver code
function check ( str ) { let n = str . length ; if ( n == 0 && n == 1 ) return false ; if ( n == 2 ) return ( ( ( str [ n - 2 ] - ' ' ) * 10 + ( str [ n - 1 ] - ' ' ) ) % 16 == 0 ) ; if ( n == 3 ) return ( ( ( str [ n - 3 ] - ' ' ) * 100 + ( str [ n - 2 ] - ' ' ) * 10 + ( str [ n - 1 ] - ' ' ) ) % 16 == 0 ) ; let last = str [ n - 1 ] - ' ' ; let second_last = str [ n - 2 ] - ' ' ; let third_last = str [ n - 3 ] - ' ' ; let fourth_last = str [ n - 4 ] - ' ' ; return ( ( fourth_last * 1000 + third_last * 100 + second_last * 10 + last ) % 16 == 0 ) ; } let str = " " ; if ( check ( str ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find Index of given fibonacci number in constant time | A simple Javascript program to find index of given Fibonacci number . ; If Fibonacci number is less than 2 , its index will be same as number ; Iterate until generated fibonacci number is less than given fibonacci number ; res keeps track of number of generated fibonacci number ; Driver code
function findIndex ( n ) { if ( n <= 1 ) return n ; let a = 0 , b = 1 , c = 1 ; let res = 1 ; while ( c < n ) { c = a + b ; res ++ ; a = b ; b = c ; } return res ; } let result = findIndex ( 21 ) ; document . write ( result ) ;
Date after adding given number of days to the given date | Find values of day and month from offset of result year . ; Return if year is leap year or not . ; Given a date , returns number of days elapsed from the beginning of the current year ( 1 st jan ) . ; Given a year and days elapsed in it , finds date by storing results in d and m . ; Add x days to the given date . ; y2 is going to store result year and offset2 is going to store offset days in result year . ; x may store thousands of days . We find correct year and offset in the year . ; Driver code
var m2 , d2 ; function isLeap ( y ) { if ( y % 100 != 0 && y % 4 == 0 y % 400 == 0 ) return true ; return false ; } function offsetDays ( d , m , y ) { var offset = d ; if ( m - 1 == 11 ) offset += 335 ; if ( m - 1 == 10 ) offset += 304 ; if ( m - 1 == 9 ) offset += 273 ; if ( m - 1 == 8 ) offset += 243 ; if ( m - 1 == 7 ) offset += 212 ; if ( m - 1 == 6 ) offset += 181 ; if ( m - 1 == 5 ) offset += 151 ; if ( m - 1 == 4 ) offset += 120 ; if ( m - 1 == 3 ) offset += 90 ; if ( m - 1 == 2 ) offset += 59 ; if ( m - 1 == 1 ) offset += 31 ; if ( isLeap ( y ) && m > 2 ) offset += 1 ; return offset ; } function revoffsetDays ( offset , y ) { var month = [ 0 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ] ; if ( isLeap ( y ) ) month [ 2 ] = 29 ; var i ; for ( i = 1 ; i <= 12 ; i ++ ) { if ( offset <= month [ i ] ) break ; offset = offset - month [ i ] ; } d2 = offset ; m2 = i ; } function addDays ( d1 , m1 , y1 , x ) { var offset1 = offsetDays ( d1 , m1 , y1 ) ; var remDays = isLeap ( y1 ) ? ( 366 - offset1 ) : ( 365 - offset1 ) ; var y2 , offset2 = 0 ; if ( x <= remDays ) { y2 = y1 ; offset2 = offset1 + x ; } else { x -= remDays ; y2 = y1 + 1 ; var y2days = isLeap ( y2 ) ? 366 : 365 ; while ( x >= y2days ) { x -= y2days ; y2 ++ ; y2days = isLeap ( y2 ) ? 366 : 365 ; } offset2 = x ; } revoffsetDays ( offset2 , y2 ) ; document . write ( " " + d2 + " " + m2 + " " + y2 ) ; } var d = 14 , m = 3 , y = 2015 ; var x = 366 ; addDays ( d , m , y , x ) ;
Determine whether a given number is a Hyperperfect Number | Function to find the sum of all proper divisors ( excluding1 and N ) ; Iterate only until sqrt N as we are going to generate pairs to produce divisors ; As divisors occur in pairs , we can take the values i and N / i as long as i divides N ; Function to check whether the given number is prime ; base and corner cases ; Since integers can be represented as some 6 * k + y where y >= 0 , we can eliminate all integers that can be expressed in this form ; Start from 5 as this is the next prime number ; Returns true if N is a K - Hyperperfect number Else returns false . ; Condition from the definition of hyperperfect ; Driver code ; First two statements test against the condition N = 1 + K * ( sum ( proper divisors ) )
function divisorSum ( N , K ) { let sum = 0 ; for ( let i = 2 ; i <= Math . ceil ( Math . sqrt ( N ) ) ; i ++ ) if ( N % i == 0 ) sum += ( i + parseInt ( N / i , 10 ) ) ; return sum ; } function isPrime ( n ) { if ( n == 1 n == 0 ) 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 isHyperPerfect ( N , K ) { let sum = divisorSum ( N , K ) ; if ( ( 1 + K * ( sum ) ) == N ) return true ; else return false ; } let N1 = 1570153 , K1 = 12 ; let N2 = 321 , K2 = 3 ; if ( isHyperPerfect ( N1 , K1 ) ) document . write ( N1 + " " + K1 + " " + " " ) ; else document . write ( N1 + " " + K1 + " " + " " ) ; if ( isHyperPerfect ( N2 , K2 ) ) document . write ( N2 + " " + K2 + " " + " " ) ; else document . write ( N2 + " " + K2 + " " + " " ) ;
Given a number n , find the first k digits of n ^ n | function that manually calculates n ^ n and then removes digits until k digits remain ; take log10 of n ^ n . log10 ( n ^ n ) = n * log10 ( n ) ; We will now try to separate the decimal and integral part of the / product . The floor function returns the smallest integer less than or equal to the argument . So in this case , product - floor ( product ) will give us the decimal part of product ; we will now exponentiate this back by raising 10 to the power of decimal part ; We now try to find the power of 10 by which we will have to multiply the decimal part to obtain our final answer ; Driver code
function firstkdigits ( n , k ) { let product = n * Math . log10 ( n ) ; let decimal_part = product - Math . floor ( product ) ; decimal_part = Math . pow ( 10 , decimal_part ) ; let digits = Math . pow ( 10 , k - 1 ) , i = 0 ; return ( Math . floor ( decimal_part * digits ) ) ; } let n = 1450 ; let k = 6 ; document . write ( firstkdigits ( n , k ) ) ;
Generate k digit numbers with digits in strictly increasing order | number -- > Current value of number . x -- > Current digit to be considered k -- > Remaining number of digits ; Try all possible greater digits ; Generates all well ordered numbers of length k . ; Driver Code
function printWellOrdered ( number , x , k ) { if ( k == 0 ) { document . write ( number + " " ) ; return ; } for ( let i = ( x + 1 ) ; i < 10 ; i ++ ) printWellOrdered ( number * 10 + i , i , k - 1 ) ; } function generateWellOrdered ( k ) { printWellOrdered ( 0 , 0 , k ) ; } let k = 3 ; generateWellOrdered ( k ) ;
Multiply large integers under large modulo | Returns ( a * b ) % mod ; Initialize result ; Update a if it is more than or equal to mod ; If b is odd , add a with result ; Here we assume that doing 2 * a doesn 't cause overflow ; b = ( b >> 1 ) ; b = b / 2 ; Driver Code
function moduloMultiplication ( a , b , mod ) { let res = 0 ; a = ( a % mod ) ; while ( b > 0 ) { if ( ( b & 1 ) > 0 ) { res = ( res + a ) % mod ; } a = ( 2 * a ) % mod ; } return res ; } let a = 426 ; let b = 964 ; let m = 235 ; document . write ( moduloMultiplication ( a , b , m ) ) ;
Number of occurrences of 2 as a digit in numbers from 0 to n | Javascript code for above implementation ; Driver code
function numberOf2sinRange ( n ) { var s = " " ; for ( var i = 0 ; i < n + 1 ; i ++ ) s += i . toString ( ) ; var count = 0 ; for ( var i = 0 ; i < s . length ; i ++ ) { if ( s . charAt ( i ) == ' ' ) { count ++ ; } } return count ; } var n = 30 ; document . write ( numberOf2sinRange ( n ) ) ;
Number of occurrences of 2 as a digit in numbers from 0 to n | Counts the number of 2 s in a number at d - th digit ; if the digit in spot digit is ; Counts the number of '2' digits between 0 and n ; Convert integer to String to find its length ; Traverse every digit and count for every digit ; Driver Code
function count2sinRangeAtDigit ( number , d ) { var powerOf10 = parseInt ( Math . pow ( 10 , d ) ) ; var nextPowerOf10 = powerOf10 * 10 ; var right = number % powerOf10 ; var roundDown = number - number % nextPowerOf10 ; var roundup = roundDown + nextPowerOf10 ; var digit = parseInt ( number / powerOf10 ) % 10 ; if ( digit < 2 ) { return roundDown / 10 ; } if ( digit == 2 ) { return roundDown / 10 + right + 1 ; } return roundup / 10 ; } function numberOf2sinRange ( number ) { var convert ; convert = number . toString ( ) ; var s = convert ; var len = s . length ; var count = 0 ; for ( digit = 0 ; digit < len ; digit ++ ) { count += count2sinRangeAtDigit ( number , digit ) ; } return count ; } document . write ( numberOf2sinRange ( 22 ) ) ; document . write ( " " + numberOf2sinRange ( 100 ) ) ;
Modulo 10 ^ 9 + 7 ( 1000000007 ) |  ; f = ( f * i ) % M ; Now f never can exceed 10 ^ 9 + 7
function factorial ( n ) { let M = 1000000007 ; let f = 1 ; for ( let i = 1 ; i <= n ; i ++ ) return f ; }
Modulo 10 ^ 9 + 7 ( 1000000007 ) |
function mod ( a , m ) { return ( a % m + m ) % m ; }
Program to find Star number | Returns n - th star number ; Driver code
function findStarNum ( n ) { return ( 6 * n * ( n - 1 ) + 1 ) ; } let n = 3 ; document . write ( findStarNum ( n ) ) ;
Check if a large number is divisible by 5 or not | Function to find that number divisible by 5 or not . The function assumes that string length is at least one . ; Driver Code
function isDivisibleBy5 ( str ) { n = str . length ; return ( ( ( str [ n - 1 ] - ' ' ) == 0 ) || ( ( str [ n - 1 ] - ' ' ) == 5 ) ) ; } var str = " " ; var x = isDivisibleBy5 ( str ) ? " " : " " ; document . write ( x ) ;
Tidy Number ( Digits in non | Returns true if num is Tidy ; To store previous digit ( Assigning initial value which is more than any digit ) ; Traverse all digits from right to left and check if any digit is smaller than previous . ; Driver Code
function isTidy ( num ) { let prev = 10 ; while ( num != 0 ) { let rem = num % 10 ; num /= 10 ; if ( rem > prev ) return false ; prev = rem ; } return true ; } let num = 1556 ; document . write ( isTidy ( num ) ? " " : " " ) ;
Nth Square free number | Function to find nth square free number ; To maintain count of square free number ; Loop for square free numbers ; Checking whether square of a number is divisible by any number which is a perfect square ; If number is square free ; If the cnt becomes n , return the number ; Driver Code
function squareFree ( n ) { let cnt = 0 ; for ( let i = 1 ; ; i ++ ) { let isSqFree = true ; for ( let j = 2 ; j * j <= i ; j ++ ) { if ( i % ( j * j ) == 0 ) { isSqFree = false ; break ; } } if ( isSqFree == true ) { cnt ++ ; if ( cnt == n ) return i ; } } } let n = 10 ; document . write ( " " + squareFree ( n ) ) ;
Nth Square free number | Maximum prime number to be considered for square divisibility ; Maximum value of result . We do binary search from 1 to MAX_RES ; 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 ; Store all prime numbers in a [ ] ; Function to count integers upto k which are having perfect squares as factors . i is index of next prime number whose square needs to be checked . curr is current number whos square to be checked . ; variable to store square of prime ; If value of greatest integer becomes zero ; Counting integers with squares as factor ; Inclusion ( Recur for next prime number ) ; Exclusion ( Recur for next prime number ) ; Final count ; Function to return nth square free number ; Computing primes and storing it in an array a [ ] ; Applying binary search ; ' c ' contains Number of square free numbers less than or equal to ' mid ' ; If c < n , then search right side of mid else search left side of mid ; nth square free number ; Driver code
let MAX_PRIME = 100000 ; let MAX_RES = 2000000000 ; function SieveOfEratosthenes ( a ) { let prime = new Array ( MAX_PRIME + 1 ) ; for ( let i = 0 ; i < ( MAX_PRIME + 1 ) ; i ++ ) { prime [ i ] = true ; } for ( let p = 2 ; p * p <= MAX_PRIME ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * 2 ; i <= MAX_PRIME ; i += p ) prime [ i ] = false ; } } for ( let p = 2 ; p <= MAX_PRIME ; p ++ ) if ( prime [ p ] ) a . push ( p ) ; } function countSquares ( i , cur , k , a ) { let square = a [ i ] * a [ i ] ; let newCur = square * cur ; if ( newCur > k ) return 0 ; let cnt = Math . floor ( k / ( newCur ) ) ; cnt += countSquares ( i + 1 , cur , k , a ) ; cnt -= countSquares ( i + 1 , newCur , k , a ) ; return cnt ; } function squareFree ( n ) { let a = [ ] ; SieveOfEratosthenes ( a ) ; let low = 1 ; let high = MAX_RES ; while ( low < high ) { let mid = low + Math . floor ( ( high - low ) / 2 ) ; let c = mid - countSquares ( 0 , 1 , mid , a ) ; if ( c < n ) low = mid + 1 ; else high = mid ; } return low ; } let n = 10 ; document . write ( squareFree ( n ) ) ;