Smallest prime divisor of a number
Given a number N, find the smallest prime divisor of N.
Examples:
Input: 25
Output: 5Input: 31
Output: 31
Approach:
- Check if the number is divisible by 2 or not.
- Iterate from i = 3 to sqrt(N) and making a jump of 2.
- If any of the numbers divide N then it is the smallest prime divisor.
- If none of them divide, then N is the answer.
Below is the implementation of the above algorithm:
C++
// C++ program to count the number of // subarrays that having 1 #include <bits/stdc++.h> using namespace std; // Function to find the smallest divisor int smallestDivisor(int n) { // if divisible by 2 if (n % 2 == 0) return 2; // iterate from 3 to sqrt(n) for (int i = 3; i * i <= n; i += 2) { if (n % i == 0) return i; } return n; } // Driver Code int main() { int n = 31; cout << smallestDivisor(n); return 0; } |
chevron_right
filter_none
Java
// Java program to count the number of // subarrays that having 1 import java.io.*; class GFG { // Function to find the smallest divisor static int smallestDivisor(int n) { // if divisible by 2 if (n % 2 == 0) return 2; // iterate from 3 to sqrt(n) for (int i = 3; i * i <= n; i += 2) { if (n % i == 0) return i; } return n; } // Driver Code public static void main (String[] args) { int n = 31; System.out.println (smallestDivisor(n)); } } |
chevron_right
filter_none
Python3
# Python3 program to count the number # of subarrays that having 1 # Function to find the smallest divisor def smallestDivisor(n): # if divisible by 2 if (n % 2 == 0): return 2; # iterate from 3 to sqrt(n) i = 3; while(i * i <= n): if (n % i == 0): return i; i += 2; return n; # Driver Code n = 31; print(smallestDivisor(n)); # This code is contributed by mits |
chevron_right
filter_none
C#
// C# program to count the number // of subarrays that having 1 using System; class GFG { // Function to find the // smallest divisor static int smallestDivisor(int n) { // if divisible by 2 if (n % 2 == 0) return 2; // iterate from 3 to sqrt(n) for (int i = 3; i * i <= n; i += 2) { if (n % i == 0) return i; } return n; } // Driver Code static public void Main () { int n = 31; Console.WriteLine(smallestDivisor(n)); } } // This code is contributed // by Sach_Code |
chevron_right
filter_none
PHP
<?php // PHP program to count the number // of subarrays that having 1 // Function to find the smallest divisor function smallestDivisor($n) { // if divisible by 2 if ($n % 2 == 0) return 2; // iterate from 3 to sqrt(n) for ($i = 3; $i * $i <= $n; $i += 2) { if ($n % $i == 0) return $i; } return $n; } // Driver Code $n = 31; echo smallestDivisor($n); // This code is contributed by Sachin ?> |
chevron_right
filter_none
Output:
31
How to efficiently find prime factors of all numbers till n?
Please refer Least prime factor of numbers till n
Recommended Posts:
- Find the k-th smallest divisor of a natural number N
- Smallest divisor D of N such that gcd(D, M) is greater than 1
- Smallest prime number missing in an array
- Check if the first and last digit of the smallest number forms a prime
- Smallest Special Prime which is greater than or equal to a given number
- Greatest divisor which divides all natural number in range [L, R]
- Largest Divisor of a Number not divisible by a perfect square
- Queries to return the absolute difference between L-th smallest number and the R-th smallest number
- Next smallest prime palindrome
- Sum and product of k smallest and k largest prime numbers in the array
- Check if a prime number can be expressed as sum of two Prime Numbers
- Find coordinates of a prime number in a Prime Spiral
- Number which has the maximum number of distinct prime factors in the range M to N
- Find the smallest number whose digits multiply to a given number n
- Check if a number is divisible by all prime divisors of another number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



