codeparrot/xlcost-text-to-code huggingface dataset:
Count of m digit integers that are divisible by an integer n | Java program to count m digit numbers having n as divisor . ; 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 ; main function
class Main { static int findCount ( int m , int n ) { int num1 = 0 ; for ( int i = 0 ; i < m ; i ++ ) num1 = ( num1 * 10 ) + 9 ; int num2 = 0 ; for ( int i = 0 ; i < ( m - 1 ) ; i ++ ) num2 = ( num2 * 10 ) + 9 ; return ( ( num1 / n ) - ( num2 / n ) ) ; } public static void main ( String [ ] args ) { int m = 2 , n = 6 ; System . out . println ( findCount ( m , n ) ) ; } }
Find the n | Simple Java program to find the n - th number made of even digits only ; 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
class GFG { static int findNthEvenDigitNumber ( int n ) { int count = 0 ; for ( int i = 0 ; ; i ++ ) { int curr = i ; boolean isCurrEvenDigit = true ; while ( curr != 0 ) { if ( curr % 10 == 1 curr % 10 == 3 curr % 10 == 5 curr % 10 == 7 curr % 10 == 9 ) isCurrEvenDigit = false ; curr = curr / 10 ; } if ( isCurrEvenDigit == true ) count ++ ; if ( count == n ) return i ; } } public static void main ( String [ ] args ) { System . out . println ( findNthEvenDigitNumber ( 2 ) ) ; System . out . println ( findNthEvenDigitNumber ( 10 ) ) ; } }
Find the n | Efficient Java program to find n - th number made of even digits only ; 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
import java . util . * ; class GFG { static int findNthEvenDigitNumber ( int n ) { if ( n == 1 ) { return 0 ; } Vector < Integer > v = new Vector < > ( ) ; n = n - 1 ; while ( n > 0 ) { v . add ( n % 5 ) ; n = n / 5 ; } int result = 0 ; for ( int i = v . size ( ) - 1 ; i >= 0 ; i -- ) { result = result * 10 ; result = result + v . get ( i ) ; } return 2 * result ; } public static void main ( String [ ] args ) { System . out . println ( findNthEvenDigitNumber ( 2 ) ) ; System . out . println ( findNthEvenDigitNumber ( 10 ) ) ; } }
Check if a large number is divisible by 25 or not | Java program to find if a 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 ; main function
class IsDivisible { static boolean isDivisibleBy25 ( String str ) { int n = str . length ( ) ; if ( n == 1 ) return false ; return ( ( str . charAt ( n - 1 ) - '0' == 0 && str . charAt ( n - 2 ) - '0' == 0 ) || ( ( str . charAt ( n - 2 ) - '0' ) * 10 + ( str . charAt ( n - 1 ) - '0' ) ) % 25 == 0 ) ; } public static void main ( String [ ] args ) { String str = "76955" ; if ( isDivisibleBy25 ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Check a large number is divisible by 16 or not | Java program to find if a 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
import java . io . * ; class GFG { static boolean check ( String str ) { int n = str . length ( ) ; if ( n == 0 && n == 1 ) return false ; if ( n == 2 ) return ( ( ( str . charAt ( n - 2 ) - '0' ) * 10 + ( str . charAt ( n - 1 ) - '0' ) ) % 16 == 0 ) ; if ( n == 3 ) return ( ( ( str . charAt ( n - 3 ) - '0' ) * 100 + ( str . charAt ( n - 2 ) - '0' ) * 10 + ( str . charAt ( n - 1 ) - '0' ) ) % 16 == 0 ) ; int last = str . charAt ( n - 1 ) - '0' ; int second_last = str . charAt ( n - 2 ) - '0' ; int third_last = str . charAt ( n - 3 ) - '0' ; int fourth_last = str . charAt ( n - 4 ) - '0' ; return ( ( fourth_last * 1000 + third_last * 100 + second_last * 10 + last ) % 16 == 0 ) ; } public static void main ( String args [ ] ) { String str = "769528" ; if ( check ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No ▁ " ) ; } }
Find Index of given fibonacci number in constant time | A simple Java 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 program to test above function
import java . io . * ; class GFG { static int findIndex ( int n ) { if ( n <= 1 ) return n ; int a = 0 , b = 1 , c = 1 ; int res = 1 ; while ( c < n ) { c = a + b ; res ++ ; a = b ; b = c ; } return res ; } public static void main ( String [ ] args ) { int result = findIndex ( 21 ) ; System . out . println ( result ) ; } }
Date after adding given number of days to the given date | Java program to find date after adding given number of days . ; 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 stjan ) . ; 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 . ; Driven Program
class GFG { static int m2 , d2 ; static boolean isLeap ( int y ) { if ( y % 100 != 0 && y % 4 == 0 y % 400 == 0 ) return true ; return false ; } static int offsetDays ( int d , int m , int y ) { int 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 ; } static void revoffsetDays ( int offset , int y ) { int [ ] month = { 0 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ; if ( isLeap ( y ) ) month [ 2 ] = 29 ; int i ; for ( i = 1 ; i <= 12 ; i ++ ) { if ( offset <= month [ i ] ) break ; offset = offset - month [ i ] ; } d2 = offset ; m2 = i ; } static void addDays ( int d1 , int m1 , int y1 , int x ) { int offset1 = offsetDays ( d1 , m1 , y1 ) ; int remDays = isLeap ( y1 ) ? ( 366 - offset1 ) : ( 365 - offset1 ) ; int y2 , offset2 = 0 ; if ( x <= remDays ) { y2 = y1 ; offset2 = offset1 + x ; } else { x -= remDays ; y2 = y1 + 1 ; int y2days = isLeap ( y2 ) ? 366 : 365 ; while ( x >= y2days ) { x -= y2days ; y2 ++ ; y2days = isLeap ( y2 ) ? 366 : 365 ; } offset2 = x ; } revoffsetDays ( offset2 , y2 ) ; System . out . println ( " d2 ▁ = ▁ " + d2 + " , ▁ m2 ▁ = ▁ " + m2 + " , ▁ y2 ▁ = ▁ " + y2 ) ; } public static void main ( String [ ] args ) { int d = 14 , m = 3 , y = 2015 ; int x = 366 ; addDays ( d , m , y , x ) ; } }
Determine whether a given number is a Hyperperfect Number | Java program to check whether a given number is k - hyperperfect ; 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 ) )
import java . io . * ; class GFG { static int divisorSum ( int N , int K ) { int sum = 0 ; for ( int i = 2 ; i <= Math . ceil ( Math . sqrt ( N ) ) ; i ++ ) if ( N % i == 0 ) sum += ( i + N / i ) ; return sum ; } static boolean isPrime ( int n ) { if ( n == 1 n == 0 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static boolean isHyperPerfect ( int N , int K ) { int sum = divisorSum ( N , K ) ; if ( ( 1 + K * ( sum ) ) == N ) return true ; else return false ; } public static void main ( String [ ] args ) { int N1 = 1570153 , K1 = 12 ; int N2 = 321 , K2 = 3 ; if ( isHyperPerfect ( N1 , K1 ) ) System . out . println ( N1 + " ▁ is ▁ " + K1 + " - HyperPerfect " ) ; else System . out . println ( N1 + " ▁ is ▁ not ▁ " + K1 + " - HyperPerfect " ) ; if ( isHyperPerfect ( N2 , K2 ) ) System . out . println ( N2 + " ▁ is ▁ " + K2 + " - HyperPerfect " ) ; else System . out . println ( N2 + " ▁ is ▁ not ▁ " + K2 + " - HyperPerfect " ) ; } }
Given a number n , find the first k digits of n ^ n | Java program to 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 function
import java . util . * ; import java . lang . * ; import java . io . * ; class KDigitSquare { public static long firstkdigits ( int n , int k ) { double product = n * Math . log10 ( n ) ; double decimal_part = product - Math . floor ( product ) ; decimal_part = Math . pow ( 10 , decimal_part ) ; double digits = Math . pow ( 10 , k - 1 ) , i = 0 ; return ( ( long ) ( decimal_part * digits ) ) ; } public static void main ( String [ ] args ) { int n = 1450 ; int k = 6 ; System . out . println ( firstkdigits ( n , k ) ) ; } }
Generate k digit numbers with digits in strictly increasing order | Java program to generate well ordered numbers with k digits . ; 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
class Generate { static void printWellOrdered ( int number , int x , int k ) { if ( k == 0 ) { System . out . print ( number + " ▁ " ) ; return ; } for ( int i = ( x + 1 ) ; i < 10 ; i ++ ) printWellOrdered ( number * 10 + i , i , k - 1 ) ; } static void generateWellOrdered ( int k ) { printWellOrdered ( 0 , 0 , k ) ; } public static void main ( String [ ] args ) { int k = 3 ; generateWellOrdered ( k ) ; } }
Multiply large integers under large modulo | Java program of finding modulo multiplication ; 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 >>= 1 ; b = b / 2 ; Driver code
class GFG { static long moduloMultiplication ( long a , long b , long mod ) { long res = 0 ; a %= mod ; while ( b > 0 ) { if ( ( b & 1 ) > 0 ) { res = ( res + a ) % mod ; } a = ( 2 * a ) % mod ; } return res ; } public static void main ( String [ ] args ) { long a = 10123465234878998L ; long b = 65746311545646431L ; long m = 10005412336548794L ; System . out . print ( moduloMultiplication ( a , b , m ) ) ; } }
Number of occurrences of 2 as a digit in numbers from 0 to n | Java code for above implementation ; Driver code
class GFG { static int numberOf2sinRange ( int n ) { String s = " " ; for ( int i = 0 ; i < n + 1 ; i ++ ) s += String . valueOf ( i ) ; int count = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '2' ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { int n = 30 ; System . out . println ( numberOf2sinRange ( n ) ) ; } }
Number of occurrences of 2 as a digit in numbers from 0 to n | Java program to count 2 s 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
class GFG { static int count2sinRangeAtDigit ( int number , int d ) { int powerOf10 = ( int ) Math . pow ( 10 , d ) ; int nextPowerOf10 = powerOf10 * 10 ; int right = number % powerOf10 ; int roundDown = number - number % nextPowerOf10 ; int roundup = roundDown + nextPowerOf10 ; int digit = ( number / powerOf10 ) % 10 ; if ( digit < 2 ) { return roundDown / 10 ; } if ( digit == 2 ) { return roundDown / 10 + right + 1 ; } return roundup / 10 ; } static int numberOf2sinRange ( int number ) { String convert ; convert = String . valueOf ( number ) ; String s = convert ; int len = s . length ( ) ; int count = 0 ; for ( int digit = 0 ; digit < len ; digit ++ ) { count += count2sinRangeAtDigit ( number , digit ) ; } return count ; } public static void main ( String [ ] args ) { System . out . println ( numberOf2sinRange ( 22 ) ) ; System . out . println ( numberOf2sinRange ( 100 ) ) ; } }
Program to find Star number | Java program to find star number ; Returns n - th star number ; Driver code
import java . io . * ; class GFG { static int findStarNum ( int n ) { return ( 6 * n * ( n - 1 ) + 1 ) ; } public static void main ( String args [ ] ) { int n = 3 ; System . out . println ( findStarNum ( n ) ) ; } }
Check if a large number is divisible by 5 or not | Java program to find if a 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 . ; main function
class IsDivisible { static boolean isDivisibleBy5 ( String str ) { int n = str . length ( ) ; return ( ( ( str . charAt ( n - 1 ) - '0' ) == 0 ) || ( ( str . charAt ( n - 1 ) - '0' ) == 5 ) ) ; } public static void main ( String [ ] args ) { String str = "76955" ; if ( isDivisibleBy5 ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Tidy Number ( Digits in non | Java program to check if a number is Tidy or not . ; 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 method
class Test { static boolean isTidy ( int num ) { int prev = 10 ; while ( num != 0 ) { int rem = num % 10 ; num /= 10 ; if ( rem > prev ) return false ; prev = rem ; } return true ; } public static void main ( String [ ] args ) { int num = 1556 ; System . out . println ( isTidy ( num ) ? " Yes " : " No " ) ; } }
Nth Square free number | Java Program to find the 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 ; driven code
import java . io . * ; class GFG { public static int squareFree ( int n ) { int cnt = 0 ; for ( int i = 1 ; ; i ++ ) { boolean isSqFree = true ; for ( int j = 2 ; j * j <= i ; j ++ ) { if ( i % ( j * j ) == 0 ) { isSqFree = false ; break ; } } if ( isSqFree == true ) { cnt ++ ; if ( cnt == n ) return i ; } } } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( " " + squareFree ( n ) ) ; } }
Nth Square free number | Java Program to find the 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
import java . util . * ; class GFG { static int MAX_PRIME = 100000 ; static int MAX_RES = 2000000000 ; static void SieveOfEratosthenes ( Vector < Long > a ) { boolean prime [ ] = new boolean [ MAX_PRIME + 1 ] ; Arrays . fill ( prime , true ) ; for ( int p = 2 ; p * p <= MAX_PRIME ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX_PRIME ; i += p ) prime [ i ] = false ; } } for ( int p = 2 ; p <= MAX_PRIME ; p ++ ) if ( prime [ p ] ) a . add ( ( long ) p ) ; } static long countSquares ( long i , long cur , long k , Vector < Long > a ) { long square = a . get ( ( int ) i ) * a . get ( ( int ) ( i ) ) ; long newCur = square * cur ; if ( newCur > k ) return 0 ; long cnt = k / ( newCur ) ; cnt += countSquares ( i + 1 , cur , k , a ) ; cnt -= countSquares ( i + 1 , newCur , k , a ) ; return cnt ; } static long squareFree ( long n ) { Vector < Long > a = new Vector < > ( ) ; SieveOfEratosthenes ( a ) ; long low = 1 ; long high = MAX_RES ; while ( low < high ) { long mid = low + ( high - low ) / 2 ; long c = mid - countSquares ( 0 , 1 , mid , a ) ; if ( c < n ) low = mid + 1 ; else high = mid ; } return low ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( squareFree ( n ) ) ; } }
Find if n can be written as product of k numbers | Java program to find if it is possible to write a number n as product of exactly k positive numbers greater than 1. ; Prints k factors of n if n can be written as multiple of k numbers . Else prints - 1. ; A vector to store all prime factors of n ; Insert all 2 's in list ; n must be odd at this point So we skip one element ( i = i + 2 ) ; This is to handle when n > 2 and n is prime ; If size ( P ) < k , k factors are not possible ; printing first k - 1 factors ; calculating and printing product of rest of numbers ; Driver code
import java . util . * ; class GFG { static void kFactors ( int n , int k ) { ArrayList < Integer > P = new ArrayList < Integer > ( ) ; while ( n % 2 == 0 ) { P . add ( 2 ) ; n /= 2 ; } for ( int i = 3 ; i * i <= n ; i = i + 2 ) { while ( n % i == 0 ) { n = n / i ; P . add ( i ) ; } } if ( n > 2 ) P . add ( n ) ; if ( P . size ( ) < k ) { System . out . println ( " - 1" ) ; return ; } for ( int i = 0 ; i < k - 1 ; i ++ ) System . out . print ( P . get ( i ) + " , ▁ " ) ; int product = 1 ; for ( int i = k - 1 ; i < P . size ( ) ; i ++ ) product = product * P . get ( i ) ; System . out . println ( product ) ; } public static void main ( String [ ] args ) { int n = 54 , k = 3 ; kFactors ( n , k ) ; } }
Largest number smaller than or equal to n and digits in non | Java program for brute force approach to find largest number having digits in non - decreasing order . ; Returns the required number ; loop to recursively check the numbers less than or equal to given number ; Keep traversing digits from right to left . For every digit check if it is smaller than prev_dig ; We found the required number ; Driver Code
import java . io . * ; class GFG { static int nondecdigits ( int n ) { int x = 0 ; for ( x = n ; x >= 1 ; x -- ) { int no = x ; int prev_dig = 11 ; boolean flag = true ; while ( no != 0 ) { if ( prev_dig < no % 10 ) { flag = false ; break ; } prev_dig = no % 10 ; no /= 10 ; } if ( flag == true ) break ; } return x ; } public static void main ( String [ ] args ) { int n = 200 ; System . out . println ( nondecdigits ( n ) ) ; } }
Largest number smaller than or equal to n and digits in non | Java program for efficient approach to find largest number having digits in non - decreasing order . ; Prints the largest number smaller than s and digits in non - decreasing order . ; array to store digits of number ; conversion of characters of string int number ; variable holds the value of index after which all digits are set 9 ; Checking the condition if the digit is less than its left digit ; If first digit is 0 no need to print it ; Driver code
import java . util . * ; class GFG { static void nondecdigits ( String s ) { int m = s . length ( ) ; int [ ] a = new int [ m + 1 ] ; for ( int i = 0 ; i < m ; i ++ ) a [ i ] = ( int ) s . charAt ( i ) - ( int ) '0' ; int level = m - 1 ; for ( int i = m - 1 ; i > 0 ; i -- ) { if ( a [ i ] < a [ i - 1 ] ) { a [ i - 1 ] -- ; level = i - 1 ; } } if ( a [ 0 ] != 0 ) { for ( int i = 0 ; i <= level ; i ++ ) System . out . print ( a [ i ] ) ; for ( int i = level + 1 ; i < m ; i ++ ) System . out . print ( "9" ) ; } else { for ( int i = 1 ; i < level ; i ++ ) System . out . print ( a [ i ] ) ; for ( int i = level + 1 ; i < m ; i ++ ) System . out . print ( "9" ) ; } } public static void main ( String [ ] args ) { String n = "200" ; nondecdigits ( n ) ; } }
Count Divisors of n in O ( n ^ 1 / 3 ) | JAVA implementation of Naive method to count all divisors ; function to count the divisors ; If divisors are equal , count only one ; else Otherwise count both ; Driver program to test above function
import java . io . * ; import java . math . * ; class GFG { static int countDivisors ( int n ) { int cnt = 0 ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) cnt ++ ; cnt = cnt + 2 ; } } return cnt ; } public static void main ( String args [ ] ) { System . out . println ( " Total ▁ distinct ▁ " + " divisors ▁ of ▁ 100 ▁ are ▁ : ▁ " + countDivisors ( 100 ) ) ; } }
Check if the door is open or closed | java implementation of doors open or closed ; Function to check whether ' n ' has even number of factors or not ; if ' n ' is a perfect square it has odd number of factors ; else ' n ' has even number of factors ; Function to find and print status of each door ; If even number of factors final status is closed ; else odd number of factors final status is open ; Driver program
import java . io . * ; class GFG { static boolean hasEvenNumberOfFactors ( int n ) { double root_n = Math . sqrt ( n ) ; if ( ( root_n * root_n ) == n ) return false ; return true ; } static void printStatusOfDoors ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { if ( hasEvenNumberOfFactors ( i ) ) System . out . print ( " closed " + " ▁ " ) ; else System . out . print ( " open " + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 5 ; printStatusOfDoors ( n ) ; } }