Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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
Advertisements
