Smallest integer which has n factors or more
Last Updated :
23 Jun, 2022
Given n, find the smallest integer which has n factors or more. It may be assumed that the result is less than 1000001.
Examples:
Input : n = 3
Output : 4
Explanation: 4 has factors 1, 2 and 4.
Input : n = 2
Output : 2
Explanation: 2 has one factor 1 and 2.
There are many methods to calculate the number of factors, but the efficient one can be found here
Simple Approach: A simple approach will be to run a loop to find out the factors of a number. One for finding out the factors of a number in O(x) is to run a loop from 1 to x and see all numbers that divide x.
Time Complexity: O(x) for every number x that we try until we find the answer or reach the limit.
Efficient Approach: We can find out factors in sqrt(x)for every iteration.
Time Complexity: O(sqrt(x)) for every number x that we try until we find the answer or reach the limit.
The Best Approach will be to traverse for every number and calculate the number of factors. Then check if the count is equal to or more than n then we get our desired smallest integer with n or more factors.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000001;
int factor[MAX] = { 0 };
void generatePrimeFactors()
{
factor[1] = 1;
for ( int i = 2; i < MAX; i++)
factor[i] = i;
for ( int i = 4; i < MAX; i += 2)
factor[i] = 2;
for ( int i = 3; i * i < MAX; i++) {
if (factor[i] == i) {
for ( int j = i * i; j < MAX; j += i) {
if (factor[j] == j)
factor[j] = i;
}
}
}
}
int calculateNoOFactors( int n)
{
if (n == 1)
return 1;
int ans = 1;
int dup = factor[n];
int c = 1;
int j = n / factor[n];
while (j != 1) {
if (factor[j] == dup)
c += 1;
else {
dup = factor[j];
ans = ans * (c + 1);
c = 1;
}
j = j / factor[j];
}
ans = ans * (c + 1);
return ans;
}
int smallest( int n)
{
for ( int i = 1;; i++)
if (calculateNoOFactors(i) >= n)
return i;
}
int main()
{
generatePrimeFactors();
int n = 4;
cout << smallest(n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GfG{
private static final int MAX = 1000001 ;
private static final int [] factor = new int [MAX];
public static void generatePrimeFactors()
{
factor[ 1 ] = 1 ;
for ( int i = 2 ; i < MAX; i++)
factor[i] = i;
for ( int i = 4 ; i < MAX; i += 2 )
factor[i] = 2 ;
for ( int i = 3 ; i * i < MAX; i++) {
if (factor[i] == i) {
for ( int j = i * i; j < MAX; j += i) {
if (factor[j] == j)
factor[j] = i;
}
}
}
}
public static int calculateNoOFactors( int n)
{
if (n == 1 )
return 1 ;
int ans = 1 ;
int dup = factor[n];
int c = 1 ;
int j = n / factor[n];
while (j != 1 ) {
if (factor[j] == dup)
c += 1 ;
else {
dup = factor[j];
ans = ans * (c + 1 );
c = 1 ;
}
j = j / factor[j];
}
ans = ans * (c + 1 );
return ans;
}
public static int smallest( int n)
{
for ( int i = 1 ;; i++)
if (calculateNoOFactors(i) >= n)
return i;
}
public static void main(String args[])
{
generatePrimeFactors();
int n = 4 ;
System.out.println(smallest(n));
}
}
|
Python3
MAX = 100001 ;
factor = [ 0 ] * MAX ;
def generatePrimeFactors():
factor[ 1 ] = 1 ;
for i in range ( 2 , MAX ):
factor[i] = i;
i = 4
while (i < MAX ):
factor[i] = 2 ;
i + = 2 ;
i = 3 ;
while (i * i < MAX ):
if (factor[i] = = i):
j = i * i;
while (j < MAX ):
if (factor[j] = = j):
factor[j] = i;
j + = i;
i + = 1 ;
def calculateNoOFactors(n):
if (n = = 1 ):
return 1 ;
ans = 1 ;
dup = factor[n];
c = 1 ;
j = int (n / factor[n]);
while (j ! = 1 ):
if (factor[j] = = dup):
c + = 1 ;
else :
dup = factor[j];
ans = ans * (c + 1 );
c = 1 ;
j = int (j / factor[j]);
ans = ans * (c + 1 );
return ans;
def smallest(n):
i = 1 ;
while ( True ):
if (calculateNoOFactors(i) > = n):
return i;
i + = 1 ;
generatePrimeFactors();
n = 4 ;
print (smallest(n));
|
C#
using System;
class GfG {
private static int MAX = 1000001;
private static int []factor = new int [MAX];
public static void generatePrimeFactors()
{
factor[1] = 1;
for ( int i = 2; i < MAX; i++)
factor[i] = i;
for ( int i = 4; i < MAX; i += 2)
factor[i] = 2;
for ( int i = 3; i * i < MAX; i++) {
if (factor[i] == i) {
for ( int j = i * i; j < MAX; j += i)
{
if (factor[j] == j)
factor[j] = i;
}
}
}
}
public static int calculateNoOFactors( int n)
{
if (n == 1)
return 1;
int ans = 1;
int dup = factor[n];
int c = 1;
int j = n / factor[n];
while (j != 1) {
if (factor[j] == dup)
c += 1;
else {
dup = factor[j];
ans = ans * (c + 1);
c = 1;
}
j = j / factor[j];
}
ans = ans * (c + 1);
return ans;
}
public static int smallest( int n)
{
for ( int i = 1; ; i++)
if (calculateNoOFactors(i) >= n)
return i;
}
public static void Main()
{
generatePrimeFactors();
int n = 4;
Console.Write(smallest(n));
}
}
|
PHP
<?php
$MAX = 100001;
$factor = array_fill (0, $MAX , 0);
function generatePrimeFactors()
{
global $MAX ;
global $factor ;
$factor [1] = 1;
for ( $i = 2; $i < $MAX ; $i ++)
$factor [ $i ] = $i ;
for ( $i = 4; $i < $MAX ; $i += 2)
$factor [ $i ] = 2;
for ( $i = 3; $i * $i < $MAX ; $i ++)
{
if ( $factor [ $i ] == $i )
{
for ( $j = $i * $i ;
$j < $MAX ; $j += $i )
{
if ( $factor [ $j ] == $j )
$factor [ $j ] = $i ;
}
}
}
}
function calculateNoOFactors( $n )
{
global $factor ;
if ( $n == 1)
return 1;
$ans = 1;
$dup = $factor [ $n ];
$c = 1;
$j = (int)( $n / $factor [ $n ]);
while ( $j != 1)
{
if ( $factor [ $j ] == $dup )
$c += 1;
else
{
$dup = $factor [ $j ];
$ans = $ans * ( $c + 1);
$c = 1;
}
$j = (int)( $j / $factor [ $j ]);
}
$ans = $ans * ( $c + 1);
return $ans ;
}
function smallest( $n )
{
for ( $i = 1;; $i ++)
if (calculateNoOFactors( $i ) >= $n )
return $i ;
}
generatePrimeFactors();
$n = 4;
echo smallest( $n );
?>
|
Javascript
<script>
var MAX = 1000001;
var factor = Array(MAX).fill(0);
function generatePrimeFactors() {
factor[1] = 1;
for (i = 2; i < MAX; i++)
factor[i] = i;
for (i = 4; i < MAX; i += 2)
factor[i] = 2;
for (i = 3; i * i < MAX; i++) {
if (factor[i] == i) {
for (j = i * i; j < MAX; j += i) {
if (factor[j] == j)
factor[j] = i;
}
}
}
}
function calculateNoOFactors(n) {
if (n == 1)
return 1;
var ans = 1;
var dup = factor[n];
var c = 1;
var j = n / factor[n];
while (j != 1) {
if (factor[j] == dup)
c += 1;
else {
dup = factor[j];
ans = ans * (c + 1);
c = 1;
}
j = j / factor[j];
}
ans = ans * (c + 1);
return ans;
}
function smallest(n) {
for (i = 1;; i++)
if (calculateNoOFactors(i) >= n)
return i;
}
generatePrimeFactors();
var n = 4;
document.write(smallest(n));
</script>
|
Output:
6
Time Complexity: O(1000001)
Auxiliary Space: O(1000001), size of this array is used
Please suggest if someone has a better solution which is more efficient in terms of space and time.
Share your thoughts in the comments
Please Login to comment...