# Count all the numbers in a range with smallest factor as K

• Last Updated : 03 Mar, 2022

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:

1. A number is divisible only by k and not by any number less than k only if k is a prime number.
2. Traverse through each number from a to b to check if the number has the smallest factor as a prime number k.
3. 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 ``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

 ``

## Javascript

 ``

Output:

`28`

Time Complexity : O(b-a)*k
Auxiliary Space : O(1)

My Personal Notes arrow_drop_up