Count all the numbers in a range with smallest factor as K
Given a range of integer from ‘a’ to ‘b’ . Our task is to calculate the amount of numbers from the interval [ a, b ], that are not divisible by any number between 2 and k – 1 and yet divisible by k .
Note : We do not have to consider a divisor equal to one.
Examples:
Input : a = 12, b = 23, k = 3 Output : 2 Between [12, 23], 15 and 21 are the only number which are divisible k and not divisible by any number between 2 and k - 1. Input : a = 1, b = 80, k = 7 Output : 3
Approach : Below is the step by step algorithm to solve this problem:
- A number is divisible only by k and not by any number less than k only if k is a prime number.
- Traverse through each number from a to b to check if the number has the smallest factor as a prime number k.
- Count all such numbers in the range whose smallest factor is a prime number k.
Below is the implementation of the above approach:
C++
// C++ program to find the count of numbers in a range // whose smallest factor is K #include <bits/stdc++.h> using namespace std; // Function to check if k is a prime number or not bool isPrime(int k) { // Corner case if (k <= 1) return false; // Check from 2 to n-1 for (int i = 2; i < k; i++) if (k % i == 0) return false; return true; } // Function to check if a number is not divisible // by any number between 2 and K-1 int check(int num, int k) { int flag = 1; // to check if the num is divisible by // any numbers between 2 and k - 1 for (int i = 2; i < k; i++) { if (num % i == 0) flag = 0; } if (flag == 1) { // if not divisible by any number between // 2 and k - 1 // but divisible by k if (num % k == 0) return 1; else return 0; } else return 0; } // Function to find count of numbers in range [a, b] // with smallest factor as K int findCount(int a, int b, int k) { int count = 0; // a number can be divisible only by k and // not by any number less than k only // if k is a prime if (!isPrime(k)) return 0; else { int ans; for (int i = a; i <= b; i++) { // to check if a number has // smallest factor as K ans = check(i, k); if (ans == 1) count++; else continue; } } return count; } // Driver code int main() { int a = 2020, b = 6300, k = 29; cout << findCount(a, b, k); return 0; } |
Java
// Java program to find the count of numbers in a range // whose smallest factor is K public class GFG { // Function to check if k is a prime number or not static boolean isPrime(int k) { // Corner case if (k <= 1) return false; // Check from 2 to n-1 for (int i = 2; i < k; i++) if (k % i == 0) return false; return true; } // Function to check if a number is not divisible // by any number between 2 and K-1 static int check(int num, int k) { int flag = 1; // to check if the num is divisible by // any numbers between 2 and k - 1 for (int i = 2; i < k; i++) { if (num % i == 0) flag = 0; } if (flag == 1) { // if not divisible by any number between // 2 and k - 1 // but divisible by k if (num % k == 0) return 1; else return 0; } else return 0; } // Function to find count of numbers in range [a, b] // with smallest factor as K static int findCount(int a, int b, int k) { int count = 0; // a number can be divisible only by k and // not by any number less than k only // if k is a prime if (!isPrime(k)) return 0; else { int ans; for (int i = a; i <= b; i++) { // to check if a number has // smallest factor as K ans = check(i, k); if (ans == 1) count++; else continue; } } return count; } // Driver code public static void main(String args[]) { int a = 2020, b = 6300, k = 29; System.out.println(findCount(a, b, k)); } // This Code is contributed by ANKITRAI1 } |
Python 3
# Python 3 program to find the count # of numbers in a range whose smallest # factor is K # Function to check if k is # a prime number or not def isPrime( k): # Corner case if (k <= 1): return False # Check from 2 to n-1 for i in range(2, k): if (k % i == 0): return false return True # Function to check if a number # is not divisible by any number # between 2 and K-1 def check(num, k): flag = 1 # to check if the num is divisible # by any numbers between 2 and k - 1 for i in range(2, k) : if (num % i == 0): flag = 0 if (flag == 1) : # if not divisible by any # number between 2 and k - 1 # but divisible by k if (num % k == 0): return 1 else: return 0 else: return 0 # Function to find count of # numbers in range [a, b] # with smallest factor as K def findCount(a, b, k): count = 0 # a number can be divisible only # by k and not by any number # less than k only if k is a prime if (not isPrime(k)): return 0 else : for i in range(a, b + 1) : # to check if a number has # smallest factor as K ans = check(i, k) if (ans == 1): count += 1 else: continue return count # Driver code if __name__ == "__main__": a = 2020 b = 6300 k = 29 print(findCount(a, b, k)) # This code is contributed # by ChitraNayal |
C#
// C# program to find the count // of numbers in a range whose // smallest factor is K using System; class GFG { // Function to check if k is // a prime number or not static bool isPrime(int k) { // Corner case if (k <= 1) return false; // Check from 2 to n-1 for (int i = 2; i < k; i++) if (k % i == 0) return false; return true; } // Function to check if a number // is not divisible by any number // between 2 and K-1 static int check(int num, int k) { int flag = 1; // to check if the num is divisible by // any numbers between 2 and k - 1 for (int i = 2; i < k; i++) { if (num % i == 0) flag = 0; } if (flag == 1) { // if not divisible by any // number between 2 and k - 1 // but divisible by k if (num % k == 0) return 1; else return 0; } else return 0; } // Function to find count of // numbers in range [a, b] // with smallest factor as K static int findCount(int a, int b, int k) { int count = 0; // a number can be divisible only // by k and not by any number less // than k only if k is a prime if (!isPrime(k)) return 0; else { int ans; for (int i = a; i <= b; i++) { // to check if a number has // smallest factor as K ans = check(i, k); if (ans == 1) count++; else continue; } } return count; } // Driver code public static void Main() { int a = 2020, b = 6300, k = 29; Console.WriteLine(findCount(a, b, k)); } } // This code is contributed // by Akanksha Rai(Abby_akku) |
PHP
<?php // PHP program to find the count // of numbers in a range whose // smallest factor is K // Function to check if k is // a prime number or not function isPrime($k) { // Corner case if ($k <= 1) return false; // Check from 2 to n-1 for ($i = 2; $i < $k; $i++) if ($k % $i == 0) return false; return true; } // Function to check if a number // is not divisible by any number // between 2 and K-1 function check($num, $k) { $flag = 1; // to check if the num is divisible // by any numbers between 2 and k - 1 for ($i = 2; $i < $k; $i++) { if ($num % $i == 0) $flag = 0; } if ($flag == 1) { // if not divisible by any // number between 2 and k - 1 // but divisible by k if ($num % $k == 0) return 1; else return 0; } else return 0; } // Function to find count of // numbers in range [a, b] // with smallest factor as K function findCount($a, $b, $k) { $count = 0; // a number can be divisible // only by k and not by any // number less than k only // if k is a prime if (!isPrime($k)) return 0; else { for ($i = $a; $i <= $b; $i++) { // to check if a number has // smallest factor as K $ans = check($i, $k); if ($ans == 1) $count++; else continue; } } return $count; } // Driver code $a = 2020; $b = 6300; $k = 29; echo (findCount($a, $b, $k)); // This code is contributed // by Shivi_Aggarwal ?> |
28
Recommended Posts:
- Count all the numbers less than 10^6 whose minimum prime factor is N
- Find kth smallest number in range [1, n] when all the odd numbers are deleted
- Count Odd and Even numbers in a range from L to R
- Count of numbers having only 1 set bit in the range [0, n]
- Smallest number S such that N is a factor of S factorial or S!
- Count factorial numbers in a given range
- Count numbers in range 1 to N which are divisible by X but not by Y
- Count the numbers divisible by 'M' in a given range
- Count of numbers from range [L, R] whose sum of digits is Y
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Count of common multiples of two numbers in a range
- Sum of numbers in a range [L, R] whose count of divisors is prime
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Count numbers from range whose prime factors are only 2 and 3
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.



