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
- Given number of matches played, find number of teams in tournament
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Number which has the maximum number of distinct prime factors in the range M to N
- Number of times the largest perfect square number can be subtracted from N
- Super Ugly Number (Number whose prime factors are in given set)
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Program to Convert Octal Number to Binary Number
- Check if a number is divisible by all prime divisors of another number
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Count Number of animals in a zoo from given number of head and legs
- Find count of digits in a number that divide the number
- Find the smallest number whose digits multiply to a given number n
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.