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.
# Python3 to check if n is a k-rough
# number or not
# Finds primes by Sieve of Eratosthenes method
isPrime = [True] * (n + 1);
i = 2;
while (i * i <= n): # If isPrime[i] is not changed, # then it is prime if (isPrime[i] == True): # Update all multiples of p j = i * 2; while (j <= n): isPrime[j] = False; j += i; i += 1; # Forming array of the # prime numbers found primes = ; for i in range(2, n + 1): if (isPrime[i]): primes.append(i); return primes; # Checking whether a # number is k-rough or not def isRough(n, k): primes = getPrimes(n); # Finding minimum # prime factor of n min_pf = n; for i in range(len(primes)): if (n % primes[i] == 0): min_pf = primes[i]; # Return true if minimum # prime factor is greater # than or equal to k. Else # return false. return (min_pf >= k);
# Driver Code
n = 75;
k = 3;
if (isRough(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
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.
75 is a 3-rough number
- Count number of trailing zeros in Binary representation of a number using Bitset
- Count number of triplets with product equal to given number with duplicates allowed
- Find minimum number to be divided to make a number a perfect square
- Number which has the maximum number of distinct prime factors in the range M to N
- Given number of matches played, find number of teams in tournament
- Represent a number as a sum of maximum possible number of Prime Numbers
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Build Lowest Number by Removing n digits from a given number
- Print a number strictly less than a given number such that all its digits are distinct.
- Find the largest good number in the divisors of given number N
- Super Ugly Number (Number whose prime factors are in given set)
- Querying maximum number of divisors that a number in a given range has
- Minimum number of given powers of 2 required to represent a number
- Count number of ways to divide a number in 4 parts
- Number of digits to be removed to make a number divisible by 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 email@example.com. 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.