A k-rough or k-jagged number is a number whose smallest prime factor is greater than or equal to the number ‘k’. Given numbers ‘n’ and ‘k’ as input, we are required to find whether ‘n; is a k-rough number or not.
Input : n = 10, k = 2
Output : 10 is a 2-rough number
Explanation: The prime factors of 10 are 2 and 5 Hence its smallest prime factor is 2 which is greater than or equal to k, i.e 2
Input : n = 55, k = 7
Output : 55 is not a 7-rough number
Explanation: The prime factors of 55 are 5 and 11 Hence its smallest prime factor is 5 which is not greater than or equal to k, i.e 7
It may be inferred from above that every positive integer, except 1, is a 2-rough number since their smallest prime factor is either 2(for even positive integers) or greater than 2(for odd positive integers).
- First find all prime numbers up to the number ‘n’.
- Next find the smallest prime factor of the number ‘n’ from its prime factorization.
- Check if the smallest prime factor is greater than or equal to ‘k’ or not.
75 is a 3-rough number
Efficient Solution :
The idea is based on Efficient program to print all prime factors of a given number.
- If n is divisible by 2 (smallest prime number), then we return true if k is smaller than or equal to 2. Else we return false.
- Then we one by one try all odd numbers. As soon as we find an odd number that divides n, we compare it with k and return true if the odd number is greater than or equal to k, else false. This solution works because if a prime number does not divide n, then its multiples will also not divide.
# Python3 program to check if given n
# is k-rough or not.
# Returns true if n is k rough else false
def isKRough(n, k):
# If n is even, then smallest
# prime factor becomes 2.
if(n % 2 == 0):
return (k <= 2); # n must be odd at this point. # So we can skip one element # (Note i = i +2) i = 3; while(i * i <= n): if(n % i == 0): return (i >= k);
i = i + 2;
return (n >= k);
# Driver Code
n = 75;
k = 3;
if (isKRough(n, k)):
print(n, “is a”, k, “-rough number”);
print(n, “is not a”, k, “-rough number”);
# This code is contributed by mits
75 is a 3-rough number
- Find minimum number to be divided to make a number a perfect square
- Count number of triplets with product equal to given number with duplicates allowed
- Number which has the maximum number of distinct prime factors in the range M to N
- Count Number of animals in a zoo from given number of head and legs
- Queries on sum of odd number digit sums of all the factors of a number
- Number of digits to be removed to make a number divisible by 3
- Build Lowest Number by Removing n digits from a given number
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Print a number strictly less than a given number such that all its digits are distinct.
- Represent a number as a sum of maximum possible number of Prime Numbers
- Program to Convert Octal Number to Binary Number
- Check if a number is divisible by all prime divisors of another number
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Find the largest good number in the divisors of given number N
- Count number of digits after decimal on dividing a 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 firstname.lastname@example.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.