Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors

09 Jun, 2022
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++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;#define N 100005 // To store number of divisors and// Prefix sum of such numbersint d[N], pre[N]; // Function to find the number of integers// 1 < x < N for which x and x + 1 have// the same number of positive divisorsvoid Positive_Divisors(){    // Count the number of divisors    for (int i = 1; i < N; i++) {         // Run a loop upto sqrt(i)        for (int j = 1; j * j <= i; j++) {             // If j is divisor of i            if (i % j == 0) {                 // If it is perfect square                if (j * j == i)                    d[i]++;                else                    d[i] += 2;            }        }    }     int ans = 0;     // x and x+1 have same number of    // positive divisors    for (int i = 2; i < N; i++) {        if (d[i] == d[i - 1])            ans++;        pre[i] = ans;    }} // Driver codeint main(){    // Function call    Positive_Divisors();     int n = 15;     // Required answer    cout << pre[n] << endl;     return 0;}
// Java implementation of the approachclass GFG{     static int N =100005; // To store number of divisors and// Prefix sum of such numbersstatic int d[] = new int[N], pre[] = new int[N]; // Function to find the number of integers// 1 < x < N for which x and x + 1 have// the same number of positive divisorsstatic void Positive_Divisors(){    // Count the number of divisors    for (int i = 1; i < N; i++)    {         // Run a loop upto sqrt(i)        for (int j = 1; j * j <= i; j++)        {             // If j is divisor of i            if (i % j == 0)            {                 // If it is perfect square                if (j * j == i)                    d[i]++;                else                    d[i] += 2;            }        }    }     int ans = 0;     // x and x+1 have same number of    // positive divisors    for (int i = 2; i < N; i++)    {        if (d[i] == d[i - 1])            ans++;        pre[i] = ans;    }} // Driver codepublic static void main(String[] args){    // Function call    Positive_Divisors();     int n = 15;     // Required answer    System.out.println(pre[n]);}} /* This code contributed by PrinciRaj1992 */
# Python3 implementation of the above approachfrom math import sqrt; N = 100005 # To store number of divisors and# Prefix sum of such numbersd = [0] * Npre = [0] * N # Function to find the number of integers# 1 < x < N for which x and x + 1 have# the same number of positive divisorsdef Positive_Divisors() :         # Count the number of divisors    for i in range(N) :         # Run a loop upto sqrt(i)        for j in range(1, int(sqrt(i)) + 1) :             # If j is divisor of i            if (i % j == 0) :                 # If it is perfect square                if (j * j == i) :                    d[i] += 1                else :                    d[i] += 2     ans = 0     # x and x+1 have same number of    # positive divisors    for i in range(2, N) :        if (d[i] == d[i - 1]) :            ans += 1        pre[i] = ans     # Driver codeif __name__ == "__main__" :     # Function call    Positive_Divisors()     n = 15     # Required answer    print(pre[n]) # This code is contributed by Ryuga
// C# implementation of the approachusing System; class GFG{     static int N =100005; // To store number of divisors and// Prefix sum of such numbersstatic int []d = new int[N];static int []pre = new int[N]; // Function to find the number of integers// 1 < x < N for which x and x + 1 have// the same number of positive divisorsstatic void Positive_Divisors(){    // Count the number of divisors    for (int i = 1; i < N; i++)    {         // Run a loop upto sqrt(i)        for (int j = 1; j * j <= i; j++)        {             // If j is divisor of i            if (i % j == 0)            {                 // If it is perfect square                if (j * j == i)                    d[i]++;                else                    d[i] += 2;            }        }    }     int ans = 0;     // x and x+1 have same number of    // positive divisors    for (int i = 2; i < N; i++)    {        if (d[i] == d[i - 1])            ans++;        pre[i] = ans;    }} // Driver codepublic static void Main(String[] args){    // Function call    Positive_Divisors();     int n = 15;     // Required answer    Console.WriteLine(pre[n]);}} // This code has been contributed by 29AjayKumar
<?php // PHP implementation of the approach $N = 100005; // To store number of divisors and// Prefix sum of such numbers$d = array_fill(0,$N,NULL);$pre = array_fill(0,$N,NULL); // Function to find the number of integers// 1 < x < N for which x and x + 1 have// the same number of positive divisorsfunction Positive_Divisors(){    global $N,$d,$pre;    // Count the number of divisors    for ($i = 1; $i < $N; $i++) {         // Run a loop upto sqrt(i)        for ($j = 1; $j * $j <= $i; $j++) {             // If j is divisor of i            if ($i % $j == 0) {                 // If it is perfect square                if ($j * $j == $i)                    $d[$i]++;                else                    $d[$i] += 2;            }        }    }     $ans = 0;     // x and x+1 have same number of    // positive divisors    for ($i = 2; $i < $N; $i++) {        if ($d[$i] == $d[$i - 1])            $ans++;        $pre[$i] = $ans;    }} // Driver code     // Function call    Positive_Divisors();     $n = 15;     // Required answer    echo $pre[$n] ;     return 0;     // This code is contributed by ChitraNayal?>
<script> // Javascript implementation of the approachconst N = 100005; // To store number of divisors and// Prefix sum of such numberslet d = new Array(N).fill(0);let pre = new Array(N).fill(0); // Function to find the number of integers// 1 < x < N for which x and x + 1 have// the same number of positive divisorsfunction Positive_Divisors(){         // Count the number of divisors    for(let i = 1; i < N; i++)    {         // Run a loop upto sqrt(i)        for(let j = 1; j * j <= i; j++)        {                         // If j is divisor of i            if (i % j == 0)            {                                 // If it is perfect square                if (j * j == i)                    d[i]++;                else                    d[i] += 2;            }        }    }     let ans = 0;     // x and x+1 have same number of    // positive divisors    for(let i = 2; i < N; i++)    {        if (d[i] == d[i - 1])            ans++;                     pre[i] = ans;    }} // Driver code // Function callPositive_Divisors();let n = 15; // Required answerdocument.write(pre[n]); // This code is contributed by souravmahato348 </script>
2
 
Time complexity: O(N3/2)
Auxiliary Space: O(N)
ankthon
princiraj1992
29AjayKumar
ukasp
souravmahato348
samim2000
rishavnitro
divisors
frequency-counting
number-theory
prefix-sum
Mathematical
prefix-sum
number-theory
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Sieve of Eratosthenes
Prime Numbers
Program to find GCD or HCF of two numbers
Find minimum number of coins that make a given value
Minimum number of jumps to reach end
Algorithm to solve Rubik's Cube
The Knight's tour problem | Backtracking-1
Program for Decimal to Binary Conversion