Skip to content
Related Articles

Related Articles

Improve Article
Count integers up to N that are equal to at least 2nd power of any integer exceeding 1
  • Last Updated : 25 May, 2021

Given a positive integer N, the task is to count the number of integers from the range [1, N], that can be represented as ab, where a and b are integers greater than 1.

Examples:

Input: N = 6
Output: 1
Explanation:
Only such integer from the range [1, 6] is 4 (= 22).
Therefore, the required count is 1.

Input: N = 10
Output: 3

Approach: The given problem can be solved by counting all the possible pairs of elements (a, b) such that ab is at most N. Follow the steps below to solve the problem:



  • Initialize a HashSet to store all possible values of ab which is at most N.
  • Iterate over the range [2, √N], and for each value of a, insert all possible values of ab having value at most N, where b lies over the range [1, N].
  • After completing the above steps, print the size of the HashSet as the resultant count of integers.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to count the integers
// up to N that can be represented
// as a ^ b, where a &b > 1
void printNumberOfPairs(int N)
{
     
    // Initialize a HashSet
    unordered_set<int> st;
 
    // Iterating over the range
    // [2, sqrt(N)]
    for(int i = 2; i * i <= N; i++)
    {
        int x = i;
 
        // Generate all possible
        // power of x
        while (x <= N)
        {
             
            // Multiply x by i
            x *= i;
 
            // If the generated number
            // lies in the range [1, N]
            // then insert it in HashSet
            if (x <= N)
            {
                st.insert(x);
            }
        }
    }
 
    // Print the total count
    cout << st.size();
}
 
// Driver code
int main()
{
    int N = 10000;
    printNumberOfPairs(N);
 
    return 0;
}
 
// This code is contributed by Kingash

Java




// Java program for the above approach
 
import java.util.HashSet;
 
public class GFG {
 
    // Function to count the integers
    // up to N that can be represented
    // as a ^ b, where a &b > 1
    static void printNumberOfPairs(int N)
    {
        // Initialize a HashSet
        HashSet<Integer> st
            = new HashSet<Integer>();
 
        // Iterating over the range
        // [2, sqrt(N)]
        for (int i = 2; i * i <= N; i++) {
 
            int x = i;
 
            // Generate all possible
            // power of x
            while (x <= N) {
 
                // Multiply x by i
                x *= i;
 
                // If the generated number
                // lies in the range [1, N]
                // then insert it in HashSet
                if (x <= N) {
                    st.add(x);
                }
            }
        }
 
        // Print the total count
        System.out.println(st.size());
    }
 
    // Driver Code
    public static void main(String args[])
    {
        int N = 10000;
        printNumberOfPairs(N);
    }
}

Python3




# Python 3 program for the above approach
from math import sqrt
 
# Function to count the integers
# up to N that can be represented
# as a ^ b, where a &b > 1
def printNumberOfPairs(N):
   
    # Initialize a HashSet
    st = set()
 
    # Iterating over the range
    # [2, sqrt(N)]
    for i in range(2, int(sqrt(N)) + 1, 1):
        x = i
         
        # Generate all possible
        # power of x
        while(x <= N):
           
            # Multiply x by i
            x *= i
             
            # If the generated number
            # lies in the range [1, N]
            # then insert it in HashSet
            if (x <= N):
                st.add(x)
 
    # Print the total count
    print(len(st))
 
# Driver code
if __name__ == '__main__':
    N = 10000
    printNumberOfPairs(N)
     
    # This code is contributed by ipg2016107.

C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to count the integers
// up to N that can be represented
// as a ^ b, where a &b > 1
static void printNumberOfPairs(int N)
{
     
    // Initialize a HashSet
    HashSet<int> st = new HashSet<int>();
 
    // Iterating over the range
    // [2, sqrt(N)]
    for(int i = 2; i * i <= N; i++)
    {
        int x = i;
 
        // Generate all possible
        // power of x
        while (x <= N)
        {
             
            // Multiply x by i
            x *= i;
 
            // If the generated number
            // lies in the range [1, N]
            // then insert it in HashSet
            if (x <= N)
            {
                st.Add(x);
            }
        }
    }
 
    // Print the total count
    Console.WriteLine(st.Count);
}
 
// Driver Code
public static void Main(string[] args)
{
    int N = 10000;
    printNumberOfPairs(N);
}
}
 
// This code is contributed by ukasp

Javascript




<script>
 
// Javascript program for the above approach
 
 
// Function to count the integers
// up to N that can be represented
// as a ^ b, where a &b > 1
function printNumberOfPairs( N)
{
    // Initialize a HashSet
    var st = new Set();
 
    // Iterating over the range
    // [2, sqrt(N)]
    for (let i = 2; i * i <= N; i++) {
 
        let x = i;
 
        // Generate all possible
        // power of x
        while (x <= N) {
 
            // Multiply x by i
            x *= i;
 
            // If the generated number
            // lies in the range [1, N]
            // then insert it in HashSet
            if (x <= N) {
                st.add(x);
            }
        }
    }
 
    // Print the total count
    document.write(st.size);
}
 
// Driver Code
 
let N = 10000;
printNumberOfPairs(N);
 
</script>
Output: 
124

 

Time Complexity: O(N log N)
Auxiliary Space: O(N)

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :