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.
For example
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.
// 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; }
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
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
Output
75 is a 3-rough 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 contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Find nth term of the Dragon Curve Sequence
- Finding n-th term of series 3, 13, 42, 108, 235…
- Repeated Unit Divisibility
- XOR Encryption by Shifting Plaintext
- First N natural can be divided into two sets with given difference and co-prime sums
- Expressing a number as sum of consecutive | Set 2 (Using odd factors)
- Making zero array by decrementing pairs of adjacent
- Program for sum of cos(x) series
- Blum Integer
- Find the Largest Cube formed by Deleting minimum Digits from a number
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.