- 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
Find a range of composite numbers of given length in C++
Suppose we have a number n. We have to find the range of positive integers, where all the numbers in the range is composite, and the length of the range is n. If there are more than one range, then print any one range. The composite number is a number where it has at least one divisor other than 1 and itself.
As the length of the range is n, then if the first number is a, then the other numbers are a + 1, a + 2, …, a + n – 1, all should be composite. If we see that x!, where x is positive integer, then x has factors of 2, 3, 4, …, p – 1. So p! + i has a factor i, so p! + i must be composite. p! + 2, p! + 3, … p! + p – 1, are all composite. So the range will be [p! + 2, p! + p – 1]
Example
#include<iostream> using namespace std; int fact (int n) { if (n == 0) return 1; return n * fact(n-1); } void showRange(int n) { int a = fact(n + 2) + 2; int b = a + n - 1; cout << "[" << a << ", " << b << "]"; } int main() { int n = 3 ; showRange(n); }
Output
[122, 124]
- Related Articles
- Python - Find the number of prime numbers within a given range of numbers
- Program to find bitwise AND of range of numbers in given range in Python
- Find numbers with K odd divisors in a given range in C++
- C++ Program to find Numbers in a Range with Given Digital Root
- Program to find count of numbers having odd number of divisors in given range in C++
- Count factorial numbers in a given range in C++
- C++ program to find numbers with K odd divisors in a given range
- C++ Program to Generate Randomized Sequence of Given Range of Numbers
- PHP program to find the sum of odd numbers within a given range
- Program to find out the number of special numbers in a given range in Python
- Print prime numbers in a given range using C++ STL
- XOR of numbers that appeared even number of times in given Range in C++
- Write a Golang program to find prime numbers in a given range
- Create list of numbers with given range in Python
- Print all Good numbers in given range in C++

Advertisements