

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- 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
- 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
- Create list of numbers with given range in Python
- Write a Golang program to find prime numbers in a given range
- Program to find count of numbers having odd number of divisors in given range in C++
- C++ Program to Generate Randomized Sequence of Given Range of Numbers
- Find numbers with K odd divisors in a given range in C++
- How to find Kaprekar numbers within a given range using Python?
- C++ Program to find Numbers in a Range with Given Digital Root
- Generating a range of numbers in MySQL?
- Find the maximum number of composite summands of a number in Python
- Count factorial numbers in a given range in C++
- Finding Armstrong numbers in a given range in JavaScript
Advertisements