- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
k-Rough Number or k-Jagged Number in C++
In this tutorial, we are going to write a program that checks whether the given number is k-rough or k-jagged number or not.
The number whose smallest prime factor is greater than or equal to the given k, it is called k-rough or k-jagged number.
Let's see the steps to solve the problem.
- Initialise the numbers n and k.
- Find all the prime numbers that are factors of n and store them in a vector.
- Get the first element from the vector and compare it with k to check whether n is k-rough or k-jagged number or not.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; bool isPrime(int n) { for (int i = 2; i * i <= n; i++) { if (n % i == 0) { return false; } } return true; } vector<int> getPrimes(int n) { vector<int> primes; for (int i = 2; i < n; i++) { if (n % i == 0 && isPrime(i)) { primes.push_back(i); } } return primes; } bool isRoughNumber(int n, int k) { vector<int> primes = getPrimes(n); return primes[0] >= k; } int main() { int n = 75, k = 3; if (isRoughNumber(n, k)) { cout << n << " is a " << k << " rough number" << endl; }else { cout << n << " is not a " << k << " rough number" << endl; } return 0; }
Output
If you run the above program, then you will get the following result.
75 is a 3 rough number
Conclusion
You can avoid storing all the prime numbers in the vector. And find the first prime factor of n and compare it with k to get the desired output. Implement the idea which is similar to the above one with lesser space and time complexity.
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Find nth number that contains the digit k or divisible by k in C++
- Find smallest number K such that K % p = 0 and q % K = 0 in C++
- Find the number of subarrays have bitwise OR >= K using C++
- Largest number smaller than or equal to N divisible by K in C++
- Minimum Number of K Consecutive Bit Flips in C++
- Largest K digit number divisible by X in C++
- Find prime number K in an array such that (A[i] % K) is maximum in C++
- Minimum number of bottles required to fill K glasses in C++
- Count number of substrings with exactly k distinct characters in C++
- Splitting Number into k length array in JavaScript
- C++ Program for Smallest K digit number divisible by X?
- C++ Program for Largest K digit number divisible by X?
- C++ Programming for Smallest K digit number divisible by X?
- C++ code to find greater number whose factor is k
- Find the Number of permutation with K inversions using C++

Advertisements