Smallest prime divisor of a number | C ++ program to count the number of subarrays that having 1 ; Function to find the smallest divisor ; if divisible by 2 ; iterate from 3 to sqrt ( n ) ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int smallestDivisor ( int n ) { if ( n % 2 == 0 ) return 2 ; for ( int i = 3 ; i * i <= n ; i += 2 ) { if ( n % i == 0 ) return i ; } return n ; } int main ( ) { int n = 31 ; cout << smallestDivisor ( n ) ; return 0 ; }