Trial division Algorithm for Prime Factorization | 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
function TrialDivision ( N ) { let i = 2 ; let k = Math . ceil ( Math . sqrt ( N ) ) ; while ( i <= k ) { if ( N % i == 0 ) return 0 ; i += 1 ; } return 1 ; } let N = 49 ; let p = TrialDivision ( N ) ; if ( p ) document . write ( " " ) ; else document . write ( " " ) ;