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.
Examples :
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 2Input : 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).
Simple Approach
- 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.
C++
// CPP to check if n is a k-rough number // or not #include <bits/stdc++.h> using namespace std; // Finds primes by Sieve of Eratosthenes // method vector<int> getPrimes(int n) { int i, j; bool isPrime[n + 1]; memset(isPrime, true, sizeof(isPrime)); for (i = 2; i * i <= n; i++) { // If isPrime[i] is not changed, // then it is prime if (isPrime[i] == true) { // Update all multiples of p for (j = i * 2; j <= n; j += i) isPrime[j] = false; } } // Forming array of the prime numbers found vector<int> primes; for (i = 2; i <= n; i++) if (isPrime[i]) primes.push_back(i); return primes; } // Checking whether a number is k-rough or not bool isRough(int n, int k) { vector<int> primes = getPrimes(n); // Finding minimum prime factor of n int min_pf = n; for (int i = 0; i < primes.size(); i++) 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 Method int main() { int n = 75, k = 3; if (isRough(n, k)) cout << n << " is a " << k << "-rough number\n"; else cout << n << " is not a " << k << "-rough number\n"; return 0; } |
Java
// Java to check if n is // a k-rough number or not import java.io.*; import java.util.*; class GFG { // Finds primes by Sieve // of Eratosthenes method static ArrayList<Integer> getPrimes(int n) { int i, j; int []isPrime = new int[n + 1]; for(i = 0; i < n + 1; i++) isPrime[i] = 1; for (i = 2; i * i <= n; i++) { // If isPrime[i] is not // changed, then it is prime if (isPrime[i] == 1) { // Update all // multiples of p for (j = i * 2; j <= n; j += i) isPrime[j] = 0; } } // Forming array of the // prime numbers found ArrayList<Integer> primes = new ArrayList<Integer>(); for (i = 2; i <= n; i++) if (isPrime[i] == 1) primes.add(i); return primes; } // Checking whether a // number is k-rough or not static boolean isRough(int n, int k) { ArrayList<Integer> primes = getPrimes(n); // Finding minimum // prime factor of n int min_pf = n; for (int i = 0; i < primes.size(); i++) if (n % primes.get(i) == 0) min_pf = primes.get(i); // Return true if minimum // prime factor is greater // than or equal to k. Else // return false. return (min_pf >= k); } // Driver Code public static void main(String args[]) { int n = 75, k = 3; if (isRough(n, k)) System.out.print(n + " is a " + k + "-rough number\n"); else System.out.print(+ n + " is not a " + k + "-rough number\n"); } } // This code is contributed by // Manish Shaw.(manishshaw1) |
Python3
# Python3 to check if n is a k-rough # number or not # Finds primes by Sieve of Eratosthenes method def getPrimes(n): 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"); else: print(n, "is not a", k, "-rough number"); # This code is contributed by mits |
C#
// C# to check if n is a k-rough number // or not using System; using System.Collections.Generic; using System.Linq; class GFG { // Finds primes by Sieve of Eratosthenes // method static List<int> getPrimes(int n) { int i, j; int []isPrime = new int[n + 1]; for(i = 0; i < n + 1; i++) isPrime[i] = 1; // Array.Clear(isPrime, 1, isPrime.Length); for (i = 2; i * i <= n; i++) { // If isPrime[i] is not changed, // then it is prime if (isPrime[i] == 1) { // Update all multiples of p for (j = i * 2; j <= n; j += i) isPrime[j] = 0; } } // Forming array of the prime numbers // found List<int> primes = new List<int>(); for (i = 2; i <= n; i++) if (isPrime[i] == 1) primes.Add(i); return primes; } // Checking whether a number is k-rough // or not static bool isRough(int n, int k) { List<int> primes = getPrimes(n); // Finding minimum prime factor of n int min_pf = n; for (int i = 0; i < primes.Count; i++) 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 Method static void Main() { int n = 75, k = 3; if (isRough(n, k)) Console.Write(n + " is a " + k + "-rough number\n"); else Console.Write(+ n + " is not a " + k + "-rough number\n"); } } // This code is contributed by manishshaw. |
PHP
<?php // PHP to check if n is // a k-rough number or not // Finds primes by Sieve // of Eratosthenes method function getPrimes($n) { $isPrime = array_fill(0, ($n + 1), true); for ($i = 2; $i * $i <= $n; $i++) { // If isPrime[i] is // not changed, // then it is prime if ($isPrime[$i] == true) { // Update all // multiples of p for ($j = $i * 2; $j <= $n; $j += $i) $isPrime[$j] = false; } } // Forming array of the // prime numbers found $primes = array(); $k = 0; for ($i = 2; $i <= $n; $i++) if ($isPrime[$i]) $primes[$k++]=$i; return $primes; } // Checking whether a // number is k-rough or not function isRough($n, $k) { $primes = getPrimes($n); // Finding minimum // prime factor of n $min_pf = $n; for ($i = 0; $i < count($primes); $i++) 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)) echo $n . " is a " . $k . "-rough number\n"; else echo $n . " is not a " . $k . "-rough number\n"; // This code is contributed by mits ?> |
Output:
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.
C++
// CPP program to check if given n is // k-rough or not. # include <bits/stdc++.h> using namespace std; // Returns true if n is k rough else false bool isKRough(int n, int 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) for (int i = 3; i*i <= n; i = i+2) if (n%i == 0) return (i >= k); return (n >= k); } /* Driver program to test above function */int main() { int n = 75, k = 3; if (isKRough(n, k)) cout << n << " is a " << k << "-rough number\n"; else cout << n << " is not a " << k << "-rough number\n"; return 0; } |
Java
// Java program to check if given n is // k-rough or not. class GFG { // Returns true if n is k rough else false static boolean isKRough(int n, int 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) for (int i = 3; i*i <= n; i = i + 2) if (n % i == 0) return (i >= k); return (n >= k); } /* Driver program to test above function */public static void main(String[] args) { int n = 75, k = 3; if (isKRough(n, k)) System.out.println(n + " is a " + k + "-rough number"); else System.out.println(n + " is not a " + k + "-rough number"); } } // This code is contributed by Smitha |
Python3
# 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"); else: print(n, "is not a", k, "-rough number"); # This code is contributed by mits |
C#
// C# program to check if given n is // k-rough or not. using System; class GFG { // Returns true if n is k rough else false static bool isKRough(int n, int 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) for (int i = 3; i*i <= n; i = i + 2) if (n % i == 0) return (i >= k); return (n >= k); } /* Driver program to test above function */public static void Main() { int n = 75, k = 3; if (isKRough(n, k)) Console.Write(n + " is a " + k + "-rough number\n"); else Console.Write(n + " is not a " + k + "-rough number\n"); } } // This code is contributed by Smitha |
PHP
<?php // PHP program to check if // given n is k-rough or not. // Returns true if n is k // rough else false function 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) for ($i = 3; $i * $i <= $n; $i = $i + 2) if ($n % $i == 0) return ($i >= $k); return ($n >= $k); } // Driver Code $n = 75; $k = 3; if (isKRough($n, $k)) echo $n . " is a " . $k . "-rough number\n"; else echo $n . " is not a " . $k . "-rough number\n"; // This code is contributed by Sam007 ?> |
Output :
75 is a 3-rough number
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Number of factors of very large number N modulo M where M is any prime number
- Find minimum number to be divided to make a number a perfect square
- How to check if a given number is Fibonacci number?
- Find the smallest number whose digits multiply to a given number n
- Find n'th number in a number system with only 3 and 4
- Build Lowest Number by Removing n digits from a given number
- Count number of ways to divide a number in 4 parts
- Querying maximum number of divisors that a number in a given range has
- Check if a number is a power of another number
- Find the Largest number with given number of digits and sum of digits
- Finding number of digits in n'th Fibonacci number
- Smallest number by rearranging digits of a given number
- Super Ugly Number (Number whose prime factors are in given set)
- Number with maximum number of prime factors
- Convert a number m to n using minimum number of given operations
- Determine whether a given number is a Hyperperfect Number
- Find count of digits in a number that divide the number
- Number of times the largest perfect square number can be subtracted from N
- Find if a number is divisible by every number in a list
- Round-off a number to a given number of significant digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

