- 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 the number of integers x in range (1,N) for which x and x+1 have same number of divisors in C++

Suppose, we have an integer N, we have to find the number of integers 1 < x < N, for which x and x + 1 has same number of positive divisors. So if N = 3, then output will be 1, as divisor of 1 is 1, divisor of 2 is 1 and 2, and divisor of 3 is 1 and 3.

To solve this, we will find the number of divisors of all numbers below N, and store them in an array. Then count number of integers x such that x, such that x + 1 have the same number of positive divisors by running the loop.

## Example

#include<iostream> #include<cmath> #define N 100005 using namespace std; int table[N], pre[N]; void findPositiveDivisor() { for (int i = 1; i < N; i++) { for (int j = 1; j * j <= i; j++) { if (i % j == 0) { if (j * j == i) table[i]++; else table[i] += 2; } } } int ans = 0; for (int i = 2; i < N; i++) { if (table[i] == table[i - 1]) ans++; pre[i] = ans; } } int main() { findPositiveDivisor(); int n = 15; cout << "Number of integers: " << pre[n] << endl; }

## Output

Number of integers: 2

- Related Questions & Answers
- Find the Number of Solutions of n = x + n x using C++
- Find the number of divisors of all numbers in the range [1, n] in C++
- Count of values of x <= n for which (n XOR x) = (n – x) in C++
- Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n in C++
- Program to find sum of 1 + x/2! + x^2/3! +…+x^n/(n+1)! in C++
- Count the numbers < N which have equal number of divisors as K in C++
- Count number of solutions of x^2 = 1 (mod p) in given range in C++
- Find the Number of solutions for the equation x + y + z <= n using C++
- Find the number of integers from 1 to n which contains digits 0’s and 1’s only in C++
- Find the largest good number in the divisors of given number N in C++
- Count the number of ways to tile the floor of size n x m using 1 x m size tiles in C++
- Find a number x such that sum of x and its digits is equal to given n in C++
- Find x, y, z that satisfy 2/n = 1/x + 1/y + 1/z in C++
- Construct a frequency array of digits of the values obtained from x^1, x^2, ....., x^n in C++
- Difference between x++ and x = x+1 in Java

Advertisements