Trial division Algorithm for Prime Factorization | Java implementation of Trial Division Algorithm ; Function to check if a number is a prime number or not ; Initializing with the value 2 from where the number is checked ; Computing the square root of the number N ; While loop till the square root of N ; If any of the numbers between [ 2 , sqrt ( N ) ] is a factor of N Then the number is composite ; If none of the numbers is a factor , then it is a prime number ; Driver Code ; To check if a number is a prime or not
import java . util . * ; class GFG { static int TrialDivision ( int N ) { int i = 2 ; int k = ( int ) Math . ceil ( Math . sqrt ( N ) ) ; while ( i <= k ) { if ( N % i == 0 ) return 0 ; i += 1 ; } return 1 ; } public static void main ( String [ ] args ) { int N = 49 ; int p = TrialDivision ( N ) ; if ( p != 0 ) System . out . print ( " Prime " ) ; else System . out . print ( " Composite " ) ; } }