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++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int k)
{
if (k <= 1)
return false ;
for ( int i = 2; i < k; i++)
if (k % i == 0)
return false ;
return true ;
}
int check( int num, int k)
{
int flag = 1;
for ( int i = 2; i < k; i++) {
if (num % i == 0)
flag = 0;
}
if (flag == 1) {
if (num % k == 0)
return 1;
else
return 0;
}
else
return 0;
}
int findCount( int a, int b, int k)
{
int count = 0;
if (!isPrime(k))
return 0;
else {
int ans;
for ( int i = a; i <= b; i++) {
ans = check(i, k);
if (ans == 1)
count++;
else
continue ;
}
}
return count;
}
int main()
{
int a = 2020, b = 6300, k = 29;
cout << findCount(a, b, k);
return 0;
}
|
Java
public class GFG {
static boolean isPrime( int k)
{
if (k <= 1 )
return false ;
for ( int i = 2 ; i < k; i++)
if (k % i == 0 )
return false ;
return true ;
}
static int check( int num, int k)
{
int flag = 1 ;
for ( int i = 2 ; i < k; i++) {
if (num % i == 0 )
flag = 0 ;
}
if (flag == 1 ) {
if (num % k == 0 )
return 1 ;
else
return 0 ;
}
else
return 0 ;
}
static int findCount( int a, int b, int k)
{
int count = 0 ;
if (!isPrime(k))
return 0 ;
else {
int ans;
for ( int i = a; i <= b; i++) {
ans = check(i, k);
if (ans == 1 )
count++;
else
continue ;
}
}
return count;
}
public static void main(String args[])
{
int a = 2020 , b = 6300 , k = 29 ;
System.out.println(findCount(a, b, k));
}
}
|
Python 3
def isPrime( k):
if (k < = 1 ):
return False
for i in range ( 2 , k):
if (k % i = = 0 ):
return false
return True
def check(num, k):
flag = 1
for i in range ( 2 , k) :
if (num % i = = 0 ):
flag = 0
if (flag = = 1 ) :
if (num % k = = 0 ):
return 1
else :
return 0
else :
return 0
def findCount(a, b, k):
count = 0
if ( not isPrime(k)):
return 0
else :
for i in range (a, b + 1 ) :
ans = check(i, k)
if (ans = = 1 ):
count + = 1
else :
continue
return count
if __name__ = = "__main__" :
a = 2020
b = 6300
k = 29
print (findCount(a, b, k))
|
C#
using System;
class GFG
{
static bool isPrime( int k)
{
if (k <= 1)
return false ;
for ( int i = 2; i < k; i++)
if (k % i == 0)
return false ;
return true ;
}
static int check( int num, int k)
{
int flag = 1;
for ( int i = 2; i < k; i++)
{
if (num % i == 0)
flag = 0;
}
if (flag == 1)
{
if (num % k == 0)
return 1;
else
return 0;
}
else
return 0;
}
static int findCount( int a, int b, int k)
{
int count = 0;
if (!isPrime(k))
return 0;
else
{
int ans;
for ( int i = a; i <= b; i++)
{
ans = check(i, k);
if (ans == 1)
count++;
else
continue ;
}
}
return count;
}
public static void Main()
{
int a = 2020, b = 6300, k = 29;
Console.WriteLine(findCount(a, b, k));
}
}
|
PHP
<?php
function isPrime( $k )
{
if ( $k <= 1)
return false;
for ( $i = 2; $i < $k ; $i ++)
if ( $k % $i == 0)
return false;
return true;
}
function check( $num , $k )
{
$flag = 1;
for ( $i = 2; $i < $k ; $i ++)
{
if ( $num % $i == 0)
$flag = 0;
}
if ( $flag == 1)
{
if ( $num % $k == 0)
return 1;
else
return 0;
}
else
return 0;
}
function findCount( $a , $b , $k )
{
$count = 0;
if (!isPrime( $k ))
return 0;
else
{
for ( $i = $a ; $i <= $b ; $i ++)
{
$ans = check( $i , $k );
if ( $ans == 1)
$count ++;
else
continue ;
}
}
return $count ;
}
$a = 2020;
$b = 6300;
$k = 29;
echo (findCount( $a , $b , $k ));
?>
|
Javascript
<script>
function isPrime(k)
{
if (k <= 1)
return false ;
for (let i = 2; i < k; i++)
if (k % i == 0)
return false ;
return true ;
}
function check(num,k)
{
let flag = 1;
for (let i = 2; i < k; i++) {
if (num % i == 0)
flag = 0;
}
if (flag == 1) {
if (num % k == 0)
return 1;
else
return 0;
}
else
return 0;
}
function findCount(a,b,k)
{
let count = 0;
if (!isPrime(k))
return 0;
else {
let ans;
for (let i = a; i <= b; i++) {
ans = check(i, k);
if (ans == 1)
count++;
else
continue ;
}
}
return count;
}
let a = 2020, b = 6300, k = 29;
document.write(findCount(a, b, k));
</script>
|
Time Complexity : O(b-a)*k
Auxiliary Space : O(1)
Last Updated :
03 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...