Given a positive integer N, the task is to count the number of integers from the range [1, N] having exactly 5 divisors.
Examples:
Input: N = 18
Output: 1
Explanation:
From all the integers over the range [1, 18], 16 is the only integer that has exactly 5 divisors, i.e. 1, 2, 8, 4 and 16.
Therefore, the count of such integers is 1.
Input: N = 100
Output: 2
Naive Approach: The simplest approach to solve the given problem is to iterate over the range [1, N] and count those integers in this range having the count of divisors as 5.
C++
#include <iostream>
#include <cmath>
using namespace std;
void SieveOfEratosthenes( int n, bool prime[],
bool primesquare[], int a[]) {
for ( int i = 2; i <= n; i++)
prime[i] = true ;
for ( int i = 0; i <= (n * n + 1); i++)
primesquare[i] = false ;
prime[1] = false ;
for ( int p = 2; p * p <= n; p++) {
if (prime[p] == true ) {
for ( int i = p * p; i <= n; i += p)
prime[i] = false ;
}
}
int j = 0;
for ( int p = 2; p <= n; p++) {
if (prime[p]) {
a[j] = p;
primesquare[p * p] = true ;
j++;
}
}
}
int countDivisors( int n) {
if (n == 1)
return 1;
bool prime[n + 1], primesquare[n * n + 1];
int a[n];
SieveOfEratosthenes(n, prime, primesquare, a);
int ans = 1;
for ( int i = 0;; i++) {
if (a[i] * a[i] * a[i] > n)
break ;
int cnt = 1;
while (n % a[i] == 0)
{
n = n / a[i];
cnt = cnt + 1;
}
ans = ans * cnt;
}
if (prime[n])
ans = ans * 2;
else if (primesquare[n])
ans = ans * 3;
else if (n != 1)
ans = ans * 4;
return ans;
}
int countIntegers( int n) {
int count = 0;
for ( int i = 1; i <= n; i++) {
int divisors = countDivisors(i);
if (divisors == 5 ) {
count++;
}
}
return count;
}
int main() {
int n = 100;
cout << countIntegers(n) << endl;
return 0;
}
|
Java
import java.util.Vector;
public class GFG {
static void SieveOfEratosthenes( int n, boolean prime[],
boolean primesquare[],
int a[])
{
for ( int i = 2 ; i <= n; i++)
prime[i] = true ;
for ( int i = 0 ; i < ((n * n) + 1 ); i++)
primesquare[i] = false ;
prime[ 1 ] = false ;
for ( int p = 2 ; p * p <= n; p++) {
if (prime[p] == true ) {
for ( int i = p * 2 ; i <= n; i += p)
prime[i] = false ;
}
}
int j = 0 ;
for ( int p = 2 ; p <= n; p++) {
if (prime[p]) {
a[j] = p;
primesquare[p * p] = true ;
j++;
}
}
}
static int countDivisors( int n)
{
if (n == 1 )
return 1 ;
boolean prime[] = new boolean [n + 1 ];
boolean primesquare[] = new boolean [(n * n) + 1 ];
int a[] = new int [n];
SieveOfEratosthenes(n, prime, primesquare, a);
int ans = 1 ;
for ( int i = 0 ;; i++) {
if (a[i] * a[i] * a[i] > n)
break ;
int cnt = 1 ;
while (n % a[i] == 0 ) {
n = n / a[i];
cnt = cnt + 1 ;
}
ans = ans * cnt;
}
if (prime[n])
ans = ans * 2 ;
else if (primesquare[n])
ans = ans * 3 ;
else if (n != 1 )
ans = ans * 4 ;
return ans;
}
static int countIntegers( int n)
{
int count = 0 ;
for ( int i = 1 ; i <= n; i++) {
int divisors = countDivisors(i);
if (divisors == 5 ) {
count++;
}
}
return count;
}
public static void main(String[] args)
{
int N = 100 ;
System.out.println(countIntegers(N));
}
}
|
Python3
import math
def sieveOfEratosthenes(n):
prime = [ True for i in range (n + 1 )]
p = 2
while (p * p < = n):
if (prime[p] = = True ):
for i in range (p * p, n + 1 , p):
prime[i] = False
p + = 1
primes = []
for p in range ( 2 , n + 1 ):
if prime[p]:
primes.append(p)
return primes
def countDivisors(n, primes):
if (n = = 1 ):
return 1
ans = 1
i = 0
while (primes[i] < = math.sqrt(n)):
cnt = 1
while (n % primes[i] = = 0 ):
n = n / / primes[i]
cnt + = 1
ans = ans * cnt
i + = 1
if (n > 1 ):
ans = ans * 2
return ans
def countIntegers(n):
count = 0
primes = sieveOfEratosthenes(n)
for i in range ( 1 , n + 1 ):
divisors = countDivisors(i, primes)
if (divisors = = 5 and int (math.sqrt(i)) * * 2 = = i):
count + = 1
return count
if __name__ = = "__main__" :
n = 100
print (countIntegers(n))
|
Javascript
function sieveOfEratosthenes(n)
{
let prime = new Array(n+1).fill( true );
let p = 2;
while (p * p <= n)
{
if (prime[p] == true )
{
for (let i = p * p; i <= n; i += p) {
prime[i] = false ;
}
}
p += 1;
}
let primes = [];
for (let p = 2; p <= n; p++) {
if (prime[p] == true ) {
primes.push(p);
}
}
return primes;
}
function countDivisors(n, primes)
{
if (n == 1) {
return 1;
}
let ans = 1;
let i = 0;
while (primes[i] <= Math.sqrt(n))
{
let cnt = 1;
while (n % primes[i] == 0) {
n = n / primes[i];
cnt += 1;
}
ans = ans * cnt;
i += 1;
}
if (n > 1) {
ans = ans * 2;
}
return ans;
}
function countIntegers(n) {
let count = 0;
let primes = sieveOfEratosthenes(n);
for (let i = 1; i <= n; i++)
{
let divisors = countDivisors(i, primes);
if (divisors == 5 && Math.sqrt(i)**2 == i) {
count += 1;
}
}
return count;
}
let n = 100;
console.log(countIntegers(n));
|
Time Complexity: O(N4/3)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by observing a fact that the numbers that have exactly 5 divisors can be expressed in the form of p4, where p is a prime number as the count of divisors is exactly 5. Follow the below steps to solve the problem:
- Generate all primes such that their fourth power is less than 1018 by using Sieve of Eratosthenes and store it in vector, say A[].
- Initialize two variables, say low as 0 and high as A.size() – 1.
- For performing the Binary Search iterate until low is less than high and perform the following steps:
- Find the value of mid as the (low + high)/2.
- Find the value of fourth power of element at indices mid (mid – 1) and store it in a variable, say current and previous respectively.
- If the value of current is N, then print the value of A[mid] as the result.
- If the value of current is greater than N and previous is at most N, then print the value of A[mid] as the result.
- If the value of current is greater than N then update the value of high as (mid – 1). Otherwise, update the value of low as (mid + 1).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define ll long long int
const int MAX = 1e5;
using namespace std;
ll power(ll x, unsigned ll y)
{
ll res = 1;
if (x == 0)
return 0;
while (y > 0) {
if (y & 1)
res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
void SieveOfEratosthenes(
vector<pair<ll, ll> >& v)
{
bool prime[MAX + 1];
memset (prime, true , sizeof (prime));
prime[1] = false ;
for ( int p = 2; p * p <= MAX; p++) {
if (prime[p] == true ) {
for ( int i = p * 2;
i <= MAX; i += p)
prime[i] = false ;
}
}
int num = 1;
for ( int i = 1; i <= MAX; i++) {
if (prime[i]) {
v.push_back({ i, num });
num++;
}
}
}
int countIntegers(ll n)
{
if (n < 16) {
return 0;
}
vector<pair<ll, ll> > v;
SieveOfEratosthenes(v);
int low = 0;
int high = v.size() - 1;
while (low <= high) {
int mid = (low + high) / 2;
ll curr = power(v[mid].first, 4);
ll prev = power(v[mid - 1].first, 4);
if (curr == n) {
return v[mid].second;
}
else if (curr > n and prev <= n) {
return v[mid - 1].second;
}
else if (curr > n) {
high = mid - 1;
}
else {
low = mid + 1;
}
}
return 0;
}
int main()
{
ll N = 100;
cout << countIntegers(N);
return 0;
}
|
Java
import java.util.Vector;
public class GFG {
static int MAX = ( int )1e5;
public static class pair {
long first;
long second;
pair( long first, long second)
{
this .first = first;
this .second = second;
}
}
static long power( long x, long y)
{
long res = 1 ;
if (x == 0 )
return 0 ;
while (y > 0 )
{
if ((y & 1 ) == 1 )
res = (res * x);
y = y >> 1 ;
x = (x * x);
}
return res;
}
static void SieveOfEratosthenes(Vector<pair> v)
{
boolean prime[] = new boolean [MAX + 1 ];
for ( int i = 0 ; i < prime.length; i++) {
prime[i] = true ;
}
prime[ 1 ] = false ;
for ( int p = 2 ; p * p <= MAX; p++) {
if (prime[p] == true ) {
for ( int i = p * 2 ; i <= MAX; i += p)
prime[i] = false ;
}
}
int num = 1 ;
for ( int i = 1 ; i <= MAX; i++) {
if (prime[i]) {
v.add( new pair(i, num));
num++;
}
}
}
static long countIntegers( long n)
{
if (n < 16 ) {
return 0 ;
}
Vector<pair> v = new Vector<>();
SieveOfEratosthenes(v);
int low = 0 ;
int high = v.size() - 1 ;
while (low <= high) {
int mid = (low + high) / 2 ;
long curr = power(v.get(mid).first, 4 );
long prev = power(v.get(mid - 1 ).first, 4 );
if (curr == n) {
return v.get(mid).second;
}
else if (curr > n && prev <= n) {
return v.get(mid - 1 ).second;
}
else if (curr > n) {
high = mid - 1 ;
}
else {
low = mid + 1 ;
}
}
return 0 ;
}
public static void main(String[] args)
{
long N = 100 ;
System.out.println(countIntegers(N));
}
}
|
Python3
def power(x, y):
res = 1
if x = = 0 :
return 0
while y > 0 :
if y& 1 :
res = (res * x)
y = y >> 1
x = (x * x)
return res
def sieveofeartoshenes(vec):
prime = []
for i in range ( pow ( 10 , 5 ) + 1 ):
prime.append( True )
prime[ 1 ] = False
p = 2
while (p * p < = pow ( 10 , 5 )):
if (prime[p] = = True ):
for i in range (p * p, pow ( 10 , 5 ) + 1 , p):
prime[i] = False
p + = 1
num = 1
for i in range ( 1 , pow ( 10 , 5 ) + 1 ):
if prime[i]:
vec.append([i, num])
num + = 1
def count_integer(n):
if n < 16 :
return 0
vec = [[]]
sieveofeartoshenes(vec)
low = 0
high = len (vec) - 1
while low < = high:
mid = (low + high) / / 2
curr = power(vec[mid][ 0 ], 4 )
prev = power(vec[mid - 1 ][ 0 ], 4 )
if curr = = n:
return vec[mid][ 1 ]
elif curr > n and prev < = n:
return vec[mid - 1 ][ 1 ]
elif curr > n:
high = mid - 1
else :
low = mid + 1
n = 100
ans = count_integer(n)
print (ans)
|
C#
using System;
using System.Collections.Generic;
public class pair {
public long first;
public long second;
public pair( long first, long second)
{
this .first = first;
this .second = second;
}
}
class GFG {
static int MAX = ( int )1e5;
static long power( long x, long y)
{
long res = 1;
if (x == 0)
return 0;
while (y > 0) {
if ((y & 1) == 1)
res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
static void SieveOfEratosthenes(List<pair> v)
{
bool [] prime = new bool [MAX + 1];
for ( int i = 0; i < prime.Length; i++) {
prime[i] = true ;
}
prime[1] = false ;
for ( int p = 2; p * p <= MAX; p++) {
if (prime[p] == true ) {
for ( int i = p * 2; i <= MAX; i += p)
prime[i] = false ;
}
}
int num = 1;
for ( int i = 1; i <= MAX; i++) {
if (prime[i]) {
v.Add( new pair(i, num));
num++;
}
}
}
static long countIntegers( long n)
{
if (n < 16) {
return 0;
}
List<pair> v = new List<pair>();
SieveOfEratosthenes(v);
int low = 0;
int high = v.Count - 1;
while (low <= high) {
int mid = (low + high) / 2;
long curr = power(v[mid].first, 4);
long prev = power(v[mid - 1].first, 4);
if (curr == n) {
return v[mid].second;
}
else if (curr > n && prev <= n) {
return v[mid - 1].second;
}
else if (curr > n) {
high = mid - 1;
}
else {
low = mid + 1;
}
}
return 0;
}
public static void Main( string [] args)
{
long N = 100;
Console.WriteLine(countIntegers(N));
}
}
|
Javascript
function power(x, y)
{
let res = 1;
if (x === 0) {
return 0;
}
while (y > 0)
{
if (y&1) {
res = (res*x);
}
y = y >> 1;
x = (x*x);
}
return res;
}
function sieveofeartoshenes(vec) {
let prime = [];
for (let i = 0; i <= Math.pow(10, 5)+1; i++) {
prime.push( true );
}
prime[1] = false ;
let p = 2;
while (p * p <= Math.pow(10, 5)) {
if (prime[p] === true ) {
for (let i = p * p; i <= Math.pow(10, 5) + 1; i += p) {
prime[i] = false ;
}
}
p += 1;
}
let num = 1;
for (let i = 1; i <= Math.pow(10, 5)+1; i++)
{
if (prime[i]) {
vec.push([i, num]);
num += 1;
}
}
}
function count_integer(n)
{
if (n < 16) {
return 0;
}
let vec = [[]];
sieveofeartoshenes(vec);
let low = 0;
let high = vec.length-1;
while (low <= high)
{
let mid = (low+high)
let curr = power(vec[mid][0], 4);
let prev = power(vec[mid-1][0], 4);
if (curr === n)
{
return vec[mid][1];
}
else if (curr > n && prev <= n)
{
return vec[mid-1][1];
}
else if (curr > n)
{
high = mid - 1;
}
else
{
low = mid + 1
}
}
}
let n = 100
let ans = count_integer(n)
console.log(ans)
|
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
13 Apr, 2023
Like Article
Save Article