Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
Given an integer N. The task is to find the number of integers 1 < x < N, for which x and x + 1 have the same number of positive divisors.
Examples:
Input: N = 3
Output: 1
Divisors(1) = 1
Divisors(2) = 1 and 2
Divisors(3) = 1 and 3
Only valid x is 2.
Input: N = 15
Output: 2
Approach: Find the number of divisors of all numbers below N and store them in an array. And count the number of integers x such that x and x + 1 have the same number of positive divisors by running a loop.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 100005
int d[N], pre[N];
void Positive_Divisors()
{
for ( int i = 1; i < N; i++) {
for ( int j = 1; j * j <= i; j++) {
if (i % j == 0) {
if (j * j == i)
d[i]++;
else
d[i] += 2;
}
}
}
int ans = 0;
for ( int i = 2; i < N; i++) {
if (d[i] == d[i - 1])
ans++;
pre[i] = ans;
}
}
int main()
{
Positive_Divisors();
int n = 15;
cout << pre[n] << endl;
return 0;
}
|
Java
class GFG
{
static int N = 100005 ;
static int d[] = new int [N], pre[] = new int [N];
static void Positive_Divisors()
{
for ( int i = 1 ; i < N; i++)
{
for ( int j = 1 ; j * j <= i; j++)
{
if (i % j == 0 )
{
if (j * j == i)
d[i]++;
else
d[i] += 2 ;
}
}
}
int ans = 0 ;
for ( int i = 2 ; i < N; i++)
{
if (d[i] == d[i - 1 ])
ans++;
pre[i] = ans;
}
}
public static void main(String[] args)
{
Positive_Divisors();
int n = 15 ;
System.out.println(pre[n]);
}
}
|
Python3
from math import sqrt;
N = 100005
d = [ 0 ] * N
pre = [ 0 ] * N
def Positive_Divisors() :
for i in range (N) :
for j in range ( 1 , int (sqrt(i)) + 1 ) :
if (i % j = = 0 ) :
if (j * j = = i) :
d[i] + = 1
else :
d[i] + = 2
ans = 0
for i in range ( 2 , N) :
if (d[i] = = d[i - 1 ]) :
ans + = 1
pre[i] = ans
if __name__ = = "__main__" :
Positive_Divisors()
n = 15
print (pre[n])
|
C#
using System;
class GFG
{
static int N =100005;
static int []d = new int [N];
static int []pre = new int [N];
static void Positive_Divisors()
{
for ( int i = 1; i < N; i++)
{
for ( int j = 1; j * j <= i; j++)
{
if (i % j == 0)
{
if (j * j == i)
d[i]++;
else
d[i] += 2;
}
}
}
int ans = 0;
for ( int i = 2; i < N; i++)
{
if (d[i] == d[i - 1])
ans++;
pre[i] = ans;
}
}
public static void Main(String[] args)
{
Positive_Divisors();
int n = 15;
Console.WriteLine(pre[n]);
}
}
|
Javascript
<script>
const N = 100005;
let d = new Array(N).fill(0);
let pre = new Array(N).fill(0);
function Positive_Divisors()
{
for (let i = 1; i < N; i++)
{
for (let j = 1; j * j <= i; j++)
{
if (i % j == 0)
{
if (j * j == i)
d[i]++;
else
d[i] += 2;
}
}
}
let ans = 0;
for (let i = 2; i < N; i++)
{
if (d[i] == d[i - 1])
ans++;
pre[i] = ans;
}
}
Positive_Divisors();
let n = 15;
document.write(pre[n]);
</script>
|
PHP
<?php
$N = 100005;
$d = array_fill (0, $N ,NULL);
$pre = array_fill (0, $N ,NULL);
function Positive_Divisors()
{
global $N , $d , $pre ;
for ( $i = 1; $i < $N ; $i ++) {
for ( $j = 1; $j * $j <= $i ; $j ++) {
if ( $i % $j == 0) {
if ( $j * $j == $i )
$d [ $i ]++;
else
$d [ $i ] += 2;
}
}
}
$ans = 0;
for ( $i = 2; $i < $N ; $i ++) {
if ( $d [ $i ] == $d [ $i - 1])
$ans ++;
$pre [ $i ] = $ans ;
}
}
Positive_Divisors();
$n = 15;
echo $pre [ $n ] ;
return 0;
?>
|
Time complexity: O(N3/2)
Auxiliary Space: O(N)
Approach 2:
One approach to finding the number of integers 1 < x < N for which x and x + 1 have the same number of positive divisors is to use a prime factorization method. This involves finding the prime factors of each number and using the formula for the number of divisors of a number based on its prime factorization.
Here is the step-by-step algorithm for implementing the approach:
- Define a function Positive_Divisors() to calculate the number of positive divisors for each integer in the range [2, N).
- In the function Positive_Divisors(), initialize an array d of size N to store the number of divisors for each integer in the range [2, N).
- Iterate over each integer i in the range [2, N) using a loop.
- For each integer i, calculate its prime factorization and compute the number of divisors using the formula for the number of divisors of a positive integer. Store the result in d[i].
- Initialize a variable ans to 0 to store the final answer.
- Iterate over each integer i in the range [2, N) using another loop.
- For each integer i, check if d[i] == d[i – 1], If this condition is true, then increment ans by 1.
- Initialize an array pre of size N to store the prefix sum of the answer.
- For each integer i in the range [2, N), set pre[i] = ans.
- In the main() function, call the function Positive_Divisors().
- Print the value of pre[n], where n is a given integer.
C++
#include <bits/stdc++.h>
using namespace std;
#define N 100005
int d[N], pre[N];
void Positive_Divisors()
{
for ( int i = 2; i < N; i++) {
int num = i;
int divisors = 1;
for ( int j = 2; j * j <= num; j++) {
int count = 0;
while (num % j == 0) {
count++;
num /= j;
}
divisors *= (count + 1);
}
if (num > 1)
divisors *= 2;
d[i] = divisors;
}
int ans = 0;
for ( int i = 2; i < N; i++) {
if (d[i] == d[i - 1])
ans++;
pre[i] = ans;
}
}
int main()
{
Positive_Divisors();
int n = 15;
cout << pre[n] << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
static final int N = 100005 ;
static int [] d = new int [N];
static int [] pre = new int [N];
static void Positive_Divisors()
{
for ( int i = 2 ; i < N; i++) {
int num = i;
int divisors = 1 ;
for ( int j = 2 ; j * j <= num; j++) {
int count = 0 ;
while (num % j == 0 ) {
count++;
num /= j;
}
divisors *= (count + 1 );
}
if (num > 1 )
divisors *= 2 ;
d[i] = divisors;
}
int ans = 0 ;
for ( int i = 2 ; i < N; i++) {
if (d[i] == d[i - 1 ])
ans++;
pre[i] = ans;
}
}
public static void main(String[] args)
{
Positive_Divisors();
int n = 15 ;
System.out.println(pre[n]);
}
}
|
Python3
N = 100005
d = [ 0 ] * N
pre = [ 0 ] * N
def Positive_Divisors():
global N, d, pre
for i in range ( 2 , N):
num = i
divisors = 1
j = 2
while j * j < = num:
count = 0
while num % j = = 0 :
count + = 1
num / / = j
divisors * = (count + 1 )
j + = 1
if num > 1 :
divisors * = 2
d[i] = divisors
ans = 0
for i in range ( 2 , N):
if d[i] = = d[i - 1 ]:
ans + = 1
pre[i] = ans
Positive_Divisors()
n = 15
print (pre[n])
|
C#
using System;
public class GFG
{
const int N = 100005;
static int [] d = new int [N];
static int [] pre = new int [N];
static void PositiveDivisors()
{
for ( int i = 2; i < N; i++)
{
int num = i;
int divisors = 1;
for ( int j = 2; j * j <= num; j++)
{
int count = 0;
while (num % j == 0)
{
count++;
num /= j;
}
divisors *= (count + 1);
}
if (num > 1)
divisors *= 2;
d[i] = divisors;
}
int ans = 0;
for ( int i = 2; i < N; i++)
{
if (d[i] == d[i - 1])
ans++;
pre[i] = ans;
}
}
public static void Main( string [] args)
{
PositiveDivisors();
int n = 15;
Console.WriteLine(pre[n]);
}
}
|
Javascript
const N = 100005;
const d = new Array(N).fill(0);
const pre = new Array(N).fill(0);
function positiveDivisors() {
for (let i = 2; i < N; i++) {
let num = i;
let divisors = 1;
for (let j = 2; j * j <= num; j++) {
let count = 0;
while (num % j === 0) {
count++;
num /= j;
}
divisors *= count + 1;
}
if (num > 1) divisors *= 2;
d[i] = divisors;
}
let ans = 0;
for (let i = 2; i < N; i++) {
if (d[i] === d[i - 1]) ans++;
pre[i] = ans;
}
}
positiveDivisors();
const n = 15;
console.log(pre[n]);
|
Time Complexity: O(N log N)
Auxiliary Space: O(N)
Explaination :
The time complexity of this code is O(N log N) because calculating the prime factorization of each integer in the range [2, N) takes O(log N) time and this operation is performed for each integer in the range [2, N).
The auxiliary space complexity is O(N) because two arrays of size N are used.
Last Updated :
29 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...